Android MediaPlayer 播放音频文件

1.把本地音频文件放入res/raw文件夹

2.播放代码

try {
                mPlayer = new MediaPlayer();
                AssetFileDescriptor file = mContext.getResources().openRawResourceFd(R.raw.chat_bell);
                try {
                    mPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
                    file.close();
                    if (!mPlayer.isPlaying()) {
                        mPlayer.prepare();
                        mPlayer.start();
                        mPlayer.setLooping(true);//循环播放
                    }
                } catch (IOException e) {
                    mPlayer = null;
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
private void stopPlayFromRawFile() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer.release();
        }
        mPlayer = null;
    }

 

你可能感兴趣的:(安卓,android)