我对媒体播放器有一点问题。目前一切都很完美,除了这一部分。我有recyclerview
用于获取URLs
并使用mediaplayer
进行播放。唯一的问题是当我点击recyclerview
的位置时,它会播放音乐,但是当我点击另一个位置时,它将与当前mediaplayer
重叠,并且不会停止它。如果stop
正在播放,我将其代码添加到release
和mediaplayer
,但它无效。如果有人能就如何解决这个问题给我一些建议。我的代码发布在下面。
主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private DatabaseReference mDatabase;
private TextView btm_music_text;
private ImageView btm_play;
public static String title,body,url;
private static MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btm_music_text = (TextView) findViewById(R.id.main_act_text);
btm_play = (ImageView) findViewById(R.id.main_act_play);
mRecyclerView = (RecyclerView) findViewById(R.id.mainAct_RV);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Database Ref
mDatabase = FirebaseDatabase.getInstance().getReference().child("/episodes");
//Set up Firebase Database Adapter
FirebaseRecyclerAdapter<EpisodeReference, EpisodeViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<EpisodeReference, EpisodeViewHolder>
(EpisodeReference.class, R.layout.episode_layout, EpisodeViewHolder.class, mDatabase)
//Populating View
{
@Override
protected void populateViewHolder(final EpisodeViewHolder viewHolder, final EpisodeReference model, int position) {
viewHolder.setBody(model.getBody());
viewHolder.setTitle(model.getTitle());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
title = model.getTitle();
body = model.getBody();
url = model.getUrl();
Uri mUri = Uri.parse(url);
btm_music_text.setSelected(true);
btm_music_text.setText(title + ": " + body);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getApplicationContext(), mUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {
e.printStackTrace();
}
if (mediaPlayer.isPlaying() && mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_play));
} else {
mediaPlayer.start();
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_pause));
}
button_change();
/*Intent intent = new Intent(getApplicationContext(), MusicPlayerActivity.class);
intent.putExtra("TITLE", model.getTitle());
intent.putExtra("BODY", model.getBody());
intent.putExtra("URL", model.getUrl());
startActivity(intent);*/
}
});
}
};
mRecyclerView.setAdapter(recyclerAdapter);
}
private void button_change(){
btm_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying() && mediaPlayer != null) {
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_play));
mediaPlayer.pause();
} else {
mediaPlayer.start();
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_pause));
}
}
});
}
public static class EpisodeViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView tv1;
TextView tv2;
public EpisodeViewHolder(View view) {
super(view);
mView = view;
tv1 = (TextView) view.findViewById(R.id.tv1);
tv2 = (TextView) view.findViewById(R.id.tv2);
}
public void setBody(String body) {
tv2.setText(body);
}
public void setTitle(String title) {
tv1.setText(title);
}
}
}
答案 0 :(得分:0)
在停止上一个MediaPlayer之前,您正在制作新的MediaPlayer。像这样重新排列你的代码
@Override
protected void populateViewHolder(final EpisodeViewHolder viewHolder, final EpisodeReference model, int position) {
viewHolder.setBody(model.getBody());
viewHolder.setTitle(model.getTitle());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
title = model.getTitle();
body = model.getBody();
url = model.getUrl();
Uri mUri = Uri.parse(url);
btm_music_text.setSelected(true);
btm_music_text.setText(title + ": " + body);
if (mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_play));
} else {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getApplicationContext(), mUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
btm_play.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.selector_pause));
}
button_change();
/*Intent intent = new Intent(getApplicationContext(), MusicPlayerActivity.class);
intent.putExtra("TITLE", model.getTitle());
intent.putExtra("BODY", model.getBody());
intent.putExtra("URL", model.getUrl());
startActivity(intent);*/
}
});
}