Optimisation MediaPlayerService
This commit is contained in:
@@ -26,6 +26,7 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
@@ -150,6 +151,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
return super.onCreateOptionsMenu(menu);
|
return super.onCreateOptionsMenu(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
@SuppressLint("UseCompatLoadingForDrawables")
|
@SuppressLint("UseCompatLoadingForDrawables")
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||||
@@ -157,8 +159,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
switch (id){
|
switch (id){
|
||||||
case R.id.refresh:
|
case R.id.refresh:
|
||||||
web.clearCache(true);
|
web.clearCache(true);
|
||||||
web.reload();
|
reStart();
|
||||||
Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
|
//web.reload();
|
||||||
|
//Toast.makeText(this, "Rechargement...", Toast.LENGTH_SHORT).show();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case R.id.exit:
|
case R.id.exit:
|
||||||
@@ -177,7 +180,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
case R.id.live:
|
case R.id.live:
|
||||||
lectureAudio("https://live.radiomercure.fr/on-air/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:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
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
|
//Binding this Client to the AudioPlayer Service
|
||||||
private ServiceConnection serviceConnection = new ServiceConnection() {
|
private ServiceConnection serviceConnection = new ServiceConnection() {
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||||
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
|
MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
|
||||||
player = binder.getService();
|
player = binder.getService();
|
||||||
serviceBound = true;
|
serviceBound = true;
|
||||||
|
|
||||||
Toast.makeText(MainActivity.this, "Vous écoutez Radio Mercure en direct", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -212,6 +222,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
@SuppressLint("UseCompatLoadingForDrawables")
|
@SuppressLint("UseCompatLoadingForDrawables")
|
||||||
private void lectureAudio(String chemin) {
|
private void lectureAudio(String chemin) {
|
||||||
//Check is service is active
|
//Check is service is active
|
||||||
@@ -220,9 +231,12 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
playerIntent.putExtra("media",chemin);
|
playerIntent.putExtra("media",chemin);
|
||||||
startService(playerIntent);
|
startService(playerIntent);
|
||||||
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||||
} else {
|
Toast.makeText(this, "Lecture", Toast.LENGTH_SHORT).show();
|
||||||
//Toast.makeText(player, "OK", Toast.LENGTH_SHORT).show();
|
} else {
|
||||||
|
onDestroy();
|
||||||
|
reStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
|
|||||||
|
|
||||||
AudioManager.OnAudioFocusChangeListener {
|
AudioManager.OnAudioFocusChangeListener {
|
||||||
|
|
||||||
|
private AudioManager audioManager;
|
||||||
|
|
||||||
// Binder given to clients
|
// Binder given to clients
|
||||||
private final IBinder iBinder = new LocalBinder();
|
private final IBinder iBinder = new LocalBinder();
|
||||||
|
|
||||||
@@ -89,8 +91,49 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAudioFocusChange(int focusChange) {
|
public void onAudioFocusChange(int focusState) {
|
||||||
//Invoked when the audio focus of the system is updated.
|
//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 {
|
public class LocalBinder extends Binder {
|
||||||
@@ -166,10 +209,10 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Request audio focus
|
//Request audio focus
|
||||||
/* if (requestAudioFocus() == false) {
|
if (requestAudioFocus() == false) {
|
||||||
//Could not gain focus
|
//Could not gain focus
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}*/
|
}
|
||||||
|
|
||||||
if (fichierMedia != null && fichierMedia != "")
|
if (fichierMedia != null && fichierMedia != "")
|
||||||
initMediaPlayer();
|
initMediaPlayer();
|
||||||
@@ -184,7 +227,7 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
|
|||||||
stopMedia();
|
stopMedia();
|
||||||
mediaPlayer.release();
|
mediaPlayer.release();
|
||||||
}
|
}
|
||||||
//removeAudioFocus();
|
removeAudioFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Gestion des appels tel entrant : suspension de la lecture
|
//Gestion des appels tel entrant : suspension de la lecture
|
||||||
@@ -226,4 +269,5 @@ public class MediaPlayerService extends Service implements MediaPlayer.OnComplet
|
|||||||
telephonyManager.listen(phoneStateListener,
|
telephonyManager.listen(phoneStateListener,
|
||||||
PhoneStateListener.LISTEN_CALL_STATE);
|
PhoneStateListener.LISTEN_CALL_STATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user