我需要实现一个切换功能来改变扬声器和耳机
我使用此代码在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();
答案 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();