androidde uygulama destroy edilse bile arkaplanda çalışan mediaplayer
27.09.2020 - 03:33
androidde uygulama destroy edilse bile arkaplanda mediaplayer çalmasını istiyorum. uygulama arkaplanda çalışıyor ve bildirim üzerinden play pause işlemleri yapılabiliyor fakat destroy edilince kapanıyor arkaplanda çalısmıyor. bunun için ne yapabilirim
ikinci sorum ise geri tuşuna bastığım anda müzik çalmaya devam ediyor ve telefonun ana ekranına dönüyor geçmiş uygulamalardan tekrar açtığımda ise çalan müzik main activityde duruyor gibi görünüyor aslında pause tuşu görünmesi gerekirken play tuşu görünüyor ve playe bastığımda müzik 2. kez çalmaya başlıyor yani aynı 2 müzik üst üste biniyor. kısacası geri tuşuna bastığımda müzik durmuyor fakat buttonlarımın play ve pause işlevi 1 adım geri gidiyor.
public class MainActivity extends AppCompatActivity implements Playable{
ImageButton playButton;
ImageButton closeButton;
String Stream= "http://stream.radioreklama.bg:80/radio1rock128";
MediaPlayer mediaPlayer;
NotificationManager notificationManager;
List<Track> tracks;
Boolean prepared=false;
Boolean started=false;
int position=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton= (ImageButton) findViewById(R.id.playButton);
playButton.setImageResource(R.drawable.play);
playButton.setEnabled(false);
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(Stream);
populateTracks();
//Notification denemesi..
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
createChannel();
registerReceiver(broadcastReceiver,new IntentFilter("TRACKS_TRACKS"));
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
}
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//CreateNotification.createNotification(MainActivity.this,tracks.get(1), R.drawable.pause,1,tracks.size() -1);
//mediaPlayer.start();
if (!mediaPlayer.isPlaying()){
// mediaPlayer.pause();
//playButton.setBackgroundResource(R.drawable.play);
startService(new Intent(MainActivity.this, OnClearFromRecentService.class));
onNPlay();
}else {
// mediaPlayer.start();
//playButton.setBackgroundResource(R.drawable.pause);
onNPause();
stopService(new Intent(MainActivity.this, OnClearFromRecentService.class));
}
}
});
}
private void createChannel() {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel=new NotificationChannel(CreateNotification.CHANNEL_ID,
"denela",NotificationManager.IMPORTANCE_LOW);
notificationManager=getSystemService(NotificationManager.class);
if(notificationManager!=null){
notificationManager.createNotificationChannel(channel);
}
}
}
private void populateTracks(){
tracks=new ArrayList<>();
tracks.add(new Track("Radio Feelo","Live is in spiker",R.drawable.radyoamblem));
tracks.add(new Track("Radio Feelo","Live is in spiker",R.drawable.radyoamblem));
}
class PlayerTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared=true;
}catch(IOException e) {
e.printStackTrace();
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
playButton.setEnabled(true);
}
}
BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action =intent.getExtras().getString("actionname");
switch (action){
case CreateNotification.ACTION_PLAY:
if (mediaPlayer.isPlaying()){
onNPause();
}else {
onNPlay();
}
break;
case CreateNotification.ACTION_CLOSE:
onNClose();
break;
}
}
};
@Override
public void onNPlay() {
mediaPlayer.start();
//started=true;
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.pause,position,tracks.size()-1);
playButton.setImageResource(R.drawable.pause);
if(!mediaPlayer.isPlaying())
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
started=true;
}
});
}
@Override
public void onNPause() {
mediaPlayer.pause();
//started=false;
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.play,position,tracks.size()-1);
playButton.setImageResource(R.drawable.play);
if(mediaPlayer.isPlaying())
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
started=false;
}
});
}
@Override
public void onNClose() {
//mediaPlayer.release();
CreateNotification.createNotification(MainActivity.this, tracks.get(position),
R.drawable.close,position,tracks.size()-1);
notificationManager.cancelAll();
mediaPlayer.release();
unregisterReceiver(broadcastReceiver);
this.finish();
}
@Override
protected void onResume() {
super.onResume();
/*
if(started){
mediaPlayer.start();
}
*/
}
@Override
protected void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
notificationManager.cancelAll();
}
/*
if(prepared){
mediaPlayer.release();
}
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
notificationManager.cancelAll();
}
unregisterReceiver(broadcastReceiver);
*/
}
10
Görüntülenme
0 Beğeni