如何在音频播放期间从扬声器和耳机切换

时间:2017-09-06 10:54:49

标签: android android-mediaplayer

我需要实现一个切换功能来改变扬声器和耳机

我使用此代码在earpiace中播放

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, Uri.parse("http://***.mp3"));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.prepare();
mediaPlayer.start();

如果使用AudioManager.STREAM_MUSIC代替AudioManager.STREAM_VOICE_CALL与扬声器配合使用。但如果我切换rintime没有变化。这是我的代码

int currentPosition = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.seekTo(currentPosition);
mediaPlayer.start();

1 个答案:

答案 0 :(得分:2)

尝试这样做:

int currentPosition = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
// The next two lines are added to your original code
mediaPlayer.reset();
mediaPlayer.setDataSource(this, Uri.parse("http://***.mp3"));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.seekTo(currentPosition);
mediaPlayer.start();