HarmonyOS鸿蒙Next中后台播放AVSession开启了长时任务及权限设置,但播控中心仍显示未播放
HarmonyOS鸿蒙Next中后台播放AVSession开启了长时任务及权限设置,但播控中心仍显示未播放
import { media } from ‘@kit.MediaKit’
import AvSession from ‘@ohos.multimedia.avsession’;
import { wantAgent } from ‘@kit.AbilityKit’
import { backgroundTaskManager } from ‘@kit.BackgroundTasksKit’
export interface songItemType { img: string name: string author: string url: string id: string }
export class AvSessionManager { static session: AvSession.AVSession // 单例 媒体会话对象 static controller: AvSession.AVSessionController // 控制器 static async init(context: Context) { // 拿到会话对象 AvSessionManager.session = await AvSession.createAVSession(context, “bgPlay”, “audio”) AvSessionManager.controller = await AvSessionManager.session.getController() // 拿到控制器 } }
@Entry @Component struct FM { avPlayer?: media.AVPlayer song: songItemType = { img: ‘http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/11.jpg’, name: ‘苦笑’, author: ‘汪苏泷’, url: ‘http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/11.mp3’, id: ‘0011’ } async play() { //开启后台播放 this.startBackgroundTask() //创建了播放器实例 const avPlayer = await media.createAVPlayer() avPlayer.on(‘stateChange’, state => { if (state === ‘initialized’) { avPlayer.prepare() } else if (state === ‘prepared’) { avPlayer.play() } }) avPlayer.url = ‘http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/11.mp3’ // this.avPlayer = avPlayer } // 开启后台长时任务 async startBackgroundTask() { // 会话对象只要有就说明不用开启了 if (AvSessionManager.session.sessionId) { // 如果已经有会话了那就不用开启了 return } try { // want的信息组成 目的是点击播控中心的时候 可以进入应用 let wantAgentInfo: wantAgent.WantAgentInfo = { wants: [ { bundleName: ‘com.itcast.myapplication’, abilityName: ‘EntryAbility’ } ], operationType: wantAgent.OperationType.START_ABILITIES, requestCode: 0, wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] } const want = await wantAgent.getWantAgent(wantAgentInfo) // 申请want信息 // 申请长时任务 await backgroundTaskManager.startBackgroundRunning(getContext(), backgroundTaskManager.BackgroundMode.AUDIO_PLAYBACK, want ) } catch (error) { AlertDialog.show({ message: error.message }) } } build() { Column() { Button(‘点击播放’) .onClick(() => { this.play() }) } .height(‘100%’) .width(‘100%’) } }
更多关于HarmonyOS鸿蒙Next中后台播放AVSession开启了长时任务及权限设置,但播控中心仍显示未播放的实战教程也可以访问 https://www.itying.com/category-93-b0.html
需要正确初始化播控中心的播放资源,并在状态变化时更新。
// 设置状态: 播放状态,进度位置,播放倍速,缓存的时间
let playbackState: AVSessionManager.AVPlaybackState = {
state: AVSessionManager.PlaybackState.PLAYBACK_STATE_PLAY, // 播放状态
position: {
elapsedTime: 1000, // 已经播放的位置,以ms为单位
updateTime: new Date().getTime(), // 应用更新当前位置时的时间戳,以ms为单位
},
speed: 1.0, // 可选,默认是1.0,播放的倍速,按照应用内支持的speed进行设置,系统不做校验
bufferedTime: 14000, // 可选,资源缓存的时间,以ms为单位
};
session.setAVPlaybackState(playbackState, (err) => {
if (err) {
console.error(`Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`SetAVPlaybackState successfully`);
}
});
更多详细步骤可参考接入AVSession场景介绍。
更多关于HarmonyOS鸿蒙Next中后台播放AVSession开启了长时任务及权限设置,但播控中心仍显示未播放的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
你没有同步播放状态
好的谢谢,
你还没有设置metadata还有必须要监听至少一个AVSession的事件
好的感谢,
基本信息
姓名: 张三
职位: 软件工程师
技能: Python, Java, C++
工作经验
公司: ABC有限公司
职位: 软件工程师
时间: 2018-2022
描述: 参与开发和维护多个项目,负责代码审查和优化。
教育背景
学校: 清华大学
学位: 计算机科学与技术学士
时间: 2014-2018
setAVPlaybackState设置播放状态
在HarmonyOS鸿蒙Next中,即使开启了AVSession的长时任务和权限设置,播控中心仍显示未播放,可能是以下原因:
-
AVSession未正确启动:确保AVSession已成功创建并启动,使用
AVSession.createSession()
和AVSession.start()
方法。 -
媒体信息未设置:通过
AVSession.setMetadata()
设置媒体信息,如标题、艺术家等,确保播控中心能正确显示。 -
播放状态未更新:使用
AVSession.setPlaybackState()
及时更新播放状态,如播放、暂停等。 -
权限问题:检查是否已获取
ohos.permission.KEEP_BACKGROUND_RUNNING
权限,并在config.json
中声明。 -
后台任务限制:确保应用在后台运行时未被系统限制,可通过
BackgroundTaskManager
申请长时任务。 -
日志排查:使用
HiLog
查看相关日志,定位问题。
检查以上步骤,确保AVSession正确配置和运行。