Android原生目录下android.media.MediaPlayer可以播放音频,也可以播放多段连续音频,但是效果不太理想,间隔很明显,MediaPlayer播放音频代码如下:
public class PlayMedia {
private static final String TAG = PlayMedia.class.getSimpleName();
private MediaPlayer mediaPlayer;
private int playIndex = -1;
private List<String> fileNames = null;
private int time = 200;
private Context mContext;
private static class SingletonHolder {
private static final PlayMedia INSTANCE = new PlayMedia();
}
private PlayMedia() {
}
public static final PlayMedia getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 播放单一文件
* @param cxt
* @param res
*/
public void playAudio(Context cxt, int res) {
try {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(cxt, res);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void releaseMediaPlayer() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
private class MyCompleteListener implements MediaPlayer.OnCompletionListener{
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
//间隔多少时间读取下一个语音文件
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
playNext();
}
}, time);
}
}
private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mediaPlayer != null){
mediaPlayer.start();
}
}
};
/**
* 播放一组语音文件
* @param cxt
* @param audioFileNames
*/
public void playAudios(Context cxt, List<String> audioFileNames){
mContext = cxt;
try {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
}else{
mediaPlayer = new MediaPlayer();
}
this.fileNames = audioFileNames;
this.fileNames.add("money_receive.mp3");
for (int i=0;i<fileNames.size();i++){
Log.e(TAG, "playAudios fileNames: " + fileNames.get(i));
}
playIndex = -1;
mediaPlayer.setOnCompletionListener(new MyCompleteListener());
playNext();
} catch (Exception e) {
e.printStackTrace();
}
}
private void playNext(){
playIndex++;
if (playIndex < 0 || playIndex >= fileNames.size()){
return;
}
String fileName = fileNames.get(playIndex);
try {
mediaPlayer.reset();
//从Assets目录读取
AssetFileDescriptor afd = mContext.getAssets().openFd(fileName);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
//从网络读取
// Uri uri = Uri.parse(fileName);
// mediaPlayer.setDataSource(cxt, uri);
mediaPlayer.setLooping(false);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(mPreparedListener);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
后来在网上查找到可以使用Google官方提供的Exoplayer进行多段语音播放,使用如下:
public class MyExoPlayer {
private static final String TAG = MyExoPlayer.class.getSimpleName();
private SimpleExoPlayer player;
private static class SingletonHolder {
private static final MyExoPlayer INSTANCE = new MyExoPlayer();
}
private MyExoPlayer() {
}
public static final MyExoPlayer getInstance() {
return MyExoPlayer.SingletonHolder.INSTANCE;
}
public void playAudios(Context context, List<String> fileNames){
if (fileNames == null){
return;
}
if (player == null){
player = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
//0.8f是设置播放的速度,1.0f是设置播放的音调
player.setPlaybackParameters(new PlaybackParameters(0.8f, 1.0f));
}
// userAgent -> audio/mpeg 不能为空
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(context, "audio/mpeg");
//创建一个媒体连接源
ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource();
for (int i=0;i<fileNames.size();i++){
String fileName = fileNames.get(i);
Log.e(TAG, "playAudios fileName: " + fileName);
//创建一个播放数据源,这里数据源是存放在src/main/assets目录下
MediaSource mediaSource = new ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(Uri.parse("file:///android_asset/" + fileName));
concatenatingMediaSource.addMediaSource(mediaSource);
}
player.setPlayWhenReady(true);
player.prepare(concatenatingMediaSource);
}
public void release(){
if (player != null){
player.release();
player = null;
}
}
}
这里需要注意:播放音频的时候,如果音频文件很短,而且前面一段空白,中间是声音,后面一段空白,有可能出现无声音的情况,这时需要对音频文件进行截取处理,提取出有声音的部分,然后进行播放即可