HelloMoon hangs on playback


#1

When I run HelloMoon, it hangs on the MediaPlayer.create line. I get no exception (the try/catch caught nothing) and the call to create never returns. Eventually, Android tells me that my app has stopped responding (duh!)

package com.bignerdranch.android.hellomoon;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.widget.Toast;

public class AudioPlayer {
	
	private MediaPlayer mPlayer;
	
	public void stop() {
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
		
	}
	
	public void play(Context c){
		stop();
		try {
			mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
		}
		catch (Exception ex) {
			Toast.makeText(c, ex.getMessage(), Toast.LENGTH_LONG).show();
		}
		 
		mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				stop();
			}
		});
		
		mPlayer.start();
	}

}

In Logcat, I see this:


#2

Are you still running into the problem or were you able to resolve it? See if the following code works:

public void play(Context c) {
        stop();

          // Creates the media player if needed or resets the existing media player if one already exists.
            if (mp == null) {
                mp = new MediaPlayer();
            }
            else {
                mp.reset();
            }

        mPlayer = MediaPlayer.create(c, R.raw.one_small_step);

        // stops the MediaPlayer once the song is done
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stop();
            }
        });

        mPlayer.start();

    }

#3

gbritton: Could you pull traces.txt from your device and paste it here? There should be instructions near the ANR in logcat.


#4

I’ve concluded that it must have been an Android bug or a conflict with another app. After a fresh restart all was well.