-1

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

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

            }
        });
4

3 回答 3

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

            }
        });
于 2016-06-17T05:17:59.530 回答
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
        }
    }
于 2016-06-17T05:27:55.537 回答
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();
}
于 2016-06-17T05:28:25.197 回答