gpt4 book ai didi

android - MediaPlayer.setOnCompletionListener 未执行

转载 作者:行者123 更新时间:2023-11-30 00:07:03 25 4
gpt4 key购买 nike

public class MainActivity extends AppCompatActivity
implements View.OnClickListener {

private MediaPlayer mediaPlayer;
private ImageButton playBtn,previousBtn,forwardBtn,rewindBtn,nextBtn;
private SeekBar seekBar;
private int[] songsName = {R.raw.song1,R.raw.song2,R.raw.song3,R.raw.song4};
private int songPosition=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

seekBar = (SeekBar) findViewById(R.id.songProgressBar);
seekBar.setClickable(false);
seekBar.setEnabled(false);
playBtn = (ImageButton) findViewById(R.id.play);
playBtn.setOnClickListener((View.OnClickListener)this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

if(mediaPlayer != null && b){
mediaPlayer.seekTo(i);
}

}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

forwardBtn = (ImageButton) findViewById(R.id.forward);
forwardBtn.setOnClickListener((View.OnClickListener)this);

rewindBtn = (ImageButton) findViewById(R.id.rewind);
rewindBtn.setOnClickListener((View.OnClickListener)this);

nextBtn = (ImageButton) findViewById(R.id.next);
nextBtn.setOnClickListener((View.OnClickListener)this);

previousBtn = (ImageButton) findViewById(R.id.previous);
previousBtn.setOnClickListener((View.OnClickListener)this);

subView = (SubtitleView) findViewById(R.id.subs_box);

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mediaplayer) {

Toast.makeText(getApplicationContext(),"Media Player End",Toast.LENGTH_LONG).show();
nextSong();

}
});
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
playSong();
break;
case R.id.forward:
forwardSong(5000);
break;
case R.id.rewind:
rewindSong(5000);
break;
case R.id.next:
if(mediaPlayer !=null)
nextSong();
break;
case R.id.previous:
previousSong();
break;
}
}

public void playSong(){
imageName = (String) playBtn.getTag();
if(imageName.equals("play")) {
playBtn.setTag("stop");
//mediaPlayer = MediaPlayer.create(this, R.raw.chalisa);
Log.d(TAG,""+songPosition);
prepareMediaPlayer(songPosition);
seekBar.setMax(mediaPlayer.getDuration());
seekUpdation();
seekBar.setEnabled(true);
playBtn.setImageResource(R.drawable.stop);
}else{
playBtn.setImageResource(R.drawable.play);
playBtn.setTag("play");
mediaPlayer.pause();
mediaPlayer = null;
seekBar.setProgress(0);
seekBar.setEnabled(false);
subView.setText("");
}
}

public void forwardSong(int time){
if(mediaPlayer != null){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + time);
}

}

public void rewindSong(int time){
if(mediaPlayer != null){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - time);
}
}

public void nextSong(){
songPosition++;
if(songPosition < songsName.length){
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}else{
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
songPosition = 0;
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}
}

public void previousSong(){
songPosition--;
if (songPosition != -1){
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}else{
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
songPosition = 0;
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}



}


public void prepareMediaPlayer(int songPosition){
mediaPlayer = MediaPlayer.create(this, songsName[songPosition]);
mediaPlayer.start();
subView.setPlayer(mediaPlayer);
subView.setSubSource(subTitleName[songPosition], MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);

}

public void seekUpdation() {
if(mediaPlayer != null) {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
myHandler.postDelayed(run, 1000);
}
}

Runnable run = new Runnable() {
@Override public void run()
{
seekUpdation();
}
};

完成后媒体播放器不工作,也无法打印日志。

我的 Intent 是在当前歌曲完成后播放下一首歌曲,但我无法检查歌曲是否完成。

在 logcat 中,它显示以下警告

W/MediaPlayer-JNI(16888):MediaPlayer 最终确定但未发布。

在 logcat 中没有显示任何错误......

可能是什么问题?

MediaPlayer.onCreate 有什么问题吗?

最佳答案

setOnCompletionListener 在分配媒体资源后始终工作。从 OnCreate 方法中删除它并准备好媒体播放器后,将此监听器添加到您的方法 PlaySong() 中。它可能会起作用。

public void playSong(){
imageName = (String) playBtn.getTag();
if(imageName.equals("play")) {
playBtn.setTag("stop");
//mediaPlayer = MediaPlayer.create(this, R.raw.chalisa);
Log.d(TAG,""+songPosition);
prepareMediaPlayer(songPosition);
seekBar.setMax(mediaPlayer.getDuration());
seekUpdation();
seekBar.setEnabled(true);
playBtn.setImageResource(R.drawable.stop);
}else{
playBtn.setImageResource(R.drawable.play);
playBtn.setTag("play");
mediaPlayer.pause();
mediaPlayer = null;
seekBar.setProgress(0);
seekBar.setEnabled(false);
subView.setText("");
}

myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// do what ever you want
}
});
}

还有这个 mediaPlayer = MediaPlayer.create(this, songsName[songPosition]); 只为选定的歌曲添加单次播放后不要再次创建..然后在做下一首歌曲时再次创建。

关于android - MediaPlayer.setOnCompletionListener 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48786527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com