博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Android]让RemoteControlClient显示Music Album
阅读量:6272 次
发布时间:2019-06-22

本文共 3366 字,大约阅读时间需要 11 分钟。

hot3.png

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;}

转载于:https://my.oschina.net/u/996206/blog/138776

你可能感兴趣的文章
ANT task之Junit、JunitReport
查看>>
selenium的那些事--运行报错
查看>>
谋求职业发展,是“走”还是“留”
查看>>
SpreadJS 在 Angular2 中支持绑定哪些属性?
查看>>
Lucene 定义
查看>>
硅谷返乡大潮,AI海归大调查:薪酬、发展、故乡情
查看>>
GNU make manual 翻译(八十二)
查看>>
算法学习之顺序结构
查看>>
百度2013年校园招聘题
查看>>
第3章 敏捷项目管理概述
查看>>
设计模式系列-装饰模式
查看>>
Oozie的作用
查看>>
Android -- TabHost、Fragment、状态保存、通信
查看>>
TFS二次开发系列:四、TFS二次开发WorkItem添加和修改、保存
查看>>
一次向svn中增加所有新增文件 svn add all new files【转】
查看>>
云计算-从基础到应用架构系列-云计算的概念
查看>>
How to convert smartform output into pdf?
查看>>
SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
查看>>
粒子滤波概述
查看>>
FlyCapture2 Qt5 MinGW Configuration
查看>>