我是靠谱客的博主 爱听歌便当,最近开发中收集的这篇文章主要介绍Android开发笔记之广播,service实现音乐的播放暂停停止快进等功能,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

利用广播实现音乐的播放暂停停止快进等功能

broadast

  1. 此处是service里面实现的功能

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);
}

}

在主activity我们主要实现广播的发送命令,控件的初始化和绑定监听事件,广播信号源我们采取自定义的信号源,利用intent把我们的发送命令传递到音乐服务类中去,在音乐服务类中,在接收方法中,利用广播接收到带有操作命令的intent,通intent.getIntExtra(“type”, -1);得到我们要操作的命令值,去执行音乐的播放-暂停-停止-快进-后退等服务.

以上就是所有的代码,布局没有贴上,很丑陋的布局,大家自己去建一个布局,相信你们会实现想要的效果的.
贴上我的工程图片,以免大家不知道在那里操作

最后

以上就是爱听歌便当为你收集整理的Android开发笔记之广播,service实现音乐的播放暂停停止快进等功能的全部内容,希望文章能够帮你解决Android开发笔记之广播,service实现音乐的播放暂停停止快进等功能所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部