安卓后台音乐播放(Service)
2015-07-17 15:05
411 查看
Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行。若要关闭程序也关闭服务需要借助其中的方法。
以下是实现代码:
1.Activity类 package demo.camera;
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;
/**
* 演示Activity如何利用Service来完成后台Audio的播放功能
* 同时如何将Service和Activity进行绑定
* @author Administrator
*
*/
public class BackgroundAudioDemo extends Activity {
private AudioService audioService;
//使用ServiceConnection来监听Service状态的变化
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
audioService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
//这里我们实例化audioService,通过binder来实现
audioService = ((AudioService.AudioBinder)binder).getService();
}
};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.back_audio);
}
public void onClick(View v){
int id = v.getId();
Intent intent = new Intent();
intent.setClass(this, AudioService.class);
if(id == R.id.btn_start){
//启动Service,然后绑定该Service,这样我们可以在同时销毁该Activity,看看歌曲是否还在播放
startService(intent);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
finish();
}else if(id == R.id.btn_end){
//结束Service
unbindService(conn);
stopService(intent);
finish();
}else if(id == R.id.btn_fun){
audioService.haveFun();
}
}
} 2.Service类
package demo.camera;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.MediaController.MediaPlayerControl;
/**
* 为了可以使得在后台播放音乐,我们需要Service
* Service就是用来在后台完成一些不需要和用户交互的动作
* @author Administrator
*
*/
public class AudioService extends Service implements MediaPlayer.OnCompletionListener{
MediaPlayer player;
private final IBinder binder = new AudioBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
/**
* 当Audio播放完的时候触发该动作
*/
@Override
public void onCompletion(MediaPlayer player) {
// TODO Auto-generated method stub
stopSelf();//结束了,则结束Service
}
//在这里我们需要实例化MediaPlayer对象
public void onCreate(){
super.onCreate();
//我们从raw文件夹中获取一个应用自带的mp3文件
player = MediaPlayer.create(this, R.raw.tt);
player.setOnCompletionListener(this);
}
/**
* 该方法在SDK2.0才开始有的,替代原来的onStart方法
*/
public int onStartCommand(Intent intent, int flags, int startId){
if(!player.isPlaying()){
player.start();
}
return START_STICKY;
}
public void onDestroy(){
//super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();
}
//为了和Activity交互,我们需要定义一个Binder对象
class AudioBinder extends Binder{
//返回Service对象
AudioService getService(){
return AudioService.this;
}
}
//后退播放进度
public void haveFun(){
if(player.isPlaying() && player.getCurrentPosition()>2500){
player.seekTo(player.getCurrentPosition()-2500);
}
}
}
3、在清单文件AndroidManifest.xml中配置Service
<service
android:name=".AudioService" />
以下是实现代码:
1.Activity类 package demo.camera;
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;
/**
* 演示Activity如何利用Service来完成后台Audio的播放功能
* 同时如何将Service和Activity进行绑定
* @author Administrator
*
*/
public class BackgroundAudioDemo extends Activity {
private AudioService audioService;
//使用ServiceConnection来监听Service状态的变化
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
audioService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
//这里我们实例化audioService,通过binder来实现
audioService = ((AudioService.AudioBinder)binder).getService();
}
};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.back_audio);
}
public void onClick(View v){
int id = v.getId();
Intent intent = new Intent();
intent.setClass(this, AudioService.class);
if(id == R.id.btn_start){
//启动Service,然后绑定该Service,这样我们可以在同时销毁该Activity,看看歌曲是否还在播放
startService(intent);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
finish();
}else if(id == R.id.btn_end){
//结束Service
unbindService(conn);
stopService(intent);
finish();
}else if(id == R.id.btn_fun){
audioService.haveFun();
}
}
} 2.Service类
package demo.camera;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.MediaController.MediaPlayerControl;
/**
* 为了可以使得在后台播放音乐,我们需要Service
* Service就是用来在后台完成一些不需要和用户交互的动作
* @author Administrator
*
*/
public class AudioService extends Service implements MediaPlayer.OnCompletionListener{
MediaPlayer player;
private final IBinder binder = new AudioBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
/**
* 当Audio播放完的时候触发该动作
*/
@Override
public void onCompletion(MediaPlayer player) {
// TODO Auto-generated method stub
stopSelf();//结束了,则结束Service
}
//在这里我们需要实例化MediaPlayer对象
public void onCreate(){
super.onCreate();
//我们从raw文件夹中获取一个应用自带的mp3文件
player = MediaPlayer.create(this, R.raw.tt);
player.setOnCompletionListener(this);
}
/**
* 该方法在SDK2.0才开始有的,替代原来的onStart方法
*/
public int onStartCommand(Intent intent, int flags, int startId){
if(!player.isPlaying()){
player.start();
}
return START_STICKY;
}
public void onDestroy(){
//super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();
}
//为了和Activity交互,我们需要定义一个Binder对象
class AudioBinder extends Binder{
//返回Service对象
AudioService getService(){
return AudioService.this;
}
}
//后退播放进度
public void haveFun(){
if(player.isPlaying() && player.getCurrentPosition()>2500){
player.seekTo(player.getCurrentPosition()-2500);
}
}
}
3、在清单文件AndroidManifest.xml中配置Service
<service
android:name=".AudioService" />
相关文章推荐
- HDFS DataNode问题-_COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There
- 关于类成员变量的初始化
- 面试题04_替换空格_剑指Offer系列
- Java编程中“为了性能”需做的26件事
- ServletRequest与ServletResponse
- 凯撒密码
- tju3243 Blocked Road(树状数组/线段树)
- c++
- 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活
- Linux常用C函数-字符串转换篇
- javascript实现的闭包简单实例
- Socket_TcpClient学习开发中不经意的小问题。
- 如何禁止chrome跨域安全检查
- 结构体的字节对齐 转自海子博客
- 火箭作业系统知识总结
- 杂记
- 教程-Delphi7设置功能表
- Linux环境下Python的安装过程
- Remove Event Handler via Reflection
- Ducci Sequence