Where to startService?
<p>I'm programming a little app to download a song from a url and then play it in a service with a media player. I've got just about everything I need working. However, when I start the song playing, the stop and pause buttons don't work, and if I hit play again, a second copy of the song starts playing. I'm pretty sure I know what's going wrong, I just don't know how to fix it.</p>

<pre><code>public class Player extends IntentService {
    public Player() {
        super("Player");
    }
    MediaPlayer mp;

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        Uri uri = intent.getData();
        mp = MediaPlayer.create(this, uri);

        if(extras != null)
        {
            if(extras.getString("action").equals("play"))
            {
                mp.start();
            }
            else if(extras.getString("action").equals("pause"))
            {
                mp.pause();
            }
            else if(extras.getString("action").equals("stop"))
            {
                if (mp != null) {
                    mp.stop();
                    mp.release();
                }
            }
        }
    }
}
</code></pre>

<p>An example of the code for one of my buttons:</p>

<pre><code>    play.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            intent.putExtra("action", "play");
            startService(intent);
        }});
</code></pre>

<p>I believe each time I hit a button and send my intent, it's creating yet another instance of my media player, so it won't pause or play, since that copy isn't playing, and hitting play just spawns another copy. I've tried to set it up so that the first time and only the first time I hit play creates the player, but that didn't work. I also tried making another action like "create" and sending it when the main activity first downloads the file, but that didn't work either. Both caused it to crash. Perhaps I did them wrong; I'm sorry I don't have those copies of the code or the LogCat. Does anyone have any suggestions of how I can create the player just once? Or pass the file's uri to an onCreate?</p>

<p>Edit: Since posting this I gave another try at changing the play button response. It is now:</p>

<pre><code>protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    if(extras != null)
    {
        if(extras.getString("action").equals("play"))
        {
            if (mp != null) {
                mp.start();
            }
            else {
                Uri uri = intent.getData();
                    mp = MediaPlayer.create(this, uri);                 
                mp.start();
            }
        }
</code></pre>

<p>I've used Log calls to pin down where I'm crashing. It's failing on the .create call with my LogCat saying this:</p>

<p>09-22 22:58:24.566: E/AndroidRuntime(1446): FATAL EXCEPTION: IntentService[Player]
09-22 22:58:24.566: E/AndroidRuntime(1446): java.lang.NullPointerException
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:868)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:854)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.media.MediaPlayer.create(MediaPlayer.java:788)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.media.MediaPlayer.create(MediaPlayer.java:769)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at com.example.mp3player.Player.onHandleIntent(Player.java:36)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.os.Looper.loop(Looper.java:137)
09-22 22:58:24.566: E/AndroidRuntime(1446):     at android.os.HandlerThread.run(HandlerThread.java:60)</p>

<p>Any help at all is appreciated!</p>
