[Android]让RemoteControlClient显示Music Album

RemoteControlClient可以用来显示远程Music播放控制界面,可以显示Music album.
前提条件: 1. 要有AudioFocus , 2. 要注册MediaButton receiver, 3. 更重要的是要注册RemoteControlClient, 此步要用到2的receiver 4.通过RemoteControlClient来提交Music Metadata,包括Album bitmap.
Metadata提交要用到MetadataEditor, 最后需要apply()
MetadataEditor md = mRemoteControlClient.editMetadata(true);

int playMusic(){

    Log.v(TAG, "mediaPlayer   playMusic");
    //get AudioFocus firstly

    // Request audio focus for playback
      int result = mAudioManager.requestAudioFocus(afChangeListener,
                                       // Use the music stream.
                                       AudioManager.STREAM_MUSIC,
                                       // Request permanent focus.
                                       AudioManager.AUDIOFOCUS_GAIN);

      if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
          //mAudioManager.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
          // Start playback.
          Log.v(TAG, "Ok to get Audio Focus!");
      }
      else{
          Log.v(TAG, "Failed to get Audio Focus, return!");
          return 0;
      }

      //
      Log.v(TAG, "registerMediaButtonEventReceiver before");
      mAudioManager.registerMediaButtonEventReceiver(mbCN);
      Log.v(TAG, "registerMediaButtonEventReceiver after");

      // build the PendingIntent for the remote control client
      Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
      mediaButtonIntent.setComponent(mbCN);
      PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
      // create and register the remote control client
      if(mRemoteControlClient == null){
          mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
      }

      mAudioManager.registerRemoteControlClient(mRemoteControlClient);
      Log.v(TAG, "registerRemoteControlClient after");

      //to play music

      mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.w1374911);
      mMediaPlayer.start(); // no need to call prepare(); create() does that for you

      Log.v(TAG, "mediaPlayer.start");

      //prepare Album
      mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                                                     |RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                                                     |RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                                                     |RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);
      mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

      MetadataEditor  md = mRemoteControlClient.editMetadata(true);

      md.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "HellowWorld.mp3");
      md.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "JackLee");
      md.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, "JackLee");
      md.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "JackLee");
      //md.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, 10000);
      Log.v(TAG, "mediaPlayer   before getBitmap  album");
      Resources res=getResources();
      Drawable drawable = res.getDrawable(R.drawable.album);

      BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;

      Bitmap bitmap=bitmapDrawable.getBitmap();
      md.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, bitmap);
      md.apply();
      Log.v(TAG, "mediaPlayer   Album apply");

      btn_playpause.setText("Stop");
      playstate = 1;
      return 1;
}

你可能感兴趣的:([Android]让RemoteControlClient显示Music Album)