安卓锁屏音乐控件开发

时间:2022-09-03 00:27:09

             首先我个人先吐槽一下,这是一个吃力不讨好的活。为什么?因为现在安卓手机的各种定制rom五花八门,各种各样,miui、乐蛙、emui、努比亚。。。我们今天要做的事情就是要在各种系统的锁屏界面,出现一个音乐控件,用来做一些简单的音乐播放的控制。

             做之前,习惯性的看了各大家的做法:酷我、多米等,这些家的做法的思路都一样,都是在自己的应用中监听power锁屏事件,然后加一个“假锁屏”,即每次锁屏再打开的时候,首先显示的是自己音乐的锁屏,滑开以后再跳到系统的锁屏。这样做的好处是,通吃各种系统,弊端也是显而易见:锁屏太多了!!!尼玛,对于普通用户,究竟要滑几次才能解开?

假锁屏的方案弃置后,只能牺牲适配性,而考虑实用性,于是有了本文。本文做的是,针对安卓原生系统开发的音乐控件!因为是建立在原生的keyguard上的,所以一些野路子第三方ROM不支持,也是正常。正文开始:

              基于原生android开发,需要用的一个很重要的类RemoteControlClient,RemoteControlClient是从API 14也就是android 4.0开始出现的类,用于在锁屏状态控制音乐播放。界面是系统提供的。

api doc文档上附了一段注册代码

<pre class="prettyprint" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); line-height: 1.5; font-family: 'courier new', courier, monospace; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background-color: rgb(247, 247, 247);"><span class="typ" style="color: rgb(102, 0, 102);">ComponentName</span><span class="pln" style="color: rgb(0, 0, 0);"> myEventReceiver </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">ComponentName</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">getPackageName</span><span class="pun" style="color: rgb(102, 102, 0);">(),</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">MyRemoteControlEventReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getName</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(102, 0, 102);">AudioManager</span><span class="pln" style="color: rgb(0, 0, 0);"> myAudioManager </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">AudioManager</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> getSystemService</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Context</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">AUDIO_SERVICE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
myAudioManager</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">registerMediaButtonEventReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">myEventReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="com" style="color: rgb(136, 0, 0);">// build the PendingIntent for the remote control client</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln" style="color: rgb(0, 0, 0);"> mediaButtonIntent </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ACTION_MEDIA_BUTTON</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
mediaButtonIntent</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setComponent</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">myEventReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(102, 0, 102);">PendingIntent</span><span class="pln" style="color: rgb(0, 0, 0);"> mediaPendingIntent </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">PendingIntent</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getBroadcast</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">getApplicationContext</span><span class="pun" style="color: rgb(102, 102, 0);">(),</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> mediaButtonIntent</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="com" style="color: rgb(136, 0, 0);">// create and register the remote control client</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(102, 0, 102);">RemoteControlClient</span><span class="pln" style="color: rgb(0, 0, 0);"> myRemoteControlClient </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RemoteControlClient</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">mediaPendingIntent</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
myAudioManager</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">registerRemoteControlClient</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">myRemoteControlClient</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
 
上面的代码开始时注册了耳机按键的事件,接着注册了RemoteControlClient。 

要想弄出RemoteControlClient的界面,上面的代码还不够,还需要把当前音乐状态设置为正在播放

[java]  view plain copy 安卓锁屏音乐控件开发 安卓锁屏音乐控件开发
  1. mClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING)  

并且必须获取当前的stream focus

[java]  view plain copy 安卓锁屏音乐控件开发 安卓锁屏音乐控件开发
  1. am.requestAudioFocus(new OnAudioFocusChangeListener() {  
  2.       
  3.     @Override  
  4.     public void onAudioFocusChange(int focusChange) {  
  5.         System.out.println("focusChange = " + focusChange);  
  6.     }  
  7. }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);  

这样,在锁屏状态系统界面就能出现了,默认只有一个播放暂停按钮。要想显示额外的信息,使用editMetadata(boolean startEmpty)去put,如果想多显示几个控制按钮,如下:

[java]  view plain copy 安卓锁屏音乐控件开发 安卓锁屏音乐控件开发
  1. int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS  
  2.         | RemoteControlClient.FLAG_KEY_MEDIA_NEXT  
  3.         | RemoteControlClient.FLAG_KEY_MEDIA_PLAY  
  4.         | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE  
  5.         | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE  
  6.         | RemoteControlClient.FLAG_KEY_MEDIA_STOP;  
  7. mClient.setTransportControlFlags(flags);  


所有按键的响应事件都在注册的receiver中。