今天我们要讲述一个具有控制界面的音乐播放器
1:UI布局layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="迷你音乐播放器"
  />
  
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >
    <ImageButton
      android:id="@+id/play"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play"
    />
    <ImageButton
      android:id="@+id/pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/pause"
    />  
    <ImageButton
      android:id="@+id/stop"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:src="@drawable/stop"
    />
  </LinearLayout>
</LinearLayout>
2:实现功能核心代码MediaActivity.java
package com.android.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class MediaActivity extends Activity implements OnCompletionListener {
    
  private ImageButton play, pause, stop;
  //声明MediaPlayer实例
  private MediaPlayer mp;  
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //设置当前页面布局
                setContentView(R.layout.main);        
                //通过findViewById方法获得按钮实例
                play = (ImageButton)findViewById(R.id.play);
                pause = (ImageButton)findViewById(R.id.pause);
                stop = (ImageButton)findViewById(R.id.stop);
        
                //为按钮添加单击时间监听器
                play.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
        //开始
        play();
      }                
                });
                
                pause.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
        //暂停
        pause();
      }
                });
                
                stop.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
        //停止
        stop();
      }              
                });
                //准备
                setup();
        }
        
        public void onDestory() {
          super.onDestroy();
          if(stop.isEnabled()) {
            stop();
          }
        }

        //准备
  private void setup() {
    //加载播放文件
    loadClip();
    //使播放按钮生效
    play.setEnabled(true);
    //使暂停按钮失效
    pause.setEnabled(false);
    //使停止按钮失效
    stop.setEnabled(false);
  }

  private void loadClip() {
    try{
      //实例化MediaPlayer
      mp = MediaPlayer.create(this, R.raw.test);
      //设置监听器
      mp.setOnCompletionListener(this);
    } catch(Throwable t) {
      error(t);
    }
  }
  
  private void error(Throwable t) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("报错啦!").setMessage(t.toString()).setPositiveButton("确定", null).show();
  }

  public void stop() {
    //停止
    mp.stop();
    //使暂停按钮失效
    pause.setEnabled(false);
    //使停止按钮失效
    stop.setEnabled(false);
    
    try {
      //准备
      mp.prepare();
      //定位到开始
      mp.seekTo(0);
      //使播放按钮生效
      play.setEnabled(true);
    } catch (Throwable t) {
      error(t);
    }
  }

  public void pause() {
    //暂停
    mp.pause();
    //使播放按钮生效
    play.setEnabled(true);
    //使暂停按钮失效
    pause.setEnabled(false);
    //使停止按钮生效
    stop.setEnabled(true);
  }

  public void play() {
    //播放
    mp.start();
    //使播放按钮失效
    play.setEnabled(false);
    //使暂停按钮生效
    pause.setEnabled(true);
    //使停止按钮生效
    stop.setEnabled(true);
  }

  public void onCompletion(MediaPlayer mp) {
    //停止
    stop();
  }
}
3:运行程序如图