既然是音乐播放器就自然免不了MediaPlayer了,MediaPlayer原生的方法就有好几个常用的:
MediaPlayer.reset();重置播放器数据
MediaPlayer.setDataSource();设置播放的源文件
MediaPlayer.prepare();正式加载源文件
MediaPlayer.start();
MediaPlayer.pause();
MediaPlayer.seekTo();
MediaPlayer.getCurrentPosition();
MediaPlayer.getDuration();
MediaPlayer.release();
MediaPlayer.setOnCompletionListener();
用上面的方法足以实现播放器的大部分功能了,但是有2个问题。
1. MediaPlayer实例一个APP应该只能有一个,否则一点播放列表中的歌曲就生成一个MediaPlayer放歌,那不岂变成多首歌同时播放了么。所有我们需要一个APP全局实例,做到这一点就要用到Application组件,位于Activity高一层的组件,把Manifest.xml中的Application标签换成我们自己写的Application类。
2. 放歌的时候我们退出APP并不能真正的退出,MediaPlayer应该还要在后台播放,所以MediaPlayer肯定是与Service一起使用的。
3. 在长时间把APP放在后台,一段时间之后,又或者当系统内存紧张的时候会杀死这些后台程序的,所以为了避免Service被杀死,要把Service设为前台服务,提升优先级。
创建Service
- 编写AIDL : IMediaService.aidl
- 自动生成IMediaService.java
- 编写Service类,在OnBind函数返回继承了IMediaService.Stub类的真正服务端
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> ServiceConnection connection=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ServiceConnection()
{
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onServiceDisconnected</span>(ComponentName arg0)
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// TODO Auto-generated method stub</span>
}
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onServiceConnected</span>(ComponentName arg0, IBinder arg1)
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// TODO Auto-generated method stub</span>
server=IMediaService.Stub.asInterface(arg1);
}
};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
把Service设置成前台服务:用的就是Service.startForeground(id,notification);和Service.stopForeground(boolean);
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">public void updateNotification(String musicname,String artist)
{
Notification notification=new Notification()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
//设置Notification的status的内容
notification<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.icon</span>=R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.drawable</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ic</span>_launcher<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
notification<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tickerText</span>=musicname<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
//设置Notification的下拉下来展示的内容
RemoteViews rv=new RemoteViews(getPackageName(), R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.notification</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
rv<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextViewText</span>(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.notification</span>_musicname_tv, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"正在播放: "</span>+musicname)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
rv<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextViewText</span>(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.notification</span>_artist_tv, artist)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
notification<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.contentView</span>=rv<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
//设置按Notification时的跳转
Intent intent=new Intent(getApplicationContext(), MainActivity<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
PendingIntent pendingIntent=PendingIntent<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getActivity</span>(getApplicationContext(), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, intent, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
notification<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.contentIntent</span>=pendingIntent<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
startForeground(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, notification)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
}
public void cancelNotification()
{
stopForeground(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
}</code>