您的位置:首页 > 移动开发 > Android开发

Android开发利用service实现背景音乐播放

2015-07-03 08:44 615 查看
设计思路:

建议在播放音乐的时候把MediaPlayer放在Service中,因为如果放在Activity中会使得界面特别卡。而且音乐不能放到后台里播放,一旦退出Activity,音乐就会暂停播放。

可以在Activity中布局相关的界面,例如按钮等。然后通过这个Activitiy来启动这个Service。要通过UI与Service交互,可以通过Intent对象传递消息。更复杂一些,要实现Service向Activity发送消息,并利用这些消息来更新UI,这可以用广播机制,例如告诉Activity是否正在播放,播放进度,当前播放歌曲条目等信息。

项目整体布局:



界面放两个Button按钮。一个播放,一个暂停



下面开始写相关的代码

1. Activity类:

package com.example.service;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button playing, stop;// 声明控件变量

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findId();
playing.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, AudioService.class));// 启动service服务转向AudioService
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this, AudioService.class));// 停止service
}
});
}

void findId() {// 获取控件id
playing = (Button) findViewById(R.id.playing);
stop = (Button) findViewById(R.id.stop);
}

protected void onResume() {// 在 Activity 从 Pause 状态转换到 Active 状态时被调用。
super.onResume();
startService(new Intent(MainActivity.this, AudioService.class));
}

@Override
protected void onDestroy() {// 防止程序退出后音乐不停止问题。在destroy中停止服务
// TODO Auto-generated method stub
super.onDestroy();
stopService(new Intent(MainActivity.this, AudioService.class));// 停止service
}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


2.Service类:

package com.example.service;

/**
* 多线程实现后台播放背景音乐的service
*/
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class AudioService extends Service implements
MediaPlayer.OnCompletionListener {
// 实例化MediaPlayer对象
MediaPlayer player;
private final IBinder binder = new AudioBinder();

@Override
public IBinder onBind(Intent intent) {
return binder;
}

public void onCreate() {
super.onCreate();
// 从raw文件夹中获取一个应用自带的mp3文件
player = MediaPlayer.create(this, R.raw.qq);
player.setOnCompletionListener(this);// 是为player对象添加一个监听事件,用于监听事件完成
player.setLooping(true);//
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (!player.isPlaying()) {
new MusicPlayThread().start();
} else
player.isPlaying();
return START_STICKY;
}

/**
* 当Audio播放完的时候触发该动作
*/
public void onCompletion(MediaPlayer mp) {
stopSelf();// 结束Service

}

public void onDestroy() {
super.onDestroy();
if (player.isPlaying()) {
player.stop();
}
player.release();
}

// 为了和Activity交互,我们需要定义一个Binder对象
public class AudioBinder extends Binder {
// 返回Service对象
public AudioService getService() {
return AudioService.this;
}
}

private class MusicPlayThread extends Thread {
public void run() {
if (!player.isPlaying()) {
player.start();
}
}
}

}


3、在清单文件AndroidManifest.xml中配置Service

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