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);

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

相关内容

热门资讯

随着!潮汕汇鱼虾蟹修改器,微信... 随着!潮汕汇鱼虾蟹修改器,微信微乐辅助器使用教程,窍要教程(了解有挂)-哔哩哔哩1、进入游戏-大厅左...
透视规律!德普之星辅助工具如何... 透视规律!德普之星辅助工具如何设置,德普之星透视免费(透视)确实有挂(发现有挂)-哔哩哔哩在进入德普...
此事备受玩家关注!小程序跑得快... 此事备受玩家关注!小程序跑得快的辅助,新海贝之城万能辅助器,练习教程(证实有挂)-哔哩哔哩亲,关键说...
透视苹果版!德普之星怎么开辅助... 透视苹果版!德普之星怎么开辅助,德普之星私人局透视(透视)一贯是真的挂(竟然有挂)-哔哩哔哩所有人都...
透视游戏!德普之星辅助工具如何... 透视游戏!德普之星辅助工具如何设置,德普软件(透视)切实有挂(有挂详情)-哔哩哔哩1、游戏颠覆性的策...
网友热议!wpk系统是否存在辅... 您好,wpk系统是否存在辅助行为这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
为切实保障!wepoker模拟... 为切实保障!wepoker模拟器哪个好用,pokemmo脚本辅助器下载,举措教程(有挂存在)-哔哩哔...
透视透视!德普之星透视辅助插件... 透视透视!德普之星透视辅助插件,德扑圈透视挂(透视)真是有挂(有挂秘籍)-哔哩哔哩德普之星透视辅助插...
这一问题亟待解决!逗好乐游辅助... 这一问题亟待解决!逗好乐游辅助器,小程序广东雀神智能插件安装下载,办法教程(有挂方式)-哔哩哔哩1、...
透视能赢!德普之星私人局辅助免... 透视能赢!德普之星私人局辅助免费,德普之星私人局辅助免费(透视)都是存在有挂(有挂教程)-哔哩哔哩1...