标签:android mediaplayer 音乐 播放器
本文章介绍MediaPlayer本地音乐播放器,而当应用程序不再位于前台且没有正在使用它的活动时,为了确保音频继续播放,我们需要建立一个服务Service。
Activity与绑定服务Service之间的交互是本文章的重点(这里需要说明一点的是,Activity不能直接访问服务对象中的方法,所以才有了我们一下的介绍,这也是为服务的安全等方面的考虑)。
直接上代码:
布局文件:activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="开启后台服务播放音频" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<Button
android:id="@+id/fastBackward"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="快退" />
<Button
android:id="@+id/startServiceButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开启" />
<Button
android:id="@+id/stopServiceButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="关闭" />
<Button
android:id="@+id/fastForward"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="快进" />
</LinearLayout>
</LinearLayout>package com.multimediademo6audioservice;
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;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button startServiceButton, stopServiceButton, fastBackward,
fastForward;
private Intent intentService;
private SimpleAudioService simpleAudioService;
boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/**
* 实例化组件
*/
private void init() {
startServiceButton = (Button) findViewById(R.id.startServiceButton);
stopServiceButton = (Button) findViewById(R.id.stopServiceButton);
fastBackward = (Button) findViewById(R.id.fastBackward);
fastForward = (Button) findViewById(R.id.fastForward);
startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
fastBackward.setOnClickListener(this);
fastForward.setOnClickListener(this);
intentService = new Intent(this, SimpleAudioService.class);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
/***
* 开启
*/
case R.id.startServiceButton:
startService(intentService);
/**
* 通过一条bindService命令建立了与服务的连接,且该bindService命令把这个对象(
* 下面的ServiceConnection接口)命名为serviceConnection。
*/
bindService(intentService, serviceConnection,
Context.BIND_AUTO_CREATE);
flag = true;
break;
/**
* 关闭
*/
case R.id.stopServiceButton:
if (flag) {
unbindService(serviceConnection);
stopService(intentService);
flag = false;
}
break;
/**
* 快退
*/
case R.id.fastBackward:
if (flag) {
simpleAudioService.backwardFun();
}
break;
/**
* 快进
*/
case R.id.fastForward:
if (flag) {
simpleAudioService.forwardFun();
}
break;
default:
break;
}
}
/**
* serviceConnection是一个ServiceConnection类型的对象,它是一个接口,用于监听所绑定服务的状态
*/
private ServiceConnection serviceConnection = new ServiceConnection() {
/**
* 点击开启按钮,会调用serviceConnection对象中的onServiceConnected方法。
* 向该方法传递一个IBinder对象
* ,其实际是从服务本身创建和提交的。这个IBinder对象将是SimpleAudioServiceBinder类型,我们将在服务中创建它。
* 它将有一个方法用于返回我们的服务本身,成为getService,这样我们就可以对该方法返回的对象直接进行操作。
*/
@Override
public void onServiceConnected(ComponentName name, IBinder sasBinder) {
simpleAudioService = ((SimpleAudioService.SimpleAudioServiceBinder) sasBinder)
.getService();
}
/**
* 该方法用于处理与服务断开连接时的情况。
*/
@Override
public void onServiceDisconnected(ComponentName name) {
simpleAudioService = null;
}
};
}
package com.multimediademo6audioservice;
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;
import android.util.Log;
/**
* 开启的服务
*
*/
public class SimpleAudioService extends Service implements OnCompletionListener {
private String TAG = "zhongyao";
private MediaPlayer player;
public class SimpleAudioServiceBinder extends Binder {
SimpleAudioService getService() {
return SimpleAudioService.this;
}
}
private final IBinder sasBinder = new SimpleAudioServiceBinder();
@Override
public IBinder onBind(Intent intent) {
return sasBinder;
}
/**
* 在MainActivity中点击开启后调用startService开启服务时,
* 会调用如下的两个方法:onCreate(),onStartCommand()
*/
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.good);
player.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
if (!player.isPlaying()) {
player.start();
}
return super.onStartCommand(intent, flags, startId);
}
/**
* 调用stopService停止服务时,会调用onDestroy()方法。
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (player.isPlaying()) {
player.stop();
}
player.release();
}
/**
* 播放完以后调用
*/
@Override
public void onCompletion(MediaPlayer mp) {
stopSelf();
}
/**
* 服务中方法:快退
*/
public void backwardFun() {
if (player.isPlaying()) {
player.seekTo(player.getCurrentPosition() - 2500);
}
}
/**
* 服务中方法:快进
*/
public void forwardFun() {
if (player.isPlaying()) {
player.seekTo(player.getCurrentPosition() + 2500);
}
}
}
Android Mediaplayer本地音乐播放器(绑定服务),布布扣,bubuko.com
Android Mediaplayer本地音乐播放器(绑定服务)
标签:android mediaplayer 音乐 播放器
原文地址:http://blog.csdn.net/u012440207/article/details/26696535