1.把本地音频文件放入res/raw文件夹
2.播放代码
/**
* 播放来电和呼出铃声
*
* @param mContext
*/
private void playFromRawFile(Context mContext) {
//1.获取模式
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
final int ringerMode = am.getRingerMode();
//2.普通模式可以呼叫普通模式: AudioManager.RINGER_MODE_NORMAL 静音模式:AudioManager.RINGER_MODE_VIBRATE 震动模式:AudioManager.RINGER_MODE_SILENT
if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
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;
}