如何以编程方式设置 Videoview 音量?

How to set Videoview Volume Programmatically?

我有 4 个 Videoview 是动态创建的。我只想为来自服务器的一个视频设置自定义音量,其余三个视频将静音。

我想为特定视频设置自定义音量我在下面的代码中尝试过它不起作用

 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);
    }

当我给音量 0 时它工作...但是当更改 10、20、30 视频时播放完整声音....

我已经研究了以下内容:

sound volume not working on Android

Using SeekBar to Control Volume in android?

How to increase and decrease volume in android

经过几个小时的研究,我找到了解决方案

您需要将音量 0 设置为 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.
    }