问题描述
在一个屏幕中,我有一个循环背景视频(显示正常的 UI 元素),使用 VideoView 实现,如下所示:
String resPath = "android.resource://" + getPackageName() + "/" + R.raw.bg_video;videoView.setVideoURI(Uri.parse(resPath));//隐藏控件:MediaController 控制器 = new MediaController(getActivity());controller.setVisibility(View.GONE);videoView.setMediaController(控制器);//使用循环:videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@覆盖公共无效 onPrepared(MediaPlayer mp) {mp.setLooping(true);}});videoView.start();
视频效果很好.唯一的问题是,当显示此视图时(或者甚至之前,如果使用例如 ViewPager),来自其他应用程序的音乐播放停止.
我使用的视频本身没有声音.有没有办法在静音模式"下设置 VideoView 或它使用的 MediaPlayer?即,告诉它根本不要播放音频,尤其是不要干扰其他应用的音频.
在 onPrepared()
中尝试了 mp.setVolume(0, 0);
但没有效果.
从 Android 来源复制粘贴 VideoView
并注释掉以下几行:
AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);am.requestAudioFocus(空,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
例如,您可以将新类命名为 MutedVideoView
.
要点在这里:https://gist.github.com/vishna/7e9d3466bced8502fcdd>
In one screen I have a looping background video (upon which normal UI elements are shown), implemented with VideoView like this:
String resPath = "android.resource://" + getPackageName() + "/" + R.raw.bg_video;
videoView.setVideoURI(Uri.parse(resPath));
// Hide controls:
MediaController controller = new MediaController(getActivity());
controller.setVisibility(View.GONE);
videoView.setMediaController(controller);
// Use looping:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
videoView.start();
The video works great. The only problem is that when this view is shown (or even before if using e.g. ViewPager), music playback from other applications stops.
The video I'm using has no sound itself. Is there a way to set the VideoView, or the MediaPlayer it uses, in a "muted mode"? I.e., tell it not to do audio at all, and especially not to mess with other apps' audio.
Tried mp.setVolume(0, 0);
in onPrepared()
but it has no effect.
Copy paste VideoView
from Android sources and comment out these lines:
AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
You could name the new class MutedVideoView
, for example.
gist here: https://gist.github.com/vishna/7e9d3466bced8502fcdd
这篇关于如何防止 VideoView/MediaPlayer 停止其他应用程序的音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!