user1179510
user1179510

Reputation: 1023

MediaPlayer audio gets cut of sometimes, but not always

I am playing an audio file like this:

        MediaPlayer player;
        player=MediaPlayer.create(context, R.raw.sound);
        player.setLooping(false);

        player.setOnCompletionListener(new OnCompletionListener() 
        {      
            public void onCompletion(MediaPlayer player)
            {  
                player.release();       
            }            

        }); 
        player.start();

The issue is that sometimes it cuts the audio off in the middle (or the start and plays no more). If I reboot the phone the problem seems to be fixed but occurs again after some time. Could someone tell me what exactly is wrong?

Upvotes: 3

Views: 2504

Answers (1)

Alexis C.
Alexis C.

Reputation: 93872

Call player.prepare() before player.start()

And to have a better overview of how the MediaPlayer works, take a look at this state diagram :

http://developer.android.com/images/mediaplayer_state_diagram.gif

Upvotes: 3

Related Questions