使用Service播放音乐

使用Service播放音乐

res/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:layout_marginBottom="20dp" android:textColor="@android:color/white" android:textStyle="bold" android:text="使用Service播放音乐" android:gravity="center"/> <Button android:id="@+id/playButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Play Music"/> <Button android:id="@+id/pauseButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Pause Music"/> <Button android:id="@+id/stopButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop Music"/> <Button android:id="@+id/stopService" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop Service"/> </LinearLayout>


MusicService.java

package com.zeph.android.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicService extends Service { private MediaPlayer mMediaPlayer; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); mMediaPlayer = MediaPlayer.create(this, R.raw.music01); } @Override public void onDestroy() { super.onDestroy(); mMediaPlayer.stop(); mMediaPlayer.release(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); int operate = intent.getIntExtra("operate", 3); switch (operate) { case 0: if (!mMediaPlayer.isPlaying()) { mMediaPlayer.start(); } break; case 1: if (mMediaPlayer.isPlaying()) { mMediaPlayer.pause(); } break; case 2: if (mMediaPlayer.isPlaying()) { mMediaPlayer.stop(); mMediaPlayer = MediaPlayer.create(this, R.raw.music01); } break; default: break; } } }


ServiceTestActivity.java

package com.zeph.android.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ServiceTestActivity extends Activity { private Button playButton; private Button pauseButton; private Button stopButton; private Button stopService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); playButton = (Button) findViewById(R.id.playButton); pauseButton = (Button) findViewById(R.id.pauseButton); stopButton = (Button) findViewById(R.id.stopButton); stopService = (Button) findViewById(R.id.stopService); playButton.setOnClickListener(new ButtonOnClickListener()); pauseButton.setOnClickListener(new ButtonOnClickListener()); stopButton.setOnClickListener(new ButtonOnClickListener()); stopService.setOnClickListener(new ButtonOnClickListener()); } public class ButtonOnClickListener implements OnClickListener { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), MusicService.class); if (view == playButton) { intent.putExtra("operate", 0); startService(intent); } else if (view == pauseButton) { intent.putExtra("operate", 1); startService(intent); } else if (view == stopButton) { intent.putExtra("operate", 2); startService(intent); } else if (view == stopService) { stopService(intent); } } } }