Posts

Showing posts with the label Media Player

Android: MediaPlayer SetVolume Function

Answer : This function is actualy wonderful. Thanks to it you can create a volume scale with any number of steps! Let's assume you want 50 steps: int maxVolume = 50; Then to set setVolume to any value in this range (0-49) you do this: float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume)); yourMediaPlayer.setVolume(log1,log1); //set volume takes two paramater Nice and easy! And DON'T use AudioManager to set volume! It will cause many side effects such as disabling silent mode, which will make your users mad! Following user100858 solution I just post my exact code that works: private final static int MAX_VOLUME = 100; ... ... final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math.log(MAX_VOLUME))); mediaPlayer.setVolume(volume, volume); soundVolume is the volume you would like to set, between 0 and MAX_VOLUME. So between 0 and 100 in this example. For Android MediaPlayer.setVolume , searching the web seems to show 0.0f ...

Android : How To Set MediaPlayer Volume Programmatically?

Answer : Using AudioManager , you can simply control the volume of media players. AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0); also from MediaPlayer (But I didn't try that) setVolume(float leftVolume, float rightVolume) Since: API Level 1 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. UI controls should be scaled logarithmically. Parameters leftVolume left volume scalar rightVolume right volume scalar Hope this help audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); For Volume UP audi...

Android MediaPlayer Stop And Play

Answer : You should use only one mediaplayer object public class PlayaudioActivity extends Activity { private MediaPlayer mp; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.button1); Button b2 = (Button) findViewById(R.id.button2); final TextView t = (TextView) findViewById(R.id.textView1); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopPlaying(); mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far); mp.start(); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void ...