1
votes

I have 4 Videoview which is created dynamically. I want to set a custom volume for only one video which is coming from the server, the remaining three videos will be mute.

I want to set a custom volume for particular videos I tried below code it's not working

 vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                {
                    public void onPrepared(MediaPlayer mp)
                    {
                     //mp.setVolume(100f, 100f);
                     //mp.setLooping(true);
                     vid1.enableSound(20,mp); //here i will set music sound dynamically
                     vid1.start();
                     playingvideo1 = true;
                    }
                });
                //startTimeForContent = dateFormatForContent.format(new Date());
                vid1.setOnErrorListener(mOnErrorListener2);
                playBackfunction1();
            }

public void enableSound(int sound, MediaPlayer mp){
        Float f = Float.valueOf(sound);
        Log.e("checkingsounds","&&&&&   "+f);
        mp.setVolume(f,f);
        mp.setLooping(true);
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND);
    }

When I give volume 0 it is working...but when change 10,20,30 video is playing full sound....

I already research below:

sound volume not working on Android

Using SeekBar to Control Volume in android?

How to increase and decrease volume in android

1

1 Answers

0
votes

After researching some hours I got a solution

You need to set Volume 0 to 15

vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                {
                    public void onPrepared(MediaPlayer mp)
                    {
                     //mp.setVolume(100f, 100f);
                     //mp.setLooping(true);

                     vid1.enableSound(10,mp);  //set Volume 0 to 15.

                     vid1.start();
                     playingvideo1 = true;
                    }
                });
                //startTimeForContent = dateFormatForContent.format(new Date());
                vid1.setOnErrorListener(mOnErrorListener2);
                playBackfunction1();
            }

 public void enableSound(int sound, MediaPlayer mp){
        Float f = Float.valueOf(sound);
        Log.e("checkingsounds","&&&&&   "+f);
        mp.setVolume(f,f);
        mp.setLooping(true);
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //Max Volume 15
        audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);  //this will return current volume.

        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND);   //here you can set custom volume.
    }