Problem when using the AudioManager if other app open

2

Good morning everyone,

I have an application that plays a radio in streaming 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 hangs up, it automatically switches itself on again. Up to here everything perfect.

The problem comes when you open an application that uses sound, such as YouTube. When the Youtube video starts to play, the radio stops all perfect, but when I stop playing the video, the radio does not play until the Youtube application is completely closed. I have to go to the tasks in the background and close it so that it automatically plays again.

On the other hand, I think it has something to do with the same problem, is that by playing the radio, I open a web browser and if I play something from the browser, the radio and the browser playback are heard 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.

    
asked by Natlum 21.12.2016 в 12:41
source

1 answer

3

You must add this:

case AudioManager.AUDIOFOCUS_GAIN: 
    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 Reiniciado", Toast.LENGTH_LONG).show()
    
answered by 22.12.2016 / 17:02
source