伙计们,我正在我的应用程序中处理音频模块,所以我创建了带有服务的简单音频演示。
它的工作达到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
。
请大家帮帮我。我最近两天收到了这个错误。如果我错了,请纠正我。
坦克请花点时间回答我的问题。