Stop media player android, when receiving calls or another application uses the audio

3

I hope I have been specific, the detail is that I am doing an audio streaming with media player, and I would like to know if there is any way to know if another application is using the audio from the phone, and if so, I interrupt the service ( the one I use for streaming). I have the following class:

public class MyServicioRadio extends Service {
int countdown=0;
int countdown1=0;
private MediaPlayer mediaPlayer;
public static Boolean bandera;
public MyServicioRadio() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Log.i(getClass().getSimpleName(), "Intent recibido");
    stopPlaying();
    mediaPlayer = new MediaPlayer();

    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource("http://s13.myradiostream.com:35446/");
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });

    } catch (IOException e) {
        e.printStackTrace();
    }
    showForegroundNotification();
    /*if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
    } else {
        mediaPlayer.start();
    }*/
    return START_STICKY;
}

@Override
public void onDestroy() {
    //App.getInstance().cancelPendingRequests(getClass().getSimpleName());
    super.onDestroy();
    stopPlaying();

    bandera=false;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void showForegroundNotification() {

    // Se construye la notificación
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Futbol Mania")
            .setContentText("Si Si Si");

// Crear Intent para iniciar una actividad al presionar la notificación
        Intent notificationIntent = new Intent(this, MenuPrincipal.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder.setContentIntent(pendingIntent);

// Poner en primer plano
        startForeground(1, builder.build());

// Acciones de proceso...
    }
    private void stopPlaying() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

I hope you explained to me

    
asked by Luis Bautista 25.08.2016 в 23:29
source

1 answer

2

When your app accesses the background you can pause the media player, overwriting the onPause() method:

    @Override
    protected void onPause{
      super.onPause();
      mediaplayer.pause();
   }

This method is executed in your application when another application is activated or in this case when you receive a phone call.

    
answered by 26.08.2016 в 00:00