Java – MediaPlayer only plays music files in Debug mode (Android)

MediaPlayer only plays music files in Debug mode (Android)… here is a solution to the problem.

MediaPlayer only plays music files in Debug mode (Android)

I’ve written an Android app (in eclipse) to play music files like this:

MediaPlayer mediaPlayer = new MediaPlayer();
        try
        {

mediaPlayer.setDataSource(fileName);
            mediaPlayer.prepare();
            mediaPlayer.start();
          mediaPlayer.stop();

}
        catch (Exception e)
        {
            Log.d("Exception---", e.getMessage());
        }

When I run it, it

doesn’t play the file (and doesn’t show any exception either), but when I switch to “Debug mode” and track it line by line, it plays the file. I’m really confused. Can you help me figure out what the problem is?

Thank you.

Solution

Place a log before “mediaPlayer.setDataSource(fileName)” and check if you get a valid filename in Release mode.

Related Problems and Solutions