我正在爲我的應用程序中的音頻模塊工作,所以我創建了簡單的音頻演示與服務。媒體播放器中停止服務後音頻不播放
它的工作可達Play
- Pause
- Playing in Background
但問題很奇怪。
簡單而推出的應用程序,我已經添加了兩個按鈕像
Play/Pause
(會改變按標題狀態)stop
(停止服務)
現在我說我的play-pause & Stop
工作正常,但當我嘗試播放音頻後點擊stop
它的原因在Start
Error : 05-01 13:37:42.671: E/start(8096): java.lang.IllegalStateException
問題Audio_Activity.java
public class Audio_Activity extends Activity {
private static final String TAG = "ServicesDemo";
public static Button play_pause;
public static Button buttonStop;
Audio_Service ms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ms = new Audio_Service();
play_pause = (Button) findViewById(R.id.play_pause);
buttonStop = (Button) findViewById(R.id.buttonStop);
play_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isMyServiceRunning()) {
ms.play_pause();
} else {
startService(new Intent(Audio_Activity.this,
Audio_Service.class));
play_pause.setText("Pause");
}
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isMyServiceRunning()) {
ms.stop_audio();
stopService(new Intent(Audio_Activity.this,
Audio_Service.class));
play_pause.setText("Play");
} else {
stopService(new Intent(Audio_Activity.this,
Audio_Service.class));
play_pause.setText("Play");
}
}
});
}
public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service: manager
.getRunningServices(Integer.MAX_VALUE)) {
if (Audio_Service.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
}
Audio_Service.java
public class Audio_Service extends Service implements OnErrorListener,
OnCompletionListener, OnPreparedListener {
public static MediaPlayer player = new MediaPlayer();
Audio_Activity ac = new Audio_Activity();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
try {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Log.e("ONdistroy", "" + e);
}
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
try {
Log.e("player_status", "" + player);
String url = "MY_WEB_URL";
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.reset();
player.setDataSource(url);
player.prepareAsync();
player.setOnPreparedListener(this);
player.setOnErrorListener(this);
player.setOnCompletionListener(this);
} catch (Exception e) {
// TODO: handle exception
Log.e("start", "" + e);
}
// player.start();
}
public void play_pause() {
try {
if (player != null) {
if (player.isPlaying()) {
ac.play_pause.setText("Play");
player.pause();
} else {
ac.play_pause.setText("Pause");
player.start();
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("play_pause", "" + e);
}
}
public void stop_audio() {
try {
if (player != null) {
if (player.isPlaying()) {
ac.play_pause.setText("Play");
player.stop();
player.release();
} else {
ac.play_pause.setText("Play");
player.release();
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("stop_service", "" + e);
}
}
public void cleanUp() {
if (player != null) {
player.stop();
player = null;
player.release();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
try {
Log.d("complete", "called...........");
player.stop();
player.release();
player = null;
stopService(new Intent(this, Audio_Service.class));
ac.play_pause.setText("Play");
} catch (Exception e) {
// TODO: handle exception
Log.e("complete", "" + e);
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
try {
player.stop();
player.release();
player = null;
// mp.reset();
ac.play_pause.setText("Play");
} catch (Exception e) {
// TODO: handle exception
Log.e("onError", "" + e);
}
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
try {
player.start();
} catch (Exception e) {
// TODO: handle exception
Log.e("onPrepare", "" + e);
}
}
}
我已經添加下面的東西Manifest
文件
<uses-permission android:name="android.permission.INTERNET"/>
<service
android:name=".Audio_Service"
android:enabled="true" />
更新
我知道我可以在理想狀態下播放音頻的,我必須設置reset();
但不知道在哪裏。因爲如果我設置了onDestroy;
或cleanUp()
或stop_audio()
或onStart()
然後再增加一個錯誤,那就是NullpointerException
。
請大家幫幫我。這個錯誤是我最近兩天得到的。 糾正我,如果我錯了。
坦克需要你的時間在我的問題。
我有打電話給我服務'startService(新意圖(Audio_Activity.this,Audio_Service.class));''中點擊play_pause'事件 – 2013-05-01 09:17:18
但你自己創建新的使用實例,您使用與此實例communicatate常規方法調用。這不是如何使用服務。在調用startService()之後,您需要讓OS自己創建實例。您需要等待連接到srevice才能建立,並且您需要使用活頁夾與它進行通信。請參閱鏈接查看更多細節。 – selalerer 2013-05-01 10:22:58
感謝您的鏈接。這很好。但我解決了我的問題,我必須在'stop_audio()'中添加'player = null;'。 – 2013-05-01 10:34:41