Guys I am working on Audio module in my app so i have created on simple Audio demo with service.
Its working up to Play
- Pause
- Playing in Background
but issue is strange.
Simply while launch app i have added two Buttons like
Play/Pause
(Will change caption according state)stop
(To stop service)
Now as i said my play-pause & Stop
are working perfectly but when i try to play again audio after the click on stop
its cause the problem in Start
Error : 05-01 13:37:42.671: E/start(8096): java.lang.IllegalStateException
- Reference Link for Media Player State
- Reference Link for play audio in background
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);
}
}
}
I have added below things in Manifest
file
<uses-permission android:name="android.permission.INTERNET"/>
<service
android:name=".Audio_Service"
android:enabled="true" />
Update
I know that i can play audio in ideal state for that i have to set reset();
but don't know where.Because if i set in onDestroy;
or cleanUp()
or stop_audio()
or onStart()
then its adding one more error and that is NullpointerException
.
Please guys help me. this error i am getting form last two days. Correct me if i am wrong.
Tanks for take your time on my question.