Get media player to play first on right speaker and then on left speaker

时光毁灭记忆、已成空白 提交于 2019-12-08 12:15:56

问题


I would like to play an audio file that starts on the left speaker and then switches to the right speaker.

I have tried doing something like this:

MediaPlayer mp = new MediaPlayer();

// Setup audio file

mp.start(); 

mp.setVolume(1.0F, 0F);

// Delay a second or two (I actually use a Handler and the postDelayed method)

mp.setVolume(0F, 1.0F);

but the sound comes through on both speakers the whole time.

How can I play audio in Android with either the left or right speaker muted (or at reduced volume)?

EDIT:

I got the correct behavior for a while while I was testing my app, but then it returned to what I described above with the exact same code base. Based on this, is there anything else I could check to find out what's going on?


回答1:


One option would be

  1. Start mediaplayer with setVolume(1.0F, 0F);
  2. When you want to switch to other speaker, get current position of media player by using getCurrentPosition() method.
  3. Then stop media player.
  4. Then again start with setVolume(0F,1.0F);
  5. Seek to the positin you got in 2nd step using seekTo() method

Done.

Overhead:This method may cause you some delay




回答2:


It looks like you are doing it correctly according to the Android API http://developer.android.com/reference/android/media/MediaPlayer.html

public void setVolume (float leftVolume, float rightVolume)

Sets the volume on this player. This API is recommended for balancing the output of 
audio streams within an application. Unless you are writing an application to control 
user settings, this API should be used in preference to setStreamVolume(int, int, int) 
which sets the volume of ALL streams of a particular type. Note that the passed volume 
values are raw scalars in range 0.0 to 1.0. UI controls should be scaled logarithmically.

Parameters
leftVolume  left volume scalar
rightVolume right volume scalar

My best advice is to try 0.0F instead of just 0F and then maybe trying to set the volume before you start playing the track then transition while it's playing.



来源:https://stackoverflow.com/questions/22367649/get-media-player-to-play-first-on-right-speaker-and-then-on-left-speaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!