MediaPlayer.setOnCompletionListener未执行

时间:2018-02-14 11:49:41

标签: android android-mediaplayer

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();
        }
    };

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

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

在logcat中,它显示以下警告

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

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

可能是什么问题?

MediaPlayer.onCreate有什么问题吗?

1 个答案:

答案 0 :(得分:0)

分配媒体资源后,

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]);仅为播放后的所选歌曲添加单次时间不再创建.. 然后在你做Nextsong的时候再创造。