Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

android - How to prevent VideoView/MediaPlayer from stopping other apps' audio?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...