<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:gravity="center" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pin"
/>
<ProgressBar
android:id="@+id/mProgressBar"
android:layout_marginTop="5dp"
android:layout_width="250dp"
android:layout_height="10dp"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal" >
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播 放"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂 停"/>
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退 出"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
package com.example.mp3player;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener{
ActionBar mActionBar;
Button pause,play,exit;
ProgressBar mProgressBar;
static Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBar=getActionBar();
mActionBar.hide();
pause=(Button) findViewById(R.id.pause);
play=(Button) findViewById(R.id.play);
exit=(Button) findViewById(R.id.exit);
mProgressBar=(ProgressBar) findViewById(R.id.mProgressBar);
pause.setOnClickListener(this);
play.setOnClickListener(this);
exit.setOnClickListener(this);
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle mBundle=new Bundle();
mBundle=msg.getData();
mProgressBar.setProgress(mBundle.getInt("progress",0));
}
};
}
@Override
public void onClick(View v) {
int p=0;
Intent intent=new Intent("com.example.mp3player.musicService");
if(v==play)
p=1;
else if(v==pause)
p=2;
else if(v==exit)
{
stopService(intent);
this.finish();
}
Bundle bundle=new Bundle();
bundle.putInt("op", p);
intent.putExtras(bundle);
startService(intent);
}
}
package com.example.mp3player;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
public class MusicService extends Service {
private MediaPlayer mediaPlayer=null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
if(mediaPlayer==null)
{
mediaPlayer=MediaPlayer.create(this, R.raw.pin);
mediaPlayer.setLooping(true);
}
}
@Override
public void onDestroy(){
if(mediaPlayer!=null)
{
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public void onStart(Intent intent,int id){
if(intent==null) return ;
int p=intent.getIntExtra("op", 1);
switch(p)
{
case 1:
play();break;
case 2:
pause();break;
}
}
public void play(){
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
Thread thread=new Thread(new Runnable(){
@Override
public void run() {
while(mediaPlayer!=null&&mediaPlayer.isPlaying())
{
Bundle bundle=new Bundle();
bundle.putInt("progress", mediaPlayer.getCurrentPosition()*100/mediaPlayer.getDuration());
Message message=new Message();
message.setData(bundle);
MainActivity.handler.sendMessage(message);
}
}});
thread.start();
}
}
public void pause(){
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
}
}