本部分代码在《Android应用开发揭秘》中提到,但是在eclipse环境下调试时出现异常,几番纠结,代码终于可以播放器音乐、并成功移植到手机上......

package com.example.calculate;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

@SuppressLint("SdCardPath")
public class Music extends ListActivity{

private static ImageButton mFrontImageButton = null;
private static ImageButton mStopImageButton = null;
private static ImageButton mPlayImageButton = null;
private static ImageButton mPauseImageButton = null;
private static ImageButton mNextImageButton = null;

//MediaPlayer对象
private MediaPlayer mMediaPlayer = null;

//歌曲播放列表
private List<String> mMusicList = new ArrayList<String>();

//当前歌曲播放索引
private int currentListItem = 0;

//音乐路径
// private static final String MUSIC_PATH = new String("/sdcard/"); //该路径是用于eclipse 下仿真用的
private static final String MUSIC_PATH = new String("/storage/sdcard1/qqmusic/song/"); //该路径是用于手机,这个路径是我手机歌曲的路径

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
// setContentView(R.layout.activity_calculate);

mMediaPlayer = new MediaPlayer();
//更新播放列表
musicList();


mStopImageButton = (ImageButton)findViewById(R.id.imageButton2);

mPlayImageButton = (ImageButton)findViewById(R.id.imageButton3);
mPlayImageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
}
});

mPauseImageButton = (ImageButton)findViewById(R.id.imageButton4);
mPauseImageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (mMediaPlayer.isPlaying())
mMediaPlayer.stop();
else
mMediaPlayer.start();
}
});

mNextImageButton = (ImageButton)findViewById(R.id.imageButton5);
mNextImageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
nextMusic();
}
});

mFrontImageButton = (ImageButton)findViewById(R.id.imageButton1);
mFrontImageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
FrontMusic();
}
});


listener();
}

//监听事件
void listener(){
//停止
mStopImageButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
}
});
}

public void onClick(View view) {
if (view == mFrontImageButton){

}else if (view == mStopImageButton) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
}else if (view == mPlayImageButton) {
playMusic(MUSIC_PATH+mMusicList.get(currentListItem));

}else if (view == mPauseImageButton) {
if (mMediaPlayer.isPlaying())
mMediaPlayer.stop();
else
mMediaPlayer.start();

}else if (view == mNextImageButton) {
nextMusic();
}else if (view == mFrontImageButton) {
FrontMusic();
}
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mMediaPlayer.stop();
mMediaPlayer.release();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
currentListItem = position;
playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
}

public void playMusic(String path) {
try {
//mMediaPlayer在重新播放一首歌曲之前要之行下面的操纵,
//reset()重置,
//setDataSource(path)播放歌曲路径.
//prepare()、start() 准备、播放.
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
//播放完一首歌曲之后进行下一首
nextMusic();
}
});

}catch (IOException e) {
e.printStackTrace();
}
}

public void nextMusic() {
if (++currentListItem >= mMusicList.size()) {
currentListItem = 0;
}else {
playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
}
}

public void FrontMusic() {
if (--currentListItem >= mMusicList.size()) {
currentListItem = 0;
}else {
playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
}
}

//绑定音乐
void musicList(){
File home=new File(MUSIC_PATH);
if(home.listFiles(new MusicFilter()).length>0){
for(File file:home.listFiles(new MusicFilter())){
mMusicList.add(file.getName());
}
ArrayAdapter<String> musicList=new ArrayAdapter<String>(Music.this,R.layout.c1, mMusicList);
setListAdapter(musicList);
}
}
}

/* 过滤文件类型 */
class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
//这里还可以设置其他格式的音乐文件
return (name.endsWith(".mp3"));
}
}

以下是music.xml界面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_toRightOf="@+id/imageButton1"
android:src="@drawable/b2" />

<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton2"
android:layout_toRightOf="@+id/imageButton2"
android:src="@drawable/b3" />

<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton3"
android:layout_toRightOf="@+id/imageButton3"
android:src="@drawable/b4" />

<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton4"
android:layout_toRightOf="@+id/imageButton4"
android:src="@drawable/b5" />

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="@drawable/b1" />

<ListView
android:id="@android:id/list"
android:layout_width="320dp"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageButton2"
android:layout_marginTop="14dp" >
</ListView>

</RelativeLayout>


下面是c.xml,因为这个文件导致我花了好长的时间,这个文件会被ArrayAdapter()调用......不吐槽了,直接上码

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="无卡" />


最终是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculate"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CalculateActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Music"></activity>
<activity android:name="GetResouceActivity"></activity>
<activity android:name="ClockActivity"></activity>
</application>

</manifest>


手机运行的效果:

android之播放器_java



过程很纠结,坚持!过程问题越多,受教越广,去享受那份纠结吧。。。。