禁用视频中的声音

发布于 2024-11-25 09:18:14 字数 111 浏览 1 评论 0原文

您好,我是 Android 开发新手,正在开发游戏。我目前有一个打开的过场动画,它是视频视图,但如果用户从首选项菜单中选择静音音乐,我想静音。问题是我不知道如何在不完全关闭视频的情况下将视频视图中的音乐静音!

Hi I'm new to android development and working on a game. I currently have an opening cutsceen which is a videoview, but I want to mute the the sound, if the user selects mute music from the preference menu. The problem is I don't know how to mute the music in a videoview without actually turning off the video entirely!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-02 09:18:14

AudioManager 并不总是一个好的选择,因为你将所有系统静音...

如果你想访问 VideoView 的 MediaPlayer,你必须调用 MediaPlayer.OnPreparedListener 和 MediaPlayer.OnCompletionListener,然后你可以调用 setVolume(0f, 0f);函数将音量设置为 0。

执行以下操作:

@Override

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_video);

  VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  videoView.setMediaController(mc);
  String _path = "/mnt/sdcard/Movies/video5.mp4";

  videoView.setVideoPath(_path);
  videoView.setOnPreparedListener(PreparedListener);

  videoView.requestFocus();

  //Dont start your video here
  //videoView.start();


}

MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

     @Override
     public void onPrepared(MediaPlayer m) {
         try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m.setVolume(0f, 0f);
                m.setLooping(false);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }    
     }
 };

The AudioManager is not a good option always, because you mute all the system...

if you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call setVolume(0f, 0f); function to set the volume to 0.

Do this:

@Override

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_video);

  VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  videoView.setMediaController(mc);
  String _path = "/mnt/sdcard/Movies/video5.mp4";

  videoView.setVideoPath(_path);
  videoView.setOnPreparedListener(PreparedListener);

  videoView.requestFocus();

  //Dont start your video here
  //videoView.start();


}

MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

     @Override
     public void onPrepared(MediaPlayer m) {
         try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m.setVolume(0f, 0f);
                m.setLooping(false);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }    
     }
 };
乱了心跳 2024-12-02 09:18:14

您能做的最好的事情就是使用 AudioManager 将音乐流静音剪切场景开始并在完成后取消静音:VideoView 不提供独立的静音控件。

The best you can do is use AudioManager to mute the music stream at the beginning of the cut scene and unmute it once it is done: VideoView does not provide independent mute controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文