Ajout MediaPlayerService
This commit is contained in:
@@ -4,22 +4,20 @@ import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Dialog;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
import android.os.IBinder;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.CookieManager;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.URLUtil;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
@@ -30,8 +28,6 @@ import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final int WRITE_EXTERNAL_STORAGE_RC = 100;
|
||||
@@ -43,6 +39,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
private TextView tv_version;
|
||||
private static String versionName;
|
||||
private static final String NUMERO_TEL_RADIO = "+33375411456";
|
||||
private MediaPlayerService player;
|
||||
boolean serviceBound = false;
|
||||
|
||||
static {
|
||||
versionName = BuildConfig.VERSION_NAME;
|
||||
@@ -177,20 +175,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
return true;
|
||||
|
||||
case R.id.live:
|
||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
mediaPlayer.setScreenOnWhilePlaying(true);
|
||||
try {
|
||||
mediaPlayer.setDataSource("https://live.radiomercure.fr/on-air/live");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
mediaPlayer.prepare();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mediaPlayer.start();
|
||||
return true;
|
||||
liveRadio("https://live.radiomercure.fr/on-air/live");
|
||||
|
||||
/* Intent playerIntent = new Intent(this, MediaPlayerService.class);
|
||||
startService(playerIntent);
|
||||
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);*/
|
||||
|
||||
return true;
|
||||
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
@@ -207,4 +198,56 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
//Binding this Client to the AudioPlayer Service
|
||||
private ServiceConnection serviceConnection = new ServiceConnection() {
|
||||
@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
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
serviceBound = false;
|
||||
}
|
||||
};
|
||||
|
||||
private void liveRadio(String StreamUrl) {
|
||||
//Check is service is active
|
||||
if (!serviceBound) {
|
||||
Intent playerIntent = new Intent(this, MediaPlayerService.class);
|
||||
playerIntent.putExtra("stream_url",StreamUrl);
|
||||
startService(playerIntent);
|
||||
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
} else {
|
||||
//Toast.makeText(player, "OK", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle savedInstanceState) {
|
||||
savedInstanceState.putBoolean("ServiceState", serviceBound);
|
||||
super.onSaveInstanceState(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
serviceBound = savedInstanceState.getBoolean("ServiceState");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (serviceBound) {
|
||||
unbindService(serviceConnection);
|
||||
//service is active
|
||||
player.stopSelf();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user