title: Control Android Media Player
body: <p>I'm looking for a way to control the default Android media player from my own service.  I'm <strong>NOT</strong> interested in playing media from my service or activity; I want to control the existing application, which  consists of an activity (<code>MediaPlaybackActivity.java</code>) and more importantly a service (<code>MediaPlaybackService.java</code>) located in <code>packages/apps/Music/src/com/android/music/</code>.  Ideally, I would like a solution that is independent of the version of Android.</p>
  
  <p>I have figured out how to do pause/play/stop/next/previous operations using Intents.  I can intercept track change events using a broadcast receiver.  I can also get a list of playlists and the contents of each playlist.  <strong>What I would like to be able to do is instruct the <code>MediaPlaybackService</code> to play a specific file/song.</strong>  Again, I don't want to play this song in my application; I want the Android default media player to play it.</p>
  
  <p>I have tried two approaches so far:</p>
  
  <ol>
  <li><p>I imported the IMediaPlaybackService.aidl file from <code>packages/apps/Music/src/com/android/music</code> into my own application, and used this to bind to the <code>MediaPlaybackService</code>.  On Froyo, this works great; I can pass a path to the openFile method, and the service will play the file.  However, on Gingerbread, I get an error: <code>Permission Denial: Accessing service ComponentInfo{com.google.android.music/com.android.music.MediaPlaybackService} from
  pid=17721, uid=10058 requires null</code>.  Finding a workaround to this error would be a good temporary solution, but it's not future-proof.  This service may well change again in future versions of Android.</p></li>
  <li><p>Starting the media player via an ACTION_VIEW Intent.  This also works; however, as expected, the media player UI is brought to the front, which is not ideal.</p>
  
  <blockquote>
    <p>Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(someUri, "audio/mp3");
    startActivity(intent);</p>
  </blockquote></li>
  </ol>
  
  <p>Is there any other way to accomplish this? Is there an Intent I missed that would instruct the media player to play a specific song?  Or alternatively, is there away to start an activity in the background, or to start an activity and immediately switch back to the previous one?</p>