/** * MusicService.java * com.androidtest.service * * Function: TODO * * ver date author * ────────────────────────────────── * 2011-5-15 Leon * * Copyright (c) 2011, TNT All Rights Reserved. */ package com.zuiniuwang.service; import java.io.Serializable; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import com.zuiniuwang.*; /** * ClassName:MusicService * Function: TODO ADD FUNCTION * Reason: TODO ADD REASON * * @author Leon * @version * @since Ver 1.1 * @Date 2011-5-15 */ public class NormalMusicService extends Service{ private String TAG = NormalMusicService.class.getSimpleName(); private MediaPlayer myMediaPlayer ; public static final String INTENT_KEY= "action" ; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub Log.v(TAG , TAG+ " onCreate()"); super.onCreate(); if(myMediaPlayer==null){ myMediaPlayer=MediaPlayer.create(this, R.raw.test) ; myMediaPlayer.setLooping(false); } } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub Log.v(TAG , TAG + " onStart()"); super.onStart(intent, startId); if(intent!=null){ MyMediaController mediaControl =(MyMediaController)intent.getSerializableExtra(NormalMusicService.INTENT_KEY); mediaControl.mediaPlayer=myMediaPlayer; mediaControl.execute(); } } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.v(TAG , " onDestroy"); if(myMediaPlayer!=null){ myMediaPlayer.stop(); myMediaPlayer.release(); } } }
package com.zuiniuwang.playeractivity; /** * MusicPlayer.java * com.androidtest.activity * * Function: TODO * * ver date author * ────────────────────────────────── * 2011-5-15 Leon * * Copyright (c) 2011, TNT All Rights Reserved. */ import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.zuiniuwang.MyMediaController; import com.zuiniuwang.R; import com.zuiniuwang.service.NormalMusicService; /** * ClassName:MusicPlayer Function: TODO ADD FUNCTION Reason: TODO ADD REASON * * @author Leon * @version * @since Ver 1.1 * @Date 2011-5-15 */ public class NormalMusicPlayerActivity extends Activity implements OnClickListener { private static final String TAG = NormalMusicPlayerActivity.class.getSimpleName(); private Intent intent ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.music_player_layout); Button playButton = (Button) this.findViewById(R.id.play); playButton.setOnClickListener(this); Button pauseButton =(Button) this.findViewById(R.id.pause); pauseButton.setOnClickListener(this); Button stopButton =(Button)this.findViewById(R.id.stop); stopButton.setOnClickListener(this); Button closeActivityButton =(Button)this.findViewById(R.id.close); closeActivityButton.setOnClickListener(this); Button exitActivityButton =(Button)this.findViewById(R.id.exit); exitActivityButton.setOnClickListener(this); intent = new Intent("com.androidtest.service.mediaplayer.NormalMusicService"); } private void playAction(MyMediaController playType) { Bundle bundle = new Bundle(); bundle.putSerializable(NormalMusicService.INTENT_KEY, playType); intent.putExtras(bundle); NormalMusicPlayerActivity.this.startService(intent); } public void onClick(View view) { // TODO Auto-generated method stub switch (view.getId()) { case R.id.play: Log.d(TAG, "play......."); playAction(MyMediaController.play); break; case R.id.pause: Log.d(TAG, "pause......."); playAction(MyMediaController.pause); break; case R.id.stop: Log.d(TAG, "stop......."); playAction(MyMediaController.stop); break; case R.id.close: Log.d(TAG, "close......."); this.finish(); break; case R.id.exit: Log.d(TAG, "exit......."); stopService(intent); this.finish(); } } }
/** * MyMediaController.java * com.androidtest.service.mediaplayer * * Function: TODO * * ver date author * ────────────────────────────────── * 2011-5-16 Leon * * Copyright (c) 2011, TNT All Rights Reserved. */ package com.zuiniuwang; import java.io.Serializable; import android.media.MediaPlayer; /** * ClassName:MyMediaController * Function: Mediaplayer 的一个控制类,控制播放器的播放 暂停 停止 等动作 * REASON * * @author Leon * @version * @since Ver 1.1 * @Date 2011-5-16 */ public enum MyMediaController implements Serializable { play { @Override public void execute() { if (mediaPlayer != null && !mediaPlayer.isPlaying()) mediaPlayer.start(); // TODO Auto-generated method stub } }, pause { @Override public void execute() { // TODO Auto-generated method stub if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); } } }, stop { @Override public void execute() { // TODO Auto-generated method stub if (mediaPlayer != null) { mediaPlayer.stop(); try { // 在stop后如果要重新Start需要prepare一下 mediaPlayer.prepare(); // 从头播放 mediaPlayer.seekTo(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; public static MediaPlayer mediaPlayer; public abstract void execute(); }
(在F:\java\s服务\51CTO下载-musicservice1\musicservice)