日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓锁屏音乐控件开发

發布時間:2024/1/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓锁屏音乐控件开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ? ? ?首先我個人先吐槽一下,這是一個吃力不討好的活。為什么?因為現在安卓手機的各種定制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
  • mClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING)??
  • 并且必須獲取當前的stream focus

    [java]? view plain copy
  • am.requestAudioFocus(new?OnAudioFocusChangeListener()?{??
  • ??????
  • ????@Override??
  • ????public?void?onAudioFocusChange(int?focusChange)?{??
  • ????????System.out.println("focusChange?=?"?+?focusChange);??
  • ????}??
  • },?AudioManager.STREAM_MUSIC,?AudioManager.AUDIOFOCUS_GAIN);??

  • 這樣,在鎖屏狀態系統界面就能出現了,默認只有一個播放暫停按鈕。要想顯示額外的信息,使用editMetadata(boolean startEmpty)去put,如果想多顯示幾個控制按鈕,如下:

    [java]? view plain copy
  • int?flags?=?RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS??
  • ????????|?RemoteControlClient.FLAG_KEY_MEDIA_NEXT??
  • ????????|?RemoteControlClient.FLAG_KEY_MEDIA_PLAY??
  • ????????|?RemoteControlClient.FLAG_KEY_MEDIA_PAUSE??
  • ????????|?RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE??
  • ????????|?RemoteControlClient.FLAG_KEY_MEDIA_STOP;??
  • mClient.setTransportControlFlags(flags);??

  • 所有按鍵的響應事件都在注冊的receiver中。

    總結

    以上是生活随笔為你收集整理的安卓锁屏音乐控件开发的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。