I need to dynamically vary volume of videoView
outside of onPrepared
method. So I have saved a reference to videoView
's media player on which I am using setVolume
method.
private MediaPlayer videoViewMediaPlayer;
videoView onPreparedListener:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoViewMediaPlayer = mediaPlayer;
setVolumeForMediaPlayer(mediaPlayer);
videoView.setBackgroundColor(Color.TRANSPARENT);
mediaPlayer.setLooping(false);
}
});
...
private void setVolumeForMediaPlayer(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
if (muteAudio)
mediaPlayer.setVolume(0, 0);
else
mediaPlayer.setVolume(1.0f, 1.0f);
}
}
Now, whenever I need to adjust volume of mediaPlayer, I am using:
setVolumeForMediaPlayer(videoViewMediaPlayer);
But, following exception is thrown:
java.lang.IllegalStateException
at android.media.MediaPlayer._setVolume(Native Method)
at android.media.MediaPlayer.setVolume(MediaPlayer.java:1604)
at com.blynq.app.fragments.PlayerPane.setVolumeForMediaPlayer(PlayerPane.java:501)
at com.blynq.app.fragments.PlayerPane.changeActiveMedia(PlayerPane.java:408)
at com.blynq.app.asyncs.PanePlayerAsyncTask.onProgressUpdate(PanePlayerAsyncTask.java:130)
at com.blynq.app.asyncs.PanePlayerAsyncTask.onProgressUpdate(PanePlayerAsyncTask.java:22)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
- What is the reason for this exception ?
- If videoView is invisible by the time onPrepared is called, would
setVolume(0, 0)
set videoView to mute ? - How do I set the volume outside of onPrepared(...) method ?