Browse Source

Optimisation MediaPlayerService

pull/1/head
Samuel Vermeulen 5 months ago
parent
commit
2502b088e4
  1. 28
      app/src/main/java/fr/svpro/radiomercure/MainActivity.java
  2. 52
      app/src/main/java/fr/svpro/radiomercure/MediaPlayerService.java

28
app/src/main/java/fr/svpro/radiomercure/MainActivity.java

@ -26,6 +26,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@ -150,6 +151,7 @@ public class MainActivity extends AppCompatActivity {
return super.onCreateOptionsMenu(menu);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
@ -157,8 +159,9 @@ public class MainActivity extends AppCompatActivity {
switch (id){
case R.id.refresh:
web.clearCache(true);
web.reload();
Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
reStart();
//web.reload();
//Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
@ -177,7 +180,9 @@ public class MainActivity extends AppCompatActivity {
case R.id.live:
lectureAudio("https://live.radiomercure.fr/on-air/live");
return true;
//Toast.makeText(this, "Vous écoutez Radio Mercure en direct", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
@ -194,16 +199,21 @@ public class MainActivity extends AppCompatActivity {
}
}
private void reStart() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
//Binding this Client to the AudioPlayer Service
private ServiceConnection serviceConnection = new ServiceConnection() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
player = binder.getService();
serviceBound = true;
Toast.makeText(MainActivity.this, "Vous écoutez Radio Mercure en direct", Toast.LENGTH_SHORT).show();
}
@Override
@ -212,6 +222,7 @@ public class MainActivity extends AppCompatActivity {
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("UseCompatLoadingForDrawables")
private void lectureAudio(String chemin) {
//Check is service is active
@ -220,9 +231,12 @@ public class MainActivity extends AppCompatActivity {
playerIntent.putExtra("media",chemin);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Toast.makeText(player, "OK", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Lecture", Toast.LENGTH_SHORT).show();
} else {
onDestroy();
reStart();
}
}
@Override

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

@ -29,6 +29,8 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
AudioManager.OnAudioFocusChangeListener {
private AudioManager audioManager;
// Binder given to clients
private final IBinder iBinder = new LocalBinder();
@ -89,8 +91,49 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
}
@Override
public void onAudioFocusChange(int focusChange) {
public void onAudioFocusChange(int focusState) {
//Invoked when the audio focus of the system is updated.
switch (focusState) {
case AudioManager.AUDIOFOCUS_GAIN:
// resume playback
if (mediaPlayer == null) initMediaPlayer();
else if (!mediaPlayer.isPlaying()) mediaPlayer.start();
mediaPlayer.setVolume(1.0f, 1.0f);
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and release media player
if (mediaPlayer.isPlaying()) mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
if (mediaPlayer.isPlaying()) mediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mediaPlayer.isPlaying()) mediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}
private boolean requestAudioFocus() {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//Focus gained
return true;
}
//Could not gain focus
return false;
}
private boolean removeAudioFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
audioManager.abandonAudioFocus(this);
}
public class LocalBinder extends Binder {
@ -166,10 +209,10 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
}
//Request audio focus
/* if (requestAudioFocus() == false) {
if (requestAudioFocus() == false) {
//Could not gain focus
stopSelf();
}*/
}
if (fichierMedia != null && fichierMedia != "")
initMediaPlayer();
@ -184,7 +227,7 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
stopMedia();
mediaPlayer.release();
}
//removeAudioFocus();
removeAudioFocus();
}
//Gestion des appels tel entrant : suspension de la lecture
@ -226,4 +269,5 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}
Loading…
Cancel
Save