Android:确定通知的音频何时停止播放
创始人
2024-10-13 15:00:33
0

要确定通知的音频何时停止播放,可以使用 MediaPlayer 类来控制音频的播放和停止。以下是一个示例代码,展示了如何在 Android 上实现此功能:

首先,在 AndroidManifest.xml 文件中添加以下权限:


然后,创建一个 Service 类,用于处理通知音频的播放和停止。在该 Service 类中,我们将创建一个 MediaPlayer 对象,并在通知被移除时停止音频的播放。以下是一个示例代码:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class MyService extends Service {

    private static final String CHANNEL_ID = "ForegroundServiceChannel";
    private MediaPlayer mediaPlayer;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = MediaPlayer.create(this, R.raw.your_audio_file);
        mediaPlayer.setLooping(true);
    }

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

        createNotificationChannel();

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("Your audio is playing")
                .setSmallIcon(R.drawable.ic_notification)
                .build();

        startForeground(1, notification);

        mediaPlayer.start();

        return START_STICKY;
    }

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

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

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

最后,在你的 Activity 类中,你可以使用以下代码来启动 Service 类:

Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);

这样,当你的应用程序进入后台时,通知音频将继续播放,并且当通知被移除时,音频也会停止播放。

相关内容

热门资讯

玩家必看科普!大菠萝789辅助... 玩家必看科普!大菠萝789辅助器下载,wepoker辅助脚本,必胜教程(有挂辅助);大菠萝789辅助...
带你了解!hhpoker脚本下... 带你了解!hhpoker脚本下载,we poker辅助器,解密教程(有挂方法);1.hhpoker脚...
玩家必知教程!wepoker可... 这是一款非常优秀的wepoker究竟有没有透视 ia辅助检测软件,能够让你了解到wepoker究竟有...
一分钟了解!wepoker模拟... 一分钟了解!wepoker模拟器哪个,约局吧如何查看是否有挂,2025新版教程(有挂软件);人气非常...
分享给玩家!拱趴大菠萝万能辅助... 分享给玩家!拱趴大菠萝万能辅助器,wepoker公共底牌,2025教程(有挂技巧);1分钟了解详细教...
重大消息!哈糖大菠萝有挂吗,佛... 重大消息!哈糖大菠萝有挂吗,佛手在线大菠萝智能辅助器,靠谱教程(有挂软件);亲真的是有正版授权,小编...
盘点一款!hhpoker是真的... 盘点一款!hhpoker是真的吗,wepoker游戏安装教程,微扑克教程(有挂辅助)是一款可以让一直...
实测必看!竞技联盟破解版最新版... 1、实测必看!竞技联盟破解版最新版,pokernow辅助工具,黑科技教程(有挂攻略)2、进入游戏-大...
最新技巧!wepoker辅助真... 1、最新技巧!wepoker辅助真的假的,aapoker插件,可靠技巧(有挂透视);详细教程。2、w...
一分钟快速了解!wepoker... 一分钟快速了解!wepoker有机器人吗,德州透视插件,曝光教程(有挂透明)1、很好的工具软件,可以...