AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mediaplayer"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mediaplayer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.mediaplayer.MusicService"> 
            </service>
    </application>
   
</manifest>
===============================================================================
布局文件
<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"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        android:text="我是测试播放器" />
    
    
    <Button 
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="play"
        android:text="播放了"/>
    
    <Button 
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="暂停了"/>
    
</LinearLayout>
====================================================
Activity 页面
package com.example.mediaplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
	private MusicServiceConn conn;
	private IMusicService mService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent service = new Intent(this, MusicService.class);
		startService(service);
		conn = new MusicServiceConn();
		bindService(service, conn, BIND_AUTO_CREATE);
}
	// 连接通道
	class MusicServiceConn implements ServiceConnection {
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
mService = (IMusicService) service;
}
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
}
}
public void play(View v) {
mService.play();
}
public void stop(View v) {
mService.pause();
}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unbindService(conn);
	}
}
============================================================
服务
package com.example.mediaplayer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return new Music();
	}
class Music extends Binder implements IMusicService {
		public void play() {
			// TODO Auto-generated method stub
playMusic();
}
		public void pause() {
			// TODO Auto-generated method stub
			pauseMusic();
		}
}
	public void playMusic() {
		// TODO Auto-generated method stub
		//MediaPlayer player = new MediaPlayer();
	
		mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.xin);
		mediaPlayer.start();
}
	public void pauseMusic() {
		// TODO Auto-generated method stub
		mediaPlayer.pause();
	}
}
=============================
接口文件
package com.example.mediaplayer;
public interface IMusicService {
	public  void play();
	
	public  void pause();
}
原文:http://www.cnblogs.com/yangjies145/p/6901545.html