Android 多媒体中的音乐播放器编程

时间:2021-07-04 03:21:32

1、MediaPlayer 的使用步骤:
MediaPlayer player = new MediaPlayer
//播放音乐
player. reset(); //先 重置
player.setDataSource(“sdcard/lnh.mp3”); // 设定文件的路径。
player.prepare(); //准备播放
player.start(); //开始播放
注意在清单文件中配置service.
停止播放音乐的方法是 :将服务解绑,然后停止服务。
获取外部存储的方法: Environment.getExternalStorageDirectory();

需要提醒的是即使服务停止了MediaPlayer占用的资源不会被释放。c语言不会自动的释放内存的。
要使MeidaPlayer被释放需要手动的释放。

  public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//停止播放。
player.stop();
//释放资源 c语言资源释放。
player.release();
//引用置为空。
player =null;

}

MusicService代码:





//接口
package com.zh.musicplayer;

public interface MusicInterface {
void play();
void continueplay();
void pause();

}






package com.zh.musicplayer;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {

public MediaPlayer player;

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

class MusicControler extends Binder implements MusicInterface {

@Override
public void play() {
// TODO Auto-generated method stub
MusicService.this.play();
}

@Override
public void continueplay() {
// TODO Auto-generated method stub
MusicService.this.continueplay();
}

@Override
public void pause() {
// TODO Auto-generated method stub
MusicService.this.pause();
}

}

@Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
Log.e("onCreate", "onCreate player = " + player);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 停止播放。
player.stop();
// 释放资源 c语言资源释放。
player.release();
// 引用置为空。
player = null;

}

// 播放音乐
public void play() {

player.reset();
try {

player.setDataSource(Environment.getExternalStorageDirectory().toString() + "/xs.mp3");

player.prepare();
player.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void continueplay() {
player.start();
}

public void pause() {
player.pause();
}

}

Mainactivity中的代码:

package com.zh.musicplayer;

import java.io.File;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

public class MainActivity extends Activity implements OnClickListener {

private Button bt_play;
private Button bt_pause;
private Button bt_continueplay;
private Button bt_stop;
private MusicInterface mi;
private Intent intent;
private MyServiceconn conn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_play = (Button) findViewById(R.id.bt_play);
bt_pause = (Button) findViewById(R.id.bt_pause);
bt_continueplay = (Button) findViewById(R.id.bt_continueplay);
bt_stop = (Button) findViewById(R.id.bt_stop);
bt_play.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_continueplay.setOnClickListener(this);
bt_stop.setOnClickListener(this);

/*
* String rootPath =
* Environment.getExternalStorageDirectory().toString();
* System.out.println("rootPath = "+ rootPath+"/img/xxx.png"); File
* rootFile = Environment.getExternalStorageDirectory(); File[] files =
* rootFile.listFiles(); if (files == null) { System.out.println(
* "files = "+ files); return; }
*
* for (File file : files) { System.out.println("name = "+
* file.getName()); }
*/


intent = new Intent(this, MusicService.class);
conn = new MyServiceconn();
startService(intent); //先开启服务
bindService(intent, conn, BIND_AUTO_CREATE);//再绑定服务,上面的操作将服务提升为服务进程。
}

class MyServiceconn implements ServiceConnection {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.e("onServiceConnected", "service = " + service);
mi = (MusicInterface) service;
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_play:
Log.e("", "mi = " + mi);
mi.play();

break;
case R.id.bt_pause:
mi.pause();

break;
case R.id.bt_continueplay:
mi.continueplay();

break;
case R.id.bt_stop:
unbindService(conn);
stopService(intent);
finish();

break;

}

}

}

网络音乐播放器:

主要方法如下:
public void play() {
player.reset();
try {

// 设置文件路径。
player.setDataSource("http://192.168.1.4:8080/app/kz.mp3");
player.prepareAsync(); //这个是异步准备区别于直接加载的音乐
player.setOnPreparedListener(new OnPreparedListener() { //设置准备监听,准备好后触发onPrepared 的方法。

@Override//设置准备监听 已经准备好
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
player.start(); //网络准备已经准备完毕就开始播放。
}
});


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void continueplay() {

player.start();

}

public void pause() {
player.pause();

}

设置播放音乐的进度条:
seekbar 可以拖动的进度条 不同于 ProgressBar 下载过程进度条。
进度: 总时长 和 当前时长。
这两个都是MediaPlayer中的方法。

//获取当前歌曲的总时长。
player.getDuration();
//获取当前歌曲的位置 返回的毫秒值。
player.getCurrentPosition();

由于当前歌曲的位置不断的发生着变化需要一个子线程保证获取当前进度不断的被刷新。
子线程中需要不断的刷新进度条也就是刷新UI,需要handler对象。
另外需要注意的是Handler 处于Mainactivity中,而刷新进度条的方法却在服务中。
schedule: 计划。
调用handler发送消息的方法:(利用Bundle对象将msg消息进行封装)

    public void addTimer() {
// 开启一个计时器 相当于一个子线程,而且能记时。
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
// 获取当前歌曲的总时长。
int duration = player.getDuration();
// 获取当前歌曲的位置 返回的毫秒值。
int currentpostion = player.getCurrentPosition();
Message msg = MainActivity.handler.obtainMessage();
// 用Bundle对象封装数据。
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentpostion", currentpostion);

msg.setData(bundle);

MainActivity.handler.sendMessage(msg);
}
}, 5, 500);
}

Bundle对象封装好数据后到Mainactivity中的handleMessage方法中需要取出封装的数据:

static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
Bundle bundle = msg.getData(); //取出setData中的Bundle对象。 从bundle对象中取出数据。
int duration = bundle.getInt("duration");
int currentpostion = bundle.getInt("currentpostion");
sb.setProgress(currentpostion); //设置seekbar的当前进程。
sb.setMax(duration); //设置seekbar的最大进程。
};
};