我在recycleview中有多个音频,之后我暂停音频时,我无法恢复我暂停的位置..总是从头开始播放音频。当我单击播放或暂停时,其他媒体也开始播放播放器还会执行搜索栏
这是点击播放暂停
((HolderAudio) holder).iv_play_audio_dash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AdapterDashBoard.HolderAudio) holder).pb_audio.setVisibility(View.VISIBLE);
if (mediaPlayer.isPlaying()){
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.play_audio));
((AdapterDashBoard.HolderAudio)holder).gif_soundwave.setVisibility(View.GONE);
mediaPlayer.pause();
// mediaPlayer.reset();
pausePosition=mediaPlayer.getCurrentPosition();
Log.e("cureeenntt",pausePosition+"");
((AdapterDashBoard.HolderAudio) holder).pb_audio.setVisibility(View.GONE);
}else {
Log.e("thisss","1");
prepareSong(list.get(position).getAudio(),((AdapterDashBoard.HolderAudio) holder),position);
}
}
});
此准备歌曲方法
private void prepareSong(String audio, final HolderAudio holder, final int position) {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
mediaPlayer.reset();
}
try {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(audio);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp.isPlaying()){
mp.pause();
Log.e("thisss","2");
holder.pb_audio.setVisibility(View.GONE);
holder.iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.pause_btn));
}
playSong(mp,holder,lastPosition,position);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
这是播放歌曲的方法
private void playSong(final MediaPlayer mp, final RecyclerView.ViewHolder holder, final int lastPosition, final int position) {
if (mp.isPlaying()) {
mp.pause();
Log.e("thisss","3");
} else {
Log.e("thisss","4");
mp.start();
((AdapterDashBoard.HolderAudio) holder).pb_audio.setVisibility(View.GONE);
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.pause_btn));
((AdapterDashBoard.HolderAudio) holder).gif_soundwave.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (lastPosition==position){
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.pause_btn));
((AdapterDashBoard.HolderAudio) holder).gif_soundwave.setVisibility(View.VISIBLE);
}else {
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.play_audio));
((AdapterDashBoard.HolderAudio) holder).gif_soundwave.setVisibility(View.GONE);
}
int currentPosition;
int currentpos;
((AdapterDashBoard.HolderAudio) holder).seekBar.setMax((int) mp.getDuration() / 1000);
currentPosition = mp.getCurrentPosition() / 1000;
/*if (pausePosition > 1) {
currentPosition = pausePosition / 1000;
currentpos=pausePosition;
mp.start();
Log.e("thisss","5");
} else {
currentpos=mp.getCurrentPosition();
Log.e("thisss","6");
mp.start();
}*/
if (mp.isPlaying()){
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.pause_btn));
}else {
((AdapterDashBoard.HolderAudio) holder).iv_play_audio_dash.setImageDrawable(getResources().getDrawable(R.drawable.play_audio));
}
((AdapterDashBoard.HolderAudio) holder).seekBar.setProgress(currentPosition);
((AdapterDashBoard.HolderAudio) holder).tv_audioTimer.setText(Utility.convertDuration((long) mp.
getCurrentPosition()));
handler.postDelayed(this, 1000);
}
});
}
}