我正在使用列表视图,自定义适配器列表视图包含歌曲并显示在activity(fayrouz)中,我想在此活动中创建一个按钮来停止媒体播放器。 我已经在musicadapter中创建了媒体播放器。
这是我的代码:
public class activity_fayrouz extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
private AudioManager mAudioManager;
private Button stopper;
MediaPlayer mp ;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fayrouz);
stopper = (Button) findViewById(R.id.st) ;
stopper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// mAudioManager = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
final ArrayList<music> musics = new ArrayList<>();
musics.add(new music("shady", "fayrouz", R.raw.shady));
musics.add(new music("ana L habiby", "fayrouz", R.raw.ana_l_habiby));
musics.add(new music("alamony", "fayrouz", R.raw.alamony));
musics.add(new music("habitaak bl saief", "fayrouz", R.raw.habitak_bl_saif));
musics.add(new music("nsam 3alina el hawa", "fayrouz", R.raw.nsam_aalina_el_hawa));
musics.add(new music("e7na w el qamar jeraan", "fayrouz", R.raw.e7na_w_el_qamar_jeran));
musics.add(new music("shady", "fayrouz", R.raw.shady));
musics.add(new music("shady", "fayrouz", R.raw.shady));
musics.add(new music("ana L habiby", "fayrouz", R.raw.ana_l_habiby));
musics.add(new music("alamony", "fayrouz", R.raw.alamony));
musics.add(new music("habitaak bl saief", "fayrouz", R.raw.habitak_bl_saif));
musics.add(new music("nsam 3alina el hawa", "fayrouz", R.raw.nsam_aalina_el_hawa));
musics.add(new music("e7na w el qamar jeraan", "fayrouz", R.raw.e7na_w_el_qamar_jeran));
musicAdapter adapter = new musicAdapter(this, musics);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
我想要此停止按钮来停止在音乐适配器中创建的媒体播放器
音乐适配器:
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final music my_music = (music) getItem(position);
TextView songTitle = (TextView) listItemView.findViewById(R.id.song);
songTitle.setText(my_music.getSong());
TextView artistTitle = (TextView) listItemView.findViewById(R.id.artist);
artistTitle.setText(my_music.getArtist());
mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
Button play = (Button) listItemView.findViewById(R.id.play);
Button pause = (Button) listItemView.findViewById(R.id.pause) ;
Button stop = (Button) listItemView.findViewById(R.id.stop) ;
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
答案 0 :(得分:0)
在音乐适配器类中创建一个停止媒体播放器的公共方法,并在停止按钮上调用该方法onClick;
public void stopMediaPlayer() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
stopper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.stopMediaPlayer();
}
});