broadast
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;
import android.util.Log;
public class MyMediaService extends Service {
public static final int PLAY = 1;
public static final int PAUSE = 2;
public static final int STOP = 3;
public static final int FORWARD = 4;
public static final int BACKWARD = 5;
MediaPlayer mMediaPlayer;
@Override
public void onCreate() {
RegesterBroadcast();
Log.d("sxh", "服务");
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/*******************************************************
* create a broadcast and we can get the value by intent
* In the receiving method we can judge the value which
* is transferd through intent Then we can do something
* by the value
*/
public class MyMediaPlayerBroadcast extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("sxh", "广播");
int type = intent.getIntExtra("type", -1);
switch (type) {
case PLAY: play(); break;
case PAUSE: pause();break;
case STOP: stop(); break;
case FORWARD: forWard(); break;
case BACKWARD: backWard(); break;
default:
break;
}
}
}
/**************************************************************************
* Play the music method This method is adopted in the asynchronous loading
* resources, Asynchronous loading resources will not block the UI thread So
* he immediately to carry out The next procedure, so we call the
* MediaPlayer Start method in the event of an asynchronous loading resource
* listening in to finish This way is ready for playing music which is take
* the
*/
public void play() {
if (mMediaPlayer == null) {
try {
mMediaPlayer = MediaPlayer.create(this, R.raw.rihanna);
setMediaPrepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
} else if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
}
/**************************************************************
* the metnod is to pause the music
* we need to judge the music whether music in a state of playing
* before pasuing the music
*/
public void pause(){
if(mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
}
/*******************************************
* the method is to stop the music
* Before we stop the music, it is necessary
* to determine whether music is empty
*/
public void stop(){
if(mMediaPlayer != null){
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
/********************************************
* The fast method of music
*/
public void forWard(){
Log.i("sxh", "forWard");
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
int position = mMediaPlayer.getCurrentPosition();
mMediaPlayer.seekTo(position + 10000);
}
}
public void backWard(){
if(mMediaPlayer != null && mMediaPlayer.isPlaying()){
int position = mMediaPlayer.getCurrentPosition();
if(position >10000){
position -= 10000;
}else{
position =0;
}
mMediaPlayer.seekTo(position);
}
}
/**************************************************************************
* Set the audio loaded to monitor events, only after completion of loading
* Can go to call the beginning of the multimedia method
*/
public void setMediaPrepareAsync() {
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
}
/******************************************
* Dynamic registration broadcasting method
*/
public void RegesterBroadcast() {
IntentFilter filter = new IntentFilter();
filter.addAction("flag");
registerReceiver(new MyMediaPlayerBroadcast(), filter);
}
}
主activity里面实现的代码如下
package com.example.day3demo.activity;
import com.example.day3demo.R;
import com.example.day3demo.service.MyMediaService;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer mMediaPlayer;
Button mPlay, mPause, mStop;
TextView mTextView_one,mTextView_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/********************************
* 初始化控件
*/
public void initView() {
findViewById(R.id.play).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
findViewById(R.id.forward).setOnClickListener(this);
findViewById(R.id.backward).setOnClickListener(this);
mTextView_one =(TextView) findViewById(R.id.currenttime);
mTextView_two = (TextView) findViewById(R.id.alltime);
startService(new Intent(this, MyMediaService.class));
}
@Override
public void onClick(View v) {
int type = 0;
switch (v.getId()) {
case R.id.play:
type = MyMediaService.PLAY;
break;
case R.id.pause:
type = MyMediaService.PAUSE;
break;
case R.id.stop:
type = MyMediaService.STOP;
break;
case R.id.forward:
type = MyMediaService.FORWARD;
break;
case R.id.backward:
type = MyMediaService.BACKWARD;
break;
}
sendBroadcastData(type);
}
private void sendBroadcastData(int type) {
Intent intent = new Intent();
intent.putExtra("type", type);
intent.setAction("flag");
sendBroadcast(intent);
}
}