您的位置:首页 > 其它

使用service的startservice 和bindservice方法实现音乐的播放、暂停、继续

2017-04-05 21:04 766 查看
meidiaPlay使用简单步骤

1、create方法

第一种: 加载本地音频文件 MediaPlayer.create(this, R.raw.test);(我在android项目的res目录下新建raw目录 并放置test.mp3文件)

第二种: 加载网络音频 MediaPlayer.create(this,uri.parse(“http://…..test.mp3“));

第三种 使用create构造方法

MediaPlayer player = MediaPlayer.create();

player.setDataSources(“sdcard/test.mp3”);//指定要加载的音频文件 (实际上在这个方法执行后并未加载音频·文件)

player.prepare();//预加载音频文件 在这个方法执行后 正式加载音频文件。

2、暂停 播放

player.start();//播放 继续播放

player.stop();//停止

player.pause();//暂停播放

3、释放资源

player.release();

startservice 和 bindservice()方法

1、startservice()

 Service会经历 onCreate –> onStart

  stopService的时候直接onDestroy 如果activity没有执行stopservice(),那么service会一直运行在后台

2、bindservice

service只会运行oncreate (值得说明的是service无论是哪种方法开始服务 都只会执行一次oncreate方法,service只被创建一次。)activity退出的时候会执行unbindService() 所以如果是以bindservice开启的service服务 那么其生命周期结束跟activity是一致的。

在下面的例子中使用了onstartService 和bindService实现音乐的播放暂停。

activity代码

package com.workfirst;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_my_service_demo)
public class MyServiceDemo extends AppCompatActivity {

/* 参数设置 */

//用来 异步绑定 sc 传递给service的bindService 然后
private ServiceConnection sc = new ServiceConnection() {
/** 获取服务对象时的操作  返回后台service的IBinder对象*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.Mybinder myBinder = (MyService.Mybinder) service;
if(myBinder.getService().player!=null){
Log.e("onServiceConnected ","if");
}else {
myBinder.getService(). player = MediaPlayer.create(myBinder.getService(), R.raw.test);

}

myBinder.getService().player.start();//开始播放
myBinder.getService().bound = "start";

System.out.println(name);
System.out.println("MyServiceDemo:onServiceConnected");
//执行到这个方法之后 就可以开始调用service方法了
}

/** 无法获取到服务对象时的操作  */
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("MyServiceDemo:onServiceDisconnected");
bound = false;
onDestroy();
}
};
private Intent intent;
@ViewInject(R.id.start_service)
Button startServiceButton;// 启动服务按钮
@ViewInject(R.id.stop_service)
Button shutDownServiceButton;// 关闭服务按钮
@ViewInject(R.id.bind_service)
Button startBindServiceButton;// 启动绑定服务按钮
@ViewInject(R.id.unbind_service)
Button unbindService;

boolean bound;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_my_service_demo);
x.view().inject(this);
setData();
}

private void setData() {
intent = new Intent();
intent.setClass(MyServiceDemo.this,MyService.class);
}
@Event({R.id.stop_service,R.id.start_service,R.id.bind_service,R.id.unbind_service,R.id.pause})
private void OnClick(View v){
switch (v.getId()){
case R.id.start_service:
startService(intent);
break;
case R.id.stop_service:
stopService(intent);
break;
case R.id.bind_service:
bindService(intent,sc,BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(sc);

break;
default:
if(MyService.bound.equals("start")) {
MyService.player.pause();
}
break;
}
}

//  第一次调用这个方法之后会调用 onBind (onBind方法会返回给activityIBinder对象) onCreate()
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {

System.out.println("bindService:bindService");

if(MyService.player!=null) {
MyService.player.start();//开始播放
MyService.bound = "start";
}
return super.bindService(service, conn, flags);

}
//bindService断开连接 调用此方法
@Override
public void unbindService(ServiceConnection conn) {
System.out.println("unbindService:unbindService");
super.unbindService(conn);
}

@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(sc);
System.out.println("onDestroy:activity退出了");
}
}


xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_service_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.workfirst.MyServiceDemo">
<Button
android:id="@+id/start_service"
android:text="启动"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_below="@id/start_service"
android:id="@+id/stop_service"
android:text="停止"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bind_service"
android:text="绑定"
android:layout_below="@id/stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/unbind_service"
android:text="解绑"
android:layout_below="@id/bind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/pause"
android:text="暂停"
android:layout_below="@id/unbind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>


service代码:

package com.workfirst;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;

public class MyService extends Service {
public static String bound = "stop";
public static  MediaPlayer player;
public MyService() {
}

class Mybinder extends Binder{
MyService getService(){
return MyService.this;
}
}
private Mybinder ms = new Mybinder();

@Override
public IBinder onBind(Intent intent) {
System.out.println("MyService:onBind");
return  ms;

}
@Override
public void onRebind(Intent intent) {
System.out.println("MyService:onRebind");
super.onRebind(intent);
}

@Override
public boolean onUnbind(Intent intent) {
System.out.println("MyService:onUnbind");
return super.onUnbind(intent);
//return true;
}

@Override
public void onCreate() {

super.onCreate();
}
//    @Override
//    public void onStart(Intent intent, int startId) {
//        System.out.println("onStart:onStart3");
//        super.onStart(intent, startId);
//    }

//暂停
public void onPause() {
if(bound.equals("start")){
player.pause();
bound = "pause";
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand:onStartCommand2");
int sd = super.onStartCommand(intent, flags, startId);

Log.e("sd:",Integer.toString(sd));//1
Log.e("flags:",Integer.toString(flags));//0
Log.e("startId:",Integer.toString(startId));//1... 执行次数

System.out.println("MyService:onCreate");
//mediaplayer 的静态方法 create //从资源ID对应的资源文件夹中装载音频
if(player!=null){
player.start();//开始播放
}else {
player = MediaPlayer.create(this, R.raw.test);
player.setLooping(false);//设置是否循环

if (!bound.equals("start")) {
bound = "start";
player.start();//开始播放
}

}

return sd;
}

@Override
public void onDestroy() {
onPause();
player.release();
System.out.println("onDestroy:onDestroy(停止 只执行这一个方法)");
super.onDestroy();
}
}


贴上 startservice 和bindservice图(来源网络)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: