停止Mediaplayer后Android无法再次播放

时间:2019-03-02 06:03:09

标签: android android-mediaplayer

在我的简单播放器中,我具有播放,停止按钮以及播放和暂停媒体播放器的功能,现在,在单击“停止并再次播放”后,媒体播放器无法正常工作,我不确定要解决什么问题< / p>

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...

    playMonthLesson();

    ...
}

@SuppressLint("DefaultLocale")
public void playMonthLesson() {
    try {
        mediaPlayer.reset();
        mediaPlayer.setDataSource(CoreApplication.MEDIAFOLDER + "/" + lesson.getFilename());
        mediaPlayer.prepare();
        mediaPlayer.start();
        lesson_play.setImageResource(R.drawable.ic_pause);
        int totalDuration = mediaPlayer.getDuration();

        // set Progress bar values
        lesson_progress_bar.setProgress(curretLessonProgress);
        lesson_progress_bar.setMax(100);

        // Updating progress bar
        updateProgressBar();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@OnClick(R.id.lesson_play)
public void lesson_play(View view) {
    if (mediaPlayer == null) {
        playMonthLesson();
    } else if (mediaPlayer.isPlaying()) {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
            // Changing button image to play button
            lesson_play.setImageResource(R.drawable.ic_play);
        }
    } else {
        // Resume song
        if (mediaPlayer != null) {
            mediaPlayer.start();
            // Changing button image to pause button
            lesson_play.setImageResource(R.drawable.ic_pause);
        }
    }
}

@OnClick(R.id.lesson_stop)
public void setLesson_stop(View view) {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        lesson_play.setImageResource(R.drawable.ic_play);
        lesson_progress_bar.setProgress(0);
    }
}

2 个答案:

答案 0 :(得分:1)

根据您可以在Android API guide中查看的MediaPlayer生命周期,我认为您必须调用reset()而不是stop(),然后进行准备再次使用媒体播放器(仅使用一个)从头开始播放声音。还请考虑声音可能已经结束。因此,我还建议实现setOnCompletionListener()以确保如果您再次尝试播放声音,声音不会失败。

enter image description here

答案 1 :(得分:0)

问题在于,当您停止播放器并再次单击播放时,由于播放器不为空,因此您的呼叫将转到mediaplayer.play()

您将必须在stop方法上将mediaPlayer设置为空。现在,一旦您停止mediaplayer并再次单击播放,它将调用playMonthLesson();

@OnClick(R.id.lesson_stop)
public void setLesson_stop(View view) {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer = null;
        lesson_play.setImageResource(R.drawable.ic_play);
        lesson_progress_bar.setProgress(0);
    }
}

也更改此代码,

@SuppressLint("DefaultLocale")
public void playMonthLesson() {
    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(CoreApplication.MEDIAFOLDER + "/" + lesson.getFilename());
        mediaPlayer.setOnPreparedListener(new OnPreparedListener(){

        @Override
            public void onPrepared(MediaPlayer mp) {
               mp.start();
               lesson_play.setImageResource(R.drawable.ic_pause);
               int totalDuration = mediaPlayer.getDuration();
            }
        });
        mediaPlayer.prepareAsync();  
        // set Progress bar values
        lesson_progress_bar.setProgress(curretLessonProgress);
        lesson_progress_bar.setMax(100);

        // Updating progress bar
        updateProgressBar();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}