Good morning everyone,
I have an application that plays a streaming radio through a service. The service works perfectly, I can close the application and continue listening to the radio. Even if they call me on the phone, the radio stops and when the call is hung up, it automatically goes back on. So far everything perfect.
The problem comes when an application that uses sound is opened, such as YouTube. When you start playing the youtube video, the radio stops everything perfect, but when I stop playing the video, the radio doesn't play until you close the youtube app completely. I have to go to background tasks and close it for it to autoplay again.
On the other hand, I think it has something to do with the same problem, is that while playing the radio, I open a web browser and if I play something from the browser, the radio and the browser play at the same time. And I would like it to happen the same as with calls.
The service is as follows:
public class RadioService extends Service implements AudioManager.OnAudioFocusChangeListener {
private final static String RADIO_STATION_URL = "http://URL";
private static RadioOnline radio;
private NotificationPanel nPanel;
private static boolean value = false;
private AudioManager mAudioManager;
@Override
public void onCreate() {
try {
radio = new RadioOnline(RADIO_STATION_URL, this);
mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
radio.initializeMediaPlayer();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(this, "Servicio Creado", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if (radio.isNetworkConnected(this)){
nPanel = new NotificationPanel(this, radio);
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_LONG).show();
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
nPanel.notificationCancel();
try {
if (radio.stopPlaying()){
nPanel.notificationCancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}finally {
nPanel.notificationCancel();
}
Toast.makeText(this, "Servicio Detenido", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange)
{
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
try {
if (radio.isNetworkConnected(this)){
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
if(radio.getPlayer().isPlaying()) {
try {
radio.stopPlaying();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
}
Thank you very much for the help, I am learning a lot thanks to this community.
You must add this: