android mediaplayer service,关于android的MediaPlayer小Service的基本功能的实现

这是我第一次接触android里面Service组件,感觉它很重要,后台运行的功能很多应用都需要,这里通过一个MediaPlayerDemo来初步总结下它的一些特征。

首先,Service的生命周期与Activity完全不同,它只有onCreate,onStart和onDestory三步。所以在操作它时与Activity还是有区别的。

关于这个建议Mediaplayer的Service,我是这么写的:

public class MediaPlayers extends Service{

MediaPlayer mp;

String []

music={"/sdcard/SexyGirl.mp3","/sdcard/cold.mp3"};//将sd卡中的歌曲转化为一个String数组,但是貌似针对中文路径和中文名称有一定问题。

int temp;

public void onCreate() {

temp=0;

mp =

MediaPlayer.create(this,Uri.parse(music[temp]));//创建Service,内部代码则是创建一个MediaPlayer对象,与地址数组绑定。

super.onCreate();

}

public void onStart(Intent intent, int startId)

{//通过MediaPlayer主类中发送过来的key(flag)进行不同的操作。

String str =

intent.getStringExtra("flag");

if(str.equals("Start")){//当发送过来的value为Start时,启动mediaplayer

mp.start();

temp++;

}

else

if(str.equals("Pause")){//mp暂定

mp.pause();

}else

if(str.equals("Next")){//实现下一首操作。

mp.stop();

mp =

MediaPlayer.create(this,Uri.parse(music[temp]));

mp.start();

temp++;

if(temp==

music.length){//当temp数值越界时,归零实现歌曲的循环。

temp=0;;

}

}

super.onStart(intent,

startId);

}

public void onDestroy() {//Service销毁操作。

mp.stop();

Log.e("", "Stop");

super.onDestroy();

}

public IBinder onBind(Intent intent)

{//bind操作。可用于Service与Activity之间的数据返回。

return new MyName();

}

public class MyName extends Binder {

public String getName() {

return

music[temp];

}

}

}

MediaPlayer主类代码如下:

import android.app.Activity;

import android.app.Service;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import com.Text727.MediaPlayers.MyName;

public class MediaPlayerDemo extends Activity implements

OnClickListener{

Button

buStart;

Button

buStop;

Button

buPause;

Button

buNext;

Button

buBind;

Button

buGetName;

TextView

tv;

Intent in

;

ServiceConnection ser;

public void

onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

buStart=(Button) findViewById(R.id.button1);

buStop=(Button) findViewById(R.id.button2);

buPause=(Button) findViewById(R.id.button3);

buNext=(Button) findViewById(R.id.button4);

buBind=(Button) findViewById(R.id.button5);

buGetName=(Button) findViewById(R.id.button6);

buStart.setOnClickListener(this);

buStop.setOnClickListener(this);

buPause.setOnClickListener(this);

buNext.setOnClickListener(this);

buBind.setOnClickListener(this);

buGetName.setOnClickListener(this);

tv=(TextView) findViewById(R.id.tv);

in= new Intent();

in.setClass(this,

MediaPlayers.class);

ser = new ServiceConnection()

{//建议一个ServiceConnection对象,完成与Service的数据连接。

public void

onServiceDisconnected(ComponentName name) {

} public void

onServiceConnected(ComponentName name, IBinder service) {

MyName

music = (MyName) service;

String

mname=music.getName();

tv.setText(mname);

}

};

} public void onClick(View v) {//设置各个按键的相应触发。

switch(v.getId()){

case R.id.button1:

in.putExtra("flag",

"Start");

this.startService(in);

break;

case R.id.button2:

this.unbindService(ser);//这里值的额外注意!!!!!必须将正在运行的bindService关闭之后才能

//正常stopService。

this.stopService(in);

break;

case R.id.button3:

in.putExtra("flag", "Pause");

this.startService(in);

break;

case R.id.button4:

in.putExtra("flag",

"Next");

this.startService(in);

break;

case R.id.button5:

// Intent iin=

new Intent();

in.setClass(this,

MediaPlayers.class);

this.bindService(in,

ser,Service.BIND_AUTO_CREATE );//bindService与StartService有些类似,

Service.BIND_AUTO_CREATE 这个参数说明该bindService在建立时自动创建Service,就生命周期来说,这时Service运行到onCreate这一步。ser这个参数说明了建立连接之后的操作。

break;

case R.id.button6:

this.unbindService(ser);//必须先关闭之前的bindSevice再重新建立才能完成多次获取的操作。

Intent inn =

new Intent();

inn.setClass(this,MediaPlayers.class);

this.bindService(inn,

ser,

Service.BIND_AUTO_CREATE);//为了完成多次bindSrvice操作实时获得Service中的反馈必须做这一步。

break;

} }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值