Android老师的小作业。让使用Android的service实现音乐播放器。话不多说,直接上代码
首先你要有一个主MainActivity
package com.example.musicplayer;
import org.w3c.dom.Text;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import com.example.musicplayer.MusicSevice.MyBinder;
public class MainActivity extends Activity implements OnClickListener {
private EditText path;
private Intent intent;
private myConn conn;
MyBinder binder;
private SeekBar mSeekBar;
private Thread mThread;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
switch(msg.what){
case 100:
int currentPosition=(Integer) msg.obj;
mSeekBar.setProgress(currentPosition);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
path=(EditText) findViewById(R.id.et_inputpath);
findViewById(R.id.bt_play).setOnClickListener(this);
findViewById(R.id.bt_pause).setOnClickListener(this);
findViewById(R.id.bt_replay).setOnClickListener(this);
findViewById(R.id.bt_stop).setOnClickListener(this);
mSeekBar=(SeekBar) findViewById(R.id.seekBar1);
conn=new myConn();
intent=new Intent(this,MusicSevice.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
//初始化进度条长度,获取音乐文件长度
private void initSeekBar(){
int musicWidth=binder.getMusicWidth();
mSeekBar.setMax(musicWidth);
}
//更新音乐播放进度
private void updateProgress(){
mThread=new Thread(){
public void run(){
while(!interrupted()){
//调用服务中的获取当前播放进度
int currentPosition =binder.getCurrentPosition();
Message message=Message.obtain();
message.obj=currentPosition;
message.what=100;
handler.sendMessage(message);
}
}
};
mThread.start();
}
private class myConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder=(MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View v) {
String pathway=path.getText().toString().trim();
String path="data/data/com.example.musicplayer/1.mp3";
switch (v.getId()) {
case R.id.bt_play:
if(!TextUtils.isEmpty(path)){
binder.palys(path);
initSeekBar();
updateProgress();
}else{
Toast.makeText(this, "找不到音乐文件", 0).show();
}
break;
case R.id.bt_pause:
binder.pauses();
break;
case R.id.bt_replay:
binder.repalys(pathway);
break;
case R.id.bt_stop:
mThread.interrupt();
if(mThread.isInterrupted()){
binder.stops();
}
default:
break;
}
}
protected void onDestory(){
if(mThread!=null){
if(!mThread.isInterrupted()){
mThread.interrupt();
}
}
unbindService(conn);
super.onDestroy();
}
}
然后你要有一个service,叫MusicService,
package com.example.musicplayer;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MusicSevice extends Service{
private static final String TAG="MusicService";
public MediaPlayer mediaPlayer;
class MyBinder extends Binder{
public void palys(String path){
play(path);
}
public void pauses(){
pause();
}
public void repalys(String path){
replay(path);
}
public void stops(){
stop();
}public int getCurrentPosition(){
return getCurrenProgress();
}
public int getMusicWidth(){
return getMusicLength();
}
}
public void onCreate(){
super.onCreate();
}
@SuppressLint("NewApi")
private void play(String path) {
try{
if(mediaPlayer == null){
Log.i(TAG,"开始播放音乐");
//创建一个MediaPlayer播放器
mediaPlayer=new MediaPlayer();
//指定参数为音频文件
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//指定播放路径
mediaPlayer.setDataSource(path);
//准备播放
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}else{
int position =getCurrenProgress();
mediaPlayer.seekTo(position);
try{
mediaPlayer.prepare();
}catch(Exception e){
e.printStackTrace();
}
mediaPlayer.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
//暂停音乐
public void pause(){
if(mediaPlayer != null && mediaPlayer.isPlaying()){
Log.i(TAG, "暂停播放");
mediaPlayer.pause();
}else if(mediaPlayer !=null && (!mediaPlayer.isPlaying())){
mediaPlayer.start();
}
}
//重新播放
public void replay(String path){
if(mediaPlayer!=null){
Log.i(TAG, "重新开始播放");
mediaPlayer.seekTo(0);
try{
mediaPlayer.prepare();
}catch(Exception e){
e.printStackTrace();
}
mediaPlayer.start();
}
}
//停止播放
public void stop() {
if(mediaPlayer != null){
Log.i(TAG, "停止播放");
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}else{
Toast.makeText(getApplicationContext(), "已停止", 0).show();
}
}
//获取文件资源长度
public int getMusicLength(){
if(mediaPlayer != null){
return mediaPlayer.getDuration();
}
return 0;
}
//获取当前进度
public int getCurrenProgress(){
try{
if(mediaPlayer !=null){
if(mediaPlayer.isPlaying()){
Log.i(TAG, "获取当前进度");
return mediaPlayer.getCurrentPosition();
}else if(!mediaPlayer.isPlaying()){
return mediaPlayer.getCurrentPosition();
}
}
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
public void onDestroy(){
if(mediaPlayer !=null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return new MyBinder();
}
}
当然,你至少要有一个主样式文件,activity_main.xml,
下面的1.mp3文件是要播放的音乐文件,自己搞定哦
最后别忘了注册你的activity和service,注册文件如下
当然还有可能因为Android sdk版本的问题出现点小毛病,这里推荐使用Android 19
以上是本文的全部内容,谢谢观看