23 服务音乐的启动Demo4

时间:2024-11-07 21:06:20

23 服务音乐的启动Demo4

注意如果音乐服务和Activity在一个应用中那么将不会因为绑定的Activity销毁而关闭音乐

MainActivity.java

package com.qf.day23_service_demo4;

import com.qf.day23_service_demo4.MyMusicService.MyBinder;

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; public class MainActivity extends Activity { private MyMusicService musicService;
private MyServiceConn conn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); conn = new MyServiceConn();
} public void MyClick(View v){
switch (v.getId()) {
case R.id.btn_play://播放
if(musicService!=null){
musicService.playMusic();
}else{
Intent intent = new Intent(MainActivity.this, MyMusicService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} break;
case R.id.btn_pause://暂停
if(musicService!=null){
musicService.pauseMusic();
}
break;
case R.id.btn_stop://停止
if(musicService!=null){
musicService.stopMusic();
musicService =null;
unbindService(conn);
}
break; default:
break;
}
} public class MyServiceConn implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder myBinder = (MyBinder) service;
musicService = myBinder.getMyMusicService();
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
conn = null;
} }
}

MyMusicService.java

package com.qf.day23_service_demo4;

import java.io.IOException;

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 MyMusicService extends Service{ private MediaPlayer mediaPlayer;//媒体操作对象 private int pos=0;//每次暂停的位置 @Override
public IBinder onBind(Intent intent) {
playMusic();//绑定时 开始播放音乐
return new MyBinder();
} //传递Service对象
public class MyBinder extends Binder{
public MyMusicService getMyMusicService(){
return MyMusicService.this;
}
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
if(mediaPlayer == null){
//初始化媒体对象 将资源文件设置给mediaPlayer
mediaPlayer = MediaPlayer.create(MyMusicService.this, R.raw.heavencity);
}
//播放完毕的监听
mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();//是放资源
}
});
} //播放
public void playMusic(){
if(mediaPlayer!=null&&!mediaPlayer.isPlaying()){
try {
if(pos!=0){//从暂停到开始
mediaPlayer.seekTo(pos);//播放位置 移动到 暂停位置
mediaPlayer.start();
}else{//开始
mediaPlayer.stop();//暂停
mediaPlayer.prepare();//进行准备 必须写的
mediaPlayer.start();
} } catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } //暂停
public void pauseMusic(){ if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
pos = mediaPlayer.getCurrentPosition();//获取播放的位置
mediaPlayer.pause();
} } //停止
public void stopMusic(){ if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.reset();//资源重置
} } @Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
} }

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.day23_service_demo4"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.day23_service_demo4.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name=".MyMusicService"></service>
</application> </manifest>

相关文章