Android MediaPlayer 不会暂停

Android MediaPlayer won't pause

所以我有一个播放所选文件的按钮。但是我想在再次按下按钮时暂停播放。我检查了一些 Whosebug 帖子和教程,大多数情况下使用的是暂停方法。但是它对我不起作用。音频继续播放。我是不是遗漏了什么或者我的代码有问题?任何帮助表示赞赏。谢谢!

buttonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fileName = getFileSelected();
                MediaPlayer m = new MediaPlayer();
                    if ( !m.isPlaying() && (getFileSelected().endsWith(".3gp") || getFileSelected().endsWith(".wav"))) {
                    try {
                        m.setDataSource(getActivity().getApplicationContext().getExternalFilesDir(null).toString() + File.separator + fileName);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            m.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                            isPlaying = true;
                            buttonPlay.setText("Stop");
                        m.start();
                        Toast.makeText(getActivity().getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
                    }
                    else{
//                            m.stop();
//                            m.release();
//                            m = null;
                            m.pause();
                            isPlaying = false;
                            setButtonLabel(R.id.btnPlay, "Play Selected Recording");
                       }

            }
        });

您的 MediaPlayer 不会 暂停 因为您正在 获取音乐文件、初始化 mediaPlayer、准备它并启动媒体播放器 每次点击按钮!

您应该只初始化一次媒体播放器并在按钮点击侦听器之外,只需在点击按钮时启动或暂停它。

String fileName = getFileSelected();
MediaPlayer m = new MediaPlayer();

buttonPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                if ( !m.isPlaying() && (getFileSelected().endsWith(".3gp") || getFileSelected().endsWith(".wav"))) {
                try {
                    m.setDataSource(getActivity().getApplicationContext().getExternalFilesDir(null).toString() + File.separator + fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        m.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                        isPlaying = true;
                        buttonPlay.setText("Stop");
                    m.start();
                    Toast.makeText(getActivity().getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
                }
                else{
//                            m.stop();
//                            m.release();
//                            m = null;
                            m.pause();
                            isPlaying = false;
                            setButtonLabel(R.id.btnPlay, "Play Selected Recording");
                       }

            }
        });

这个代码肯定work.Try吧。

public AudioManager audioManager = null;
public boolean playing;
Button btnRequestFocus = (Button) findViewById(R.id.btnRequestFocus);

    btnRequestFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            boolean gotFocus = requestAudioFocusForMyApp(MainActivity.this);
            if (gotFocus) {
                //play audio.

            }
        }
    });


    Button btnReleaseFocus = (Button) findViewById(R.id.btnReleaseFocus);
    btnReleaseFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Stop playing audio.
            releaseAudioFocusForMyApp(MainActivity.this);
        }
    });
}

private boolean requestAudioFocusForMyApp(final Context context) {

    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    // Request audio focus for playback
    int result = am.requestAudioFocus(afChangeListener,
            // Use the music stream.
            AudioManager.STREAM_MUSIC,
            // Request permanent focus.
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.d("AudioFocus", "Audio focus received");

        return true;
    } else {
        Log.d("AudioFocus", "Audio focus NOT received");
        return false;
    }
}

void releaseAudioFocusForMyApp(final Context context) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
     tts.stop();

    am.abandonAudioFocus(afChangeListener);
}
AudioManager.OnAudioFocusChangeListener afChangeListener = new     AudioManager.OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)   {
            // Lower the volume
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // Raise it back to normal
        }
    }
if (m.isPlaying()) {
  m.pause();
  isPlaying = false;
  setButtonLabel(R.id.btnPlay, "Play Selected Recording");
} else {
  Toast.makeText(this, "Media Player is not playing", Toast.LENGTH_LONG).show();
}