<RelativeLayout 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: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" >
<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"
>
</ImageButton>
<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>
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="32dp"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="28dp"
android:src="@drawable/play" />
</RelativeLayout>
Step 4 :主控制程序MediaPlayerDemo.java的实现,代码如下:
package com.example.mymusic;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private ImageButton mb1,mb2,mb3;
private TextView tv;
private MediaPlayer mp;
//声明一个变量判断是否为暂停,默认为false
private boolean isPaused = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过findViewById找到资源
mb1 = (ImageButton)findViewById(R.id.myButton1);
mb2 = (ImageButton)findViewById(R.id.myButton2);
mb3 = (ImageButton)findViewById(R.id.myButton3);
tv = (TextView)findViewById(R.id.myTextView1);
//创建MediaPlayer对象,将raw文件夹下的lovefool.mp3
mp = MediaPlayer.create(this,R.raw.putao);
//增加播放音乐按钮的事件
mb1.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp != null)
/* {
mp.stop();
}
mp.prepare(); */
mp.start();
tv.setText("音乐播放中...");
} catch (Exception e) {
tv.setText("播放发生异常...");
e.printStackTrace();
}
}
});
//停止播放开始
mb2.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp !=null)
{
mp.stop();
tv.setText("音乐停止播放...");
}
} catch (Exception e) {
tv.setText("音乐停止发生异常...");
e.printStackTrace();
}
}
});
//停止播放结束
//暂停播放
mb3.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp !=null)
{
if(isPaused==false)
{
mp.pause();
isPaused=true;
tv.setText("暂停播放!");
}
else if(isPaused==true)
{
mp.start();
isPaused = false;
tv.setText("继续播发!");
}
}
} catch (Exception e) {
tv.setText("发生异常...");
e.printStackTrace();
}
}
});
/* 当MediaPlayer.OnCompletionLister会运行的Listener */
mp.setOnCompletionListener(
new MediaPlayer.OnCompletionListener()
{
// @Override
/*覆盖文件播出完毕事件*/
public void onCompletion(MediaPlayer arg0)
{
try
{
/*解除资源与MediaPlayer的赋值关系
* 让资源可以为其它程序利用*/
mp.release();
/*改变TextView为播放结束*/
tv.setText("音乐播发结束!");
}
catch (Exception e)
{
tv.setText(e.toString());
e.printStackTrace();
}
}
});
/* 当MediaPlayer.OnErrorListener会运行的Listener */
mp.setOnErrorListener(new MediaPlayer.OnErrorListener()
{
@Override
/*覆盖错误处理事件*/
public boolean onError(MediaPlayer arg0, int arg1, int arg2)
{
// TODO Auto-generated method stub
try
{
/*发生错误时也解除资源与MediaPlayer的赋值*/
mp.release();
tv.setText("播放发生异常!");
}
catch (Exception e)
{
tv.setText(e.toString());
e.printStackTrace();
}
return false;
}
});
}
}