Home » Android » android – How to prevent VideoView/MediaPlayer from stopping other apps' audio?

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

Posted by: admin June 15, 2020 Leave a comment


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;

// Hide controls:
MediaController controller = new MediaController(getActivity());

// Use looping:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {


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.

How to&Answers:

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


The accepted solution does not guarantee compatibility across all Android versions and is a dirty hack more than a true solution. I’ve tried all forms of hacks to get this working, yet none have worked to my satisfaction.

I have come up with a much better solution though – switch from a VideoView to a TextureView and load it with a MediaPlayer. There is no difference from the user’s perspective, just no more audio stoppage.

Here’s my use case for playing an MP4 looping:

private TextureView _introVideoTextureView;
private MediaPlayer _introMediaPlayer;


public void onCreate(...) {
    _introVideoTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
           try {

               _introMediaPlayer = MediaPlayer.create(SignInActivity.this, R.raw.intro_video);
               _introMediaPlayer.setSurface(new Surface(surfaceTexture));
               _introMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    public void onPrepared(MediaPlayer mediaPlayer) {

            } catch (Exception e) {
                System.err.println("Error playing intro video: " + e.getMessage());

        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {}

        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
            return false;

        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {}

public void onDestroy() {


private void destoryIntroVideo() {
    if (_introMediaPlayer != null) {
        _introMediaPlayer = null;


This worked for me:


(before .setVideUri() call)