Browse Source

Gestion des appels téléphoniques entrant

pull/1/head
Samuel Vermeulen 6 months ago
parent
commit
3b5c49e078
  1. 2
      app/src/main/AndroidManifest.xml
  2. 45
      app/src/main/java/fr/svpro/radiomercure/MediaPlayerService.java

2
app/src/main/AndroidManifest.xml

@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"

45
app/src/main/java/fr/svpro/radiomercure/MediaPlayerService.java

@ -1,11 +1,14 @@
package fr.svpro.radiomercure;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.IOException;
@ -96,7 +99,7 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setScreenOnWhilePlaying(false);
//mediaPlayer.setScreenOnWhilePlaying(true);
//Reset so that the MediaPlayer is not pointing to another data source
mediaPlayer.reset();
@ -172,5 +175,45 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
//removeAudioFocus();
}
//Gestion des appels tel entrant : suspension de la lecture
private boolean ongoingCall = false;
private PhoneStateListener phoneStateListener;
private TelephonyManager telephonyManager;
private void callStateListener() {
// Get the telephony manager
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//Starting listening for PhoneState changes
phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//if at least one call exists or the phone is ringing
//pause the MediaPlayer
case TelephonyManager.CALL_STATE_OFFHOOK:
case TelephonyManager.CALL_STATE_RINGING:
if (mediaPlayer != null) {
pauseMedia();
ongoingCall = true;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
// Phone idle. Start playing.
if (mediaPlayer != null) {
if (ongoingCall) {
ongoingCall = false;
resumeMedia();
}
}
break;
}
}
};
// Register the listener with the telephony manager
// Listen for changes to the device call state.
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}
Loading…
Cancel
Save