raw为自个建的
清单文件中注册服务
逻辑代码文件:
<span style="font-size:18px;">package com.example.day22_music;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import com.example.day22_music.MusicService.MyBind;
public class MainActivity extends Activity {
MusicService musicService;
MyConn conn=new MyConn();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
switch(v.getId())
{
case R.id.bt1:
if(musicService==null)
{
Intent intent=new Intent(MainActivity.this,MusicService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
else
{
musicService.play();
}
break;
case R.id.bt2:
if(musicService!=null)
{
musicService.pause();
}
break;
case R.id.bt3:
if(musicService!=null)
{
musicService.stop();
musicService=null;
unbindService(conn);
}
break;
}
}
class MyConn implements ServiceConnection
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBind mb= (MyBind) service;
musicService=mb.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
conn=null;
}
}
}
</span>
服务类文件:
<span style="font-size:18px;">package com.example.day22_music;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
public class MusicService extends Service{
MediaPlayer mediaPlayer;//声明操作媒体的对象
int pos=0;//记录播放的位置
@Override
public IBinder onBind(Intent intent) {
play();//绑定时进行播放
return new MyBind();
}
class MyBind extends Binder
{
public MusicService getService()
{
return MusicService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
if(mediaPlayer==null)
{
mediaPlayer=mediaPlayer.create(MusicService.this, R.raw.heavencity);
mediaPlayer.setLooping(false);//设置不需要单曲循环
}
//播放完毕 进行监听回调
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();//释放资源
}
});
}
public void play()
{
if(mediaPlayer!=null&&!mediaPlayer.isPlaying())
{
try
{
if(pos!=0)
{
//根据指定位置进行播放
mediaPlayer.seekTo(pos);
mediaPlayer.start();
}
else
{
//从头播放
mediaPlayer.stop();
mediaPlayer.prepare();//启动之前必须prepare
mediaPlayer.start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
}
}
public void pause()
{
if(mediaPlayer!=null&&mediaPlayer.isPlaying())
{
pos=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
}
public void stop()
{
if(mediaPlayer!=null)
{
mediaPlayer.stop();
mediaPlayer.reset();
}
}
}
</span>
布局文件:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:background="#00ff00"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音乐"
android:onClick="click"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停音乐"
android:onClick="click"/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止音乐"
android:onClick="click"/>
</LinearLayout>
</LinearLayout></span>