- import java.io.File;
- import java.io.FilenameFilter;
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.ListActivity;
- import android.apps.service.PlayerService;
- import android.content.Intent;
- import android.media.AudioManager;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.Toast;
-
- public class TestMediaPlayer extends ListActivity {
- /* 前段時間剛學習Android時就以爲MediaPlayer是個很神奇的東西,就試着作了個簡單的音樂播放器。
- * 支持後臺播放、進度條拖放、音量控制、讀取sdCard音樂文件進行播放等。
- * */
- private Button playButton = null;
- private Button mFrontButton = null;
- private Button mLastButton = null;
- private Button exit = null;
- /* 聲明音量管理器 */
- private AudioManager mAudioManager = null;
-
- /* 定義進度條 */
- public static SeekBar audioSeekBar = null;
-
- /* 定義音量大小 */
- private SeekBar audioVolume = null;
-
- /* 定於一個數據播放列表,用來存放從指定文件中搜索到的文件 */
- public static List<String> mMusicList = new ArrayList<String>();
-
- /* 定義音樂存放路徑 */
- // private static final String MUSIC_PATH = new String("/mnt/sdcard/"); //android 2.2
- private static final String MUSIC_PATH = new String("/sdcard/");//android 2.1
- /* 定義在播放列表中的當前選擇項 */
- public static int currentListItme = 0;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /* 更新列表 */
- musicList();
- /* 獲得控件 */
- playButton = (Button) findViewById(R.id.StartMusic);
- mFrontButton = (Button) findViewById(R.id.FrontButton);
- mLastButton = (Button) findViewById(R.id.LastMusic);
- audioVolume = (SeekBar) findViewById(R.id.audioVolume);
- exit = (Button)findViewById(R.id.exit);
- audioSeekBar = (SeekBar) findViewById(R.id.seekbar01);
- /* 播放、暫停監聽 */
- playButton.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- playMusic(AppConstant.PlayerMag.PAUSE);
- }
- });
- /* 監聽下一首 */
- mFrontButton.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- nextMusic();
- }
- });
- /* 監聽上一首 */
- mLastButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- FrontMusic();
- }
- });
- /*退出監聽*/
- exit.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(TestMediaPlayer.this, PlayerService.class);
- stopService(intent);//中止Service
- try {
- TestMediaPlayer.this.finish();//關閉當前Activity
- } catch (Throwable e) {
- e.printStackTrace();
- }
-
-
- }
- });
- /* 播放進度監聽 */
- audioSeekBar.setOnSeekBarChangeListener(new SeekBarChangeEvent());
- /*退出後再次進去程序時,進度條保持持續更新*/
- if(PlayerService.mMediaPlayer!=null){
- //設置進度條最大值
- TestMediaPlayer.audioSeekBar.setMax(PlayerService.mMediaPlayer.getDuration());
- audioSeekBar.setProgress(PlayerService.mMediaPlayer.getCurrentPosition());
- }
-
-
- /* 獲得當前音量對象 */
- mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
- /* 把當前音量值賦給進度條 */
- audioVolume.setProgress(mAudioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- /* 監聽音量 */
- audioVolume.setOnSeekBarChangeListener(new AudioVolumeChangeEvent());
- }
-
- public void playMusic(int action) {
- Intent intent = new Intent();
- intent.putExtra("MSG", action);
- intent.setClass(TestMediaPlayer.this, PlayerService.class);
-
- /* 啓動service service要在AndroidManifest.xml註冊如:<service></service>*/
-
- startService(intent);
- }
-
- /* 檢測sdcard MP3文件並加入到List列表 */
- public void musicList() {
- // 先清除list中的緩存
- mMusicList.clear();
- /* 從指定的路徑中讀取文件,並與播放列表關聯 */
- File home = new File(MUSIC_PATH);
-
- System.out.println(home.canRead());
- /* 讀取指定類型的文件,在本程序,指定播放類型爲mp3 */
- if (home.listFiles(new MusicFilter()).length > 0) {
- /* 讀取文件 */
- for (File file : home.listFiles(new MusicFilter())) {
- mMusicList.add(file.getName());
- }
- /* 播放文件與播放列表關聯 */
- ArrayAdapter<String> musicList = new ArrayAdapter<String>(
- TestMediaPlayer.this, R.layout.musictime, mMusicList);
- setListAdapter(musicList);
- }
- }
-
- /* 音量監聽 */
- class AudioVolumeChangeEvent implements SeekBar.OnSeekBarChangeListener {
-
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
-
- // mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
- mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
- 0);
-
- }
-
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
-
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
-
- }
-
- /* 音樂選擇監聽 */
- @Override
- protected void onListItemClick(android.widget.ListView l, View v,
- int position, long id) {
- super.onListItemClick(l, v, position, id);
- currentListItme = position;
- playMusic(AppConstant.PlayerMag.PLAY_MAG);
- }
-
- /* 播放下一首 */
- private void nextMusic() {
- if (++currentListItme >= mMusicList.size()) {
- Toast.makeText(TestMediaPlayer.this, "已到最後一首歌曲", Toast.LENGTH_SHORT)
- .show();
- currentListItme = mMusicList.size() - 1;
- } else {
- playMusic(AppConstant.PlayerMag.PLAY_MAG);
- }
- }
-
- /* 播放上一首歌曲 */
- private void FrontMusic() {
- if (--currentListItme >= 0) {
- playMusic(AppConstant.PlayerMag.PLAY_MAG);
-
- } else {
- Toast.makeText(TestMediaPlayer.this, "已到第一首歌曲", Toast.LENGTH_SHORT)
- .show();
- currentListItme = 0;
- }
- }
- }
-
- /* 播放文件選擇類 */
- class MusicFilter implements FilenameFilter {
- public boolean accept(File dir, String name) {
- /* 指定擴展名類型 ,能夠加其餘的音樂格式*/
- return (name.endsWith(".mp3"));
- }
- }
-
- /* 拖放進度監聽 ,別忘了Service裏面還有個進度條刷新*/
- class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
-
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- /*假設改變源於用戶拖動*/
- if (fromUser) {
- PlayerService.mMediaPlayer.seekTo(progress);// 當進度條的值改變時,音樂播放器重新的位置開始播放
- }
- }
-
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- PlayerService.mMediaPlayer.pause(); // 開始拖動進度條時,音樂暫停播放
- }
-
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- PlayerService.mMediaPlayer.start(); // 中止拖動進度條時,音樂開始播放
- }
- }
service類java
首先建議不要把播放音樂放在Activity中,由於關閉Activity後就不能實現後臺播放。因此播放音樂要放在Service中,咱們只須要管理Activity與Service間的通訊就行了。下面就是個Service類。直接看註釋和代碼:android
參數封裝緩存
- /**
- * @把參數這樣封裝易於管理和閱讀
- *
- */
- public interface AppConstant {
- public class PlayerMag{
- public static final int PLAY_MAG=1;//開始播放
- public static final int PAUSE=2;//暫停播放
- }
- }
詳細代碼下載:http://download.csdn.net/source/3318587app