android系统标准音乐接口

在自己写完音乐软件之后,可以实现安卓标准播放接口,把控制权交给第三方软件使用.

实现思路要分版本,一种是在安卓5.0以下,基于RemoteControlClient实现,另一种是在5.0以上,基于MediaSession实现.

1 . 基于RemoteControlClient实现.


@SuppressWarnings("deprecation")
public class RemoteControlResponse {

	private final static String TAG = "MediaSessonResponse";
	private static RemoteControlResponse remoteControlResponse = null;
	private static RemoteControlClient remoteClient = null;
	public static RemoteControlResponse getInstance() {
		if (remoteControlResponse == null) {
			remoteControlResponse = new RemoteControlResponse();
		}
		return remoteControlResponse;
	}
	
	public void init(){
		if (remoteClient==null) {
			 AudioManager myAudioManager = (AudioManager) App.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
			 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
			 PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(App.getInstance().getApplicationContext(), 0, mediaButtonIntent, 0);
			 remoteClient = new RemoteControlClient(mediaPendingIntent);
			 myAudioManager.registerRemoteControlClient(remoteClient);
		}
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_PLAYCONTROL, playControlObserver);
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_APP, appObserver);
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_LYRICS, mLyricsObserver);
		
	}
	
	private void release(){
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_PLAYCONTROL, playControlObserver);
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_APP, appObserver);
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_LYRICS, mLyricsObserver);
	}
	
	private void setPlayState(int state){
		remoteClient.setPlaybackState(state);
	}
	
	@SuppressLint("InlinedApi")
	private void setMusicData(){
		
		RemoteControlClient.MetadataEditor ed = remoteClient.editMetadata(true);
		Music music = ModMgr.getPlayControl().getNowPlayingMusic();
		if (music==null) {
			return;
		}
		ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE,music.name);
		ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,music.artist);
		ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM,music.album);
		ed.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, music.duration);
		Bitmap b = ModMgr.getLyricsMgr().getHeadPic();
		if (b != null) {
			Bitmap.Config config = b.getConfig();
			if (config == null) {
				config = Bitmap.Config.ARGB_8888;
			}
			b = b.copy(config, false);
			ed.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, b);
		}
		ed.apply();
		
	}
	
	//设置音乐播放状态
	private PlayControlObserver playControlObserver = new PlayControlObserver(){
		@Override
		public void IPlayControlObserver_RealPlay(Music music) {
			setPlayState((int)RemoteControlClient.PLAYSTATE_PLAYING);
		}

		@Override
		public void IPlayControlObserver_Pause() {
			setPlayState((int)RemoteControlClient.PLAYSTATE_PAUSED);
		}
		
		@Override
		public void IPlayControlObserver_Continue() {
			setPlayState((int)RemoteControlClient.PLAYSTATE_PLAYING);
		}
		@Override
		public void IPlayControlObserver_PlayStop(boolean end) {
			setPlayState((int)RemoteControlClient.PLAYSTATE_STOPPED );
		}
	};
	
	//程序退出的时候释放资
	private AppObserver appObserver = new AppObserver() {
		@Override
		public void IAppObserver_PrepareExitApp() {
			release();
		}
	};
	
	//在播放歌曲的时候设置音乐资源到系统
	private ILyricsObserver mLyricsObserver = new ILyricsObserver(){

		@Override
		public void ILyricObserver_Lyrics(DownloadStatus status, ILyrics lyrics, ILyrics extLyrics, boolean isManually) {
			
		}

		@Override
		public void ILyricObserver_SearchList(DownloadStatus status, List<LyricsListItem> lyricsList) {
			
		}

		@Override
		public void ILyricObserver_HeadPic(DownloadStatus status, Bitmap bitmap) {
			if (!DownloadStatus.BEGIN.equals(status)) {
				setMusicData();
			}
			
		}

		@Override
		public void ILyricObserver_BackgroundPic(DownloadStatus status, Bitmap bitmap, boolean isManually) {
			
		}

		@Override
		public void ILyricObserver_AutoDownloadFinished(Music m) {
			
		}

		@Override
		public void ILyricObserver_AutoDownloadNotify(int alreadyDownloadCout, int sumDownloadCount) {
			
		}	
	};
}

2 .使用MediaSession实现

public class MediaSessonResponse {
	
	private final static String TAG = "MediaSessonResponse";
	private static MediaSessonResponse mediaSessonResponse = null;
	private static MediaSession mediaSession = null;
	public static MediaSessonResponse getInstance() {
		if (mediaSessonResponse == null) {
			mediaSessonResponse = new MediaSessonResponse();
		}
		return mediaSessonResponse;
	}

	public void init(){
		if (mediaSession==null) {
			mediaSession = new MediaSession(App.getInstance().getApplicationContext(), TAG);
			mediaSession.setActive(true);
			Intent openintent = new Intent(App.getInstance().getApplicationContext(), HeadsetControlReceiver.class);
			PendingIntent pi = PendingIntent.getBroadcast(App.getInstance().getApplicationContext(), 0, openintent, PendingIntent.FLAG_CANCEL_CURRENT);
			mediaSession.setMediaButtonReceiver(pi);
			
		}
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_PLAYCONTROL, playControlObserver);
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_APP, appObserver);
		MessageManager.getInstance().attachMessage(MessageID.OBSERVER_LYRICS, mLyricsObserver);
	}
	private void release(){
		mediaSession.release();
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_PLAYCONTROL, playControlObserver);
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_APP, appObserver);
		MessageManager.getInstance().detachMessage(MessageID.OBSERVER_LYRICS, mLyricsObserver);
	}
	
	private Callback callback = new Callback() {
		@Override
		public void onPlay() {
			ModMgr.getPlayControl().continuePlay();
        }
		@Override
		public void onPause() {
			ModMgr.getPlayControl().pause();
		}
		@Override
		public void onSkipToNext() {
			ModMgr.getPlayControl().playNext();
		}
		@Override
		public void onSkipToPrevious() {
			ModMgr.getPlayControl().playPre();
		}
	};
	
	
	private void setMusicData(){
		Builder builder = new Builder();
		Music music = ModMgr.getPlayControl().getNowPlayingMusic();
		if (music==null) {
			return;
		}
		builder.putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON, ModMgr.getLyricsMgr().getHeadPic());
		builder.putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, music.artist);
		builder.putString(MediaMetadata.METADATA_KEY_TITLE, music.name);
		MediaMetadata mediaMetadata = builder.build();
		mediaSession.setMetadata(mediaMetadata);
	}
	
	private void setPlayState(int state){
		PlaybackState.Builder builder = new PlaybackState.Builder();
		builder.setState(state, 0, 0);
		PlaybackState playbackState = builder.build();
		mediaSession.setPlaybackState(playbackState);
	}
	
	
	private PlayControlObserver playControlObserver = new PlayControlObserver(){
		@Override
		public void IPlayControlObserver_RealPlay(Music music) {
			setPlayState((int)PlaybackState.STATE_PLAYING);
		}

		@Override
		public void IPlayControlObserver_Pause() {
			setPlayState((int)PlaybackState.STATE_PAUSED);
		}
		
		@Override
		public void IPlayControlObserver_Continue() {
			setPlayState((int)PlaybackState.STATE_PLAYING);
		}
		@Override
		public void IPlayControlObserver_PlayStop(boolean end) {
			setPlayState((int)PlaybackState.STATE_STOPPED);
		}
	};

	private AppObserver appObserver = new AppObserver() {
		@Override
		public void IAppObserver_PrepareExitApp() {
			release();
		}
	};
	
	private ILyricsObserver mLyricsObserver = new ILyricsObserver(){

		@Override
		public void ILyricObserver_Lyrics(DownloadStatus status, ILyrics lyrics, ILyrics extLyrics, boolean isManually) {
			
		}

		@Override
		public void ILyricObserver_SearchList(DownloadStatus status, List<LyricsListItem> lyricsList) {
			
		}

		@Override
		public void ILyricObserver_HeadPic(DownloadStatus status, Bitmap bitmap) {
			if (!DownloadStatus.BEGIN.equals(status)) {
				setMusicData();
			}
			
		}

		@Override
		public void ILyricObserver_BackgroundPic(DownloadStatus status, Bitmap bitmap, boolean isManually) {
			
		}

		@Override
		public void ILyricObserver_AutoDownloadFinished(Music m) {
			
		}

		@Override
		public void ILyricObserver_AutoDownloadNotify(int alreadyDownloadCout, int sumDownloadCount) {
			
		}
		
	};
};