布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入音乐的路径:" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/bofang"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/zhanting"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停" />
<Button
android:id="@+id/chongbo"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播" />
<Button
android:id="@+id/tingzhi"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
</LinearLayout>
</LinearLayout>
类文件:
package com.example.mp3;
import java.io.File;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText et;
private Button bofang, zhanting, chongbo, tingzhi;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) this.findViewById(R.id.et);
bofang = (Button) this.findViewById(R.id.bofang);
zhanting = (Button) this.findViewById(R.id.zhanting);
chongbo = (Button) this.findViewById(R.id.chongbo);
tingzhi = (Button) this.findViewById(R.id.tingzhi);
bofang.setOnClickListener(this);
zhanting.setOnClickListener(this);
chongbo.setOnClickListener(this);
tingzhi.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bofang:
bofang();
break;
case R.id.zhanting:
zhanting();
break;
case R.id.chongbo:
chongbo();
break;
case R.id.tingzhi:
tingzhi();
break;
}
}
private void zhanting() {
if ("继续".equals(et.getText().toString().trim())) {
mediaPlayer.start();
et.setText("暂停");
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
et.setText("继续");
}
}
// 重新播放
private void chongbo() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);//回到播放的开始
return;
} else {
bofang();
}
}
// 停止播放
private void tingzhi() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();//释放
mediaPlayer = null;
bofang.setEnabled(true);
}
}
// 播放音乐
private void bofang() {
String path = et.getText().toString().trim();
File file = new File(path);
if (file.exists() && file.length() > 0) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置多媒体的类型
mediaPlayer.setDataSource(path);//设置资源路径
mediaPlayer.prepare();//准备
mediaPlayer.start();//开始
// 播放完后调用的方法
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
bofang.setEnabled(true);
}
});
// 播放异常调用的方法
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
bofang.setEnabled(true);
return false;
}
});
bofang.setEnabled(false);
} catch (Exception e) {
Toast.makeText(this, "播放失败", 0).show();
e.printStackTrace();
}
} else {
Toast.makeText(this, "音频文件不存在", 0).show();
}
}
}