Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> android bind到music app service

android bind到music app service

編輯:初級開發

or an Android app I’m working on I need to find out what song, if any, is playing in the background on the device. Turns out there’s no documented way to do this on Android - but it also turns out that using an open source platform is great. After a few hours of poking around I had a working proof of concept, and I’m sure someone more familiar with android would’ve figured it out much faster.

IMPORTANT: I’m using an undocumented interface. While this works on android 1.5R3 and T-Mobile G1 (HTC Magic), and likely on other versions/devices, there is no guarantee it’ll keep working in future releases - use at your own risk.

The default way to play music on the G1 is the Music app. Since it can play music in the background, it seemed likely there is some way to interact with the service it uses. This of course doesn’t help if the user is using some other music player, but it should be good enough.

First step was to figure out exactly what service that is - I used something like the following code to find out what services are running while I’m playing music:

  1. ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
     

  2.  
  3. List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);
     

  4.  
  5. for (int i=0; i<rs.size(); i++) {
     
  6.         ActivityManager.RunningServiceInfo rsi = rs.get(i);
     
  7.         Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName());
     
  8. }
復制代碼

This prints up to 50 running services. The interesting line in the output is

  1. process com.android.music with component com.android.music.MediaPlaybackService
復制代碼

OK, now to figure out how to interact with MediaPlaybackService. It turns out to be surprisingly simple.

First, get a copy of IMediaPlaybackService.aidl from the source code of the Music app, and include it in your android app. The contents of this file are the methods we have Access to in MediaPlaybackService. Of course, if the Music app on the device changes, and our copy of the .aidl file is inaccurate, we have a problem - hence the warning above.

Next, implement a ServiceConnection we can use to connect to MediaPlaybackService.

  1. private class MediaPlayerServiceConnection implements ServiceConnection {
     

  2.  
  3.         public IMediaPlaybackService mService;
     

  4.  
  5.         public void onServiceConnected(ComponentName name, IBinder service) {
     
  6.                 Log.i("MediaPlayerServiceConnection", "Connected! Name: " + name.getClassName());
     

  7.  
  8.                 // This is the important line
     
  9.                 mService = IMediaPlaybackService.Stub.asInterface(service);
     

  10.  
  11.                 // If all went well, now we can use the interface
     
  12.                 try {
     
  13.                         Log.i("MediaPlayerServiceConnection", "Playing track: " + mService.getTrackName());
     
  14.                         Log.i("MediaPlayerServiceConnection", "By artist: " + mService.getArtistName());
     
  15.                         if (mService.isPlaying()) {
     
  16.                                 Log.i("MediaPlayerServiceConnection", "Music player is playing.");
     
  17.                         } else {
     
  18.                                 Log.i("MediaPlayerServiceConnection", "Music player is not playing.");
     
  19.                         }
     
  20.                 } catch (Exception e) {
     
  21.                     e.printStackTrace();
     
  22.                     throw new RuntimeException(e);
     
  23.                 }
     
  24.         }
     

  25.  
  26.         public void onServiceDisconnected(ComponentName name) {
     
  27.                 Log.i("MediaPlayerServiceConnection", "Disconnected!");
     
  28.         }
     
  29. }
復制代碼

Finally, bind the service:

  1. Intent i = new Intent();
     
  2. i.setClassName("com.android.music", "com.android.music.MediaPlaybackService");
     
  3. ServiceConnection conn = new MediaPlayerServiceConnection();
     
  4. this.bindService(i, conn, 0);
復制代碼

That’s it! From here on it should be possible to use the MediaPlaybackService like any other service。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved