Android MediaPlayer.start not starting

I have yet to find the answer to this question.

I have a local file (R.raw.Bob); and I'm trying to use MediaPlayer to play a file.

Sometimes he plays, sometimes it is not. I have another file that plays seem perfect every time.

My activity stream looks like this: In onCreate, I do the following:

MediaPlayer mBackground = MediaPlayer.create(MainAct.this, R.raw.background);
mBackground.start(); // Works as expected.

Now in another part of the action, I have the following:

MediaPlayer mBob= MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.start();

And nothing happens. I used Log.i (), and execution is done through the appropriate code, but the file does not start.

Why does MediaPlayer sometimes work and sometimes not, and is there a more reliable way to play audio files?

+4
source share
1 answer

Try to get this started:

MediaPlayer mBob = MediaPlayer.create(MainActivity.this, R.raw.Bob);

    mBob.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(final MediaPlayer mp) {
                    mp.start();
        }
    });

and stop it:

mBob.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.stop();
        }
    });
0

Source: https://habr.com/ru/post/1613874/


All Articles