同时播放2个MediaPlayers一起只在Android中播放一个MediaPlayer?

时间:2015-05-25 05:52:38

标签: android android-mediaplayer multiple-instances

在我的应用中,我正在玩"Voice audio Mediaplayer""Background music MediaPlayer",它正在开发Android lollipop 5.0以下版本。但是,当我查看Lollipop and Android One os时,它并没有同时播放2个媒体播放器。只有"背景媒体播放器"正在玩。但是当我登记Debug mode语音媒体播放器正在播放时。

这是我初始化媒体播放器的代码

在onCreate()中:

audioManger = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManger.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManger = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

// Intializing Voice audio Mediaplayer

voiceMediaPlayer = new MediaPlayer();
voiceMediaPlayer.setDataSource(bagroundMusicAudioFileName);
voiceMediaPlayer.setLooping(true);
voiceMediaPlayer.prepare();  

// Intializing Background music Mediaplayer

bgMusicMediaPlayer = new MediaPlayer();
bgMusicMediaPlayer.setDataSource(bagroundMusicAudioFileName);
bgMusicMediaPlayer.setLooping(true);
bgMusicMediaPlayer.prepare();

播放按钮onClickListener():

// Starting Voice and Background MediaPlayers 

if(voiceMediaplayer == null) {
   voiceMediaPlayer = new MediaPlayer();
   voiceMediaPlayer.setDataSource(bagroundMusicAudioFileName);
   voiceMediaPlayer.setLooping(true);
   voiceMediaPlayer.prepare();   
}
voiceMediaPlayer.start();

if(bgMusicMediaPlayer == null) {
   bgMusicMediaPlayer = new MediaPlayer();
   bgMusicMediaPlayer.setDataSource(bagroundMusicAudioFileName);
   bgMusicMediaPlayer.setLooping(true);
   bgMusicMediaPlayer.prepare();     
}
bgMusicMediaPlayer.start();

暂停按钮onClickListener:

//停止语音&暂停按钮上的背景媒体播放器单击

if (voiceMediaPlayer != null && voiceMediaPlayer.isPlaying()) {
    voiceMediaPlayer.reset();
    voiceMediaPlayer.release();
    voiceMediaPlayer = null;
}
if (bgMusicMediaPlayer != null && bgMusicMediaPlayer.isPlaying()) {
    bgMusicMediaPlayer.reset();
    bgMusicMediaPlayer.release();
    bgMusicMediaPlayer = null;
}

当我同时播放两个meidaplayers时,我只在Moto-g(Lollipop os)设备上听到背景音乐,但此代码为working on below < 5.0(Lollipop) versions。我的移动版本是4.4.4 kitkat,它正在我的设备上运行。

我的代码有什么问题吗?如何在Lollipop中一次玩2个MediaPlayers?

1 个答案:

答案 0 :(得分:1)

最后得到Playing multiple songs with MediaPlayer at the same time: only one is really playing

的解决方案

谢谢你们,你们节省了我的时间。