应用在使用服务时被杀死时,音乐会暂停

编程入门 行业动态 更新时间:2024-10-24 08:32:27
本文介绍了应用在使用服务时被杀死时,音乐会暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Service 类在后台播放音乐.当我在顶部显示通知栏时,我的应用程序有问题.当我杀死该应用程序时,音乐会停止大约1秒钟,然后再次开始播放.我不知道是什么问题.

I am using a Service class to play music in the background. While I am displaying the notification bar on top, there is issue in my app. When I kill the app, the music stops for about 1 second and then it starts again. I can't figure out what's the issue.

我正在关注此教程.

public class MyMusicService extends Service { MediaPlayer mediaPlayer; @Nullable @Override public IBinder onBind(Intent intent) { return null; } public MyMusicService() { super(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (mediaPlayer!= null) { mediaPlayer.release(); } mediaPlayer =MediaPlayer.create(this,R.raw.song); mediaPlayer.setLooping(true); mediaPlayer.start(); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); mediaPlayer.stop(); } }

推荐答案

在主要活动上使用onTaskRemoved,并在此事件上释放媒体播放器.

Use onTaskRemoved on your main activity and release your mediaplayer on this event.

更多推荐

应用在使用服务时被杀死时,音乐会暂停

本文发布于:2023-11-27 08:35:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用在   被杀   音乐会   死时

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!