media player
MusicActivity
package com.audio;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MusicActivity extends Activity {
/* 声明一个 ImageButton,TextView,MediaPlayer变量 */
private ImageButton mButton01, mButton02, mButton03;
private TextView mTextView01;
private MediaPlayer mMediaPlayer01;
/* 声明一个Flag作为确认音乐是否暂停的变量并默认为false */
private boolean bIsPaused = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 通过findViewById构造器创建TextView与ImageView对象 */
mButton01 = (ImageButton) findViewById(R.id.myButton1);
mButton02 = (ImageButton) findViewById(R.id.myButton2);
mButton03 = (ImageButton) findViewById(R.id.myButton3);
mTextView01 = (TextView) findViewById(R.id.myTextView1);
/* OnCreate时创建MediaPlayer对象 */
mMediaPlayer01 = new MediaPlayer();
/* 将音乐以Import的方式存储在res/raw/always.mp3 */
mMediaPlayer01 = MediaPlayer.create(MusicActivity.this, R.raw.big);
/* 运行播放音乐的按钮 */
mButton01.setOnClickListener(new ImageButton.OnClickListener(){
@Override
/* 覆盖Onclick事件 */
public void onClick(View v) {
try{
if( mMediaPlayer01 != null ){
mMediaPlayer01.stop();
}
/*
* 在MediaPlayer取得播放资源与stop()之后
* 要准备Playback的状态前一定要使用MediaPlayer.prepare()
*/
mMediaPlayer01.prepare();
/* 开始或回复播放 */
mMediaPlayer01.start();
/* 改变TextView为开始播放状态 */
mTextView01.setText(R.string.str_start);
}catch(Exception e){
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
/* 停止播放 */
mButton02.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try{
if( mMediaPlayer01 != null ){
/* 停止播放 */
mMediaPlayer01.stop();
/* 改变TextView为停止播放状态 */
mTextView01.setText(R.string.str_close);
}
}catch(Exception e){
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
/* 暂停播放 */
mButton03.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try{
if( mMediaPlayer01 != null ){
/* 是否为暂停状态=否 */
if(bIsPaused == false){
/* 暂停播放 */
mMediaPlayer01.pause();
/* 设置Flag为true表示 Player状态为暂停 */
bIsPaused = true;
/* 改变TextView为暂停播放 */
mTextView01.setText(R.string.str_pause);
}else{
/* 回复播出状态 */
mMediaPlayer01.start();
/* 设置Flag为false表示 Player状态为非暂停 */
bIsPaused = false;
/* 改变TextView为暂停播放 */
mTextView01.setText(R.string.str_start);
}
}
}catch(Exception e){
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
/* 当MediaPlayer.OnCompletionLister会运行的Listener */
mMediaPlayer01.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
/* 覆盖文件播出完毕事件 */
public void onCompletion(MediaPlayer mp) {
try{
/*
* 解除资源与MediaPlayer的赋值关系 让资源可以为其它程序利用
*/
mMediaPlayer01.release();
mTextView01.setText(R.string.str_OnCompletionListener);
}catch(Exception e){
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
/* 当MediaPlayer.OnErrorListener会运行的Listener */
mMediaPlayer01.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
/* 覆盖错误处理事件 */
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
try {
/* 发生错误时也解除资源与MediaPlayer的赋值 */
mMediaPlayer01.release();
mTextView01.setText(R.string.str_OnErrorListener);
} catch (Exception e) {
mTextView01.setText(e.toString());
e.printStackTrace();
}
return false;
}
});
}
@Override
/* 覆盖主程序暂停状态事件 */
protected void onPause() {
try{
/* 再主程序暂停时解除资源与MediaPlayer的赋值关系 */
mMediaPlayer01.release();
}catch(Exception e){
mTextView01.setText(e.toString());
e.printStackTrace();
}
super.onPause();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<ImageButton
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
android:layout_below="@+id/myTextView1"
/>
<ImageButton
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"
android:layout_alignTop="@+id/myButton1"
android:layout_toRightOf="@+id/myButton3"
/>
<ImageButton
android:id="@+id/myButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause"
android:layout_alignTop="@+id/myButton1"
android:layout_toRightOf="@+id/myButton1"
/>
"
</RelativeLayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.audio"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".MusicActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
values目录下面:
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="black">#000</drawable>
<drawable name="white">#FFFFFFFF</drawable>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">mp3播放器</string>
<string name="app_name">mp3播放器</string>
<string name="str_start">正在播放音乐</string>
<string name="str_close">停止播放音乐</string>
<string name="str_pause">暂停播放音乐</string>
<string name="str_OnCompletionListener">播放完毕</string>
<string name="str_OnErrorListener">出错</string>
</resources>