2011-02-14 111 views
0

我正在寫一個使用services.i的音樂播放器示例,我可以通過使用其他應用程序中的活動來啓動和停止服務。但現在我需要暫停播放器。因爲我可以這樣做,因爲其是在服務沒有的onPause方法暫停服務

回答

0

Media Player

利用:暫停();

重新啓動:準備() 的start()

**LocalService.java**              public class LocalService extends Service { 
private NotificationManager mNM; 
MediaPlayer mMediaPlayer; 
private final IBinder mBinder = new LocalBinder(); 
public class LocalBinder extends Binder { 
    LocalService getService() { 
     return LocalService.this; 
    } 
} 

@Override 
public void onCreate() {   
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
} 

@Override 
public void onDestroy() { 
    mNM.cancel(R.string.local_service_started);  
    Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show(); 
} 

public void startMp3Player() { 
    mMediaPlayer = MediaPlayer.create(getApplicationContext(), 
      R.raw.abc); 
    mMediaPlayer.start(); 
} 

public void mp3Stop() { 
    mMediaPlayer.stop(); 
    mMediaPlayer.release(); 
} 

@Override 
public IBinder onBind(Intent intent) {    
    return mBinder; 
} 

}

               **LocalServiceBinding.JAVA**                 public class LocalServiceBinding extends Activity { 
private boolean mIsBound; 
private LocalService mBoundService; 
Button mPlayButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.local_service_binding); 
    Button button = (Button)findViewById(R.id.bind); 
    button.setOnClickListener(mBindListener); 
    button = (Button)findViewById(R.id.unbind); 
    button.setOnClickListener(mUnbindListener); 
    mPlayButton = (Button)findViewById(R.id.play); 
    mPlayButton.setOnClickListener(mPlayListener); 
    mPlayButton.setText("Play"); 
    mPlayButton.setEnabled(false); 
} 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mBoundService = ((LocalService.LocalBinder)service).getService();    
     Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected, 
       Toast.LENGTH_SHORT).show(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mBoundService = null; 
     Toast.makeText(LocalServiceBinding.this, R.string.local_service_disconnected, 
       Toast.LENGTH_SHORT).show(); 
    } 
}; 

private OnClickListener mBindListener = new OnClickListener() { 
    public void onClick(View v) { 
     bindService(new Intent(LocalServiceBinding.this, 
       LocalService.class), mConnection, Context.BIND_AUTO_CREATE); 
     mIsBound = true; 
     mPlayButton.setEnabled(true); 

    } 
}; 

private OnClickListener mPlayListener = new OnClickListener() { 
    public void onClick(View v) { 
      if(mPlayButton.getText() == "Play") 
      { 
       mBoundService.startMp3Player(); 
       mPlayButton.setText("Stop"); 
      } 
      else 
      { 
       mBoundService.mp3Stop(); 
       mPlayButton.setText("Play"); 
      }    
    } 
}; 

private OnClickListener mUnbindListener = new OnClickListener() { 
    public void onClick(View v) { 
     if (mIsBound) { 
      unbindService(mConnection); 
      mIsBound = false; 
      mPlayButton.setEnabled(false); 
     } 
    } 
}; 

}

+0

這並不適用,因爲我無法訪問媒體播放器對象的活動,因爲它是在另一個應用中 – Durga