Android MediaPlayer不会暂停

时间:2016-06-17 05:03:38

标签: android audio media-player android-mediaplayer pause

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

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

            }
        });

3 个答案:

答案 0 :(得分:2)

您的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");
                       }

            }
        });

答案 1 :(得分:1)

这段代码肯定有用。试试吧。

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
        }
    }

答案 2 :(得分:0)

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