Остановка радиопередачи через 20-30 минут android (8.0 отлично, 9 и 10 остановка через 20 минут и до 7.0 кр sh) - PullRequest
0 голосов
/ 25 апреля 2020

Я занимаюсь разработкой приложения для радио. Я пытаюсь транслировать радио с URL: https://radiokahollavan.radioca.st/stream

Теперь я сделал службу переднего плана, которая отлично работает на 8.0, но на android 9 и 10 останавливаются через 20 минут, я не знал, почему я пытаюсь найти об этом, но что-то, с чем я не работал

, если кто-то может мне помочь с этим, я буду благодарен

это услуга:

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onCreate() {
    super.onCreate();

    setupMediaPlayer();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent != null && intent.getAction() != null) {
        switch (intent.getAction()) {
            case Constant.RADIO_ACTION_PLAY_PAUSE:

                if (isPlaying()) {
                    pause();
                    showNotification();
                    stopForeground(false);
                } else {
                    playRadio();
                    showNotification();
                }

                break;

            case Constant.RADIO_ACTION_STOP:

                hideNotification();
                stopService();

                break;


            default:
                break;
        }
    }


    return super.onStartCommand(intent, flags, startId);
}


public void prepStreamAndPlay()
{
    if(mediaPlayer != null)
    {
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                isLoading = false;
                isLoaded = true;

                playRadio();

                if(runOnStreamPrepared != null)
                    runOnStreamPrepared.run();
            }
        });

        try
        {
            mediaPlayer.setDataSource(Constant.STREAM_URL);
            isLoading = true;
            mediaPlayer.prepareAsync();
        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    else
    {
        setupMediaPlayer();
        prepStreamAndPlay();
    }
}




public void playRadio() {
    if (isLoaded) {
        mediaPlayer.start();
        mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    }
    else
        prepStreamAndPlay();

}

public void setupMediaPlayer() {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {

            Toast.makeText(RadioService.this, "Une erreur s'est produite", Toast.LENGTH_LONG).show();
            Log.d("RADIO_DEBUG", "ERROR");
            LocalBroadcastManager.getInstance(RadioService.this).sendBroadcast(new Intent(Constant.FAILED_PLAY_RADIO));
            return false;
        }
    });

}



public void pause() {
    if (mediaPlayer != null && mediaPlayer.isPlaying())
        mediaPlayer.pause();
}

public boolean isPlaying() {
    return mediaPlayer != null && (mediaPlayer.isPlaying() || isLoading);
}

public boolean isLoading()
{
    return isLoading;
}

public boolean isLoaded()
{
    return isLoaded;
}


public void showNotification() {

    Intent intentPlayPause = new Intent(this, RadioService.class);
    intentPlayPause.setAction(Constant.RADIO_ACTION_PLAY_PAUSE);

    PendingIntent pendingPlayPause = PendingIntent.getService(this, 0, intentPlayPause, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent intentClose = new Intent(this, RadioService.class);
    intentClose.setAction(Constant.RADIO_ACTION_STOP);

    PendingIntent pendingIntentClose = PendingIntent.getService(this, 0, intentClose, PendingIntent.FLAG_ONE_SHOT);

    Intent intentOpen = new Intent(this, MainActivity.class);
    intentOpen.putExtra("isPlaying", isPlaying());

    PendingIntent pendingLaunch = PendingIntent.getActivity(this, 0, intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);

    String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("רדיו כחול לבן מתנגן...")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2254, notification);
}

public void hideNotification() {
    stopForeground(true);
}


public void stopService() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;

        stopSelf();
    }
}

public void setRunOnStreamPrepared(final Runnable runOnStreamPrepared) {
    this.runOnStreamPrepared = runOnStreamPrepared;
}

}

...