Android Studio Android MediaPlayer在后台播放1分钟后停止

x7rlezfr  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(526)

我正在使用某个服务从MediaPlayer播放音频流,但在后台或屏幕关闭大约一分钟后,它停止播放歌曲。而另一方面,它工作正常,并在应用程序保持打开时完成歌曲。

public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    MediaPlayer mediaPlayer = null;
    String audio_stream;
    Context context;

    public int onStartCommand(Intent intent, int flags, int startId) {
        audio_stream = intent.getStringExtra("url");
        mediaPlayer = new MediaPlayer();

        try {
            mediaPlayer.setDataSource(audio_stream);
        } catch (IOException e) {
            //e.printStackTrace();
        }
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(this);

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

                return false;
            }
        });
        return START_STICKY;
    }

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


    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d("Channel", audio_stream+"he");
        mp.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }else{
            Log.d("channel", "this is the case");
        }

    }



    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mediaPlayer.release();
    }
}

MediaPlayer停止之前,会在记录档中显示4行,分别为

V/MediaPlayer: resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer: resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null

我不明白问题到底出在哪里,我怎样才能克服它。谢谢

jhdbpxl9

jhdbpxl91#

在Android 8.0和更高版本的设备上,您需要将其设置为a foreground service,并关联一个Notification。否则,您的服务将在一分钟后停止。

bis0qfac

bis0qfac2#

是正确的!在Android 8.0和更高版本的设备上必须有一个带有通知的前台服务。我遇到了同样的问题,它是这样解决的。

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

            Intent intent1 = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent1,PendingIntent.FLAG_IMMUTABLE);

        Notification notification = new NotificationCompat.Builder(this,"chanelId1" )
                .setContentTitle("Neville")
                .setContentText("Esto es un texto")
                .setSmallIcon(R.drawable.ic_item)
                .setContentIntent(pendingIntent)
                .build();
//.......

    return START_STICKY;
    }

    private void createNotificationChanel(){
            //Chequeando si la versión de Adroid es Oreo o superior:
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
    
                NotificationChannel notificationChannel = new NotificationChannel("chanelId1","Foreground Notification", NotificationManager.IMPORTANCE_DEFAULT);
    
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
    
                notificationManager.createNotificationChannel(notificationChannel);
    
            }

相关问题