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

时间:2018-08-02 07:54:09

标签: android android-videoview android-audiomanager

我有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

1 个答案:

答案 0 :(得分:0)

研究了几个小时后,我找到了解决方法

您需要将音量0设置为15

GGGGG

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