You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Flutter与iOS Media Control集成?锁屏播放显示问题求解

iOS锁屏媒体控制集成解决方案

方案1:切换到audio_service包

audio_service专门为Flutter应用提供后台播放管理和系统媒体控制支持,完全兼容iOS锁屏显示。你可以:

  • 用它替代just_audio_background,或与just_audio配合使用
  • 通过AudioHandler配置播放会话,更新播放状态、歌曲元数据(标题、艺术家、封面图等),这些信息会自动同步到iOS锁屏的媒体控制栏
  • 它原生支持播放/暂停、上一曲/下一曲等锁屏控制操作,无需额外编写原生代码

方案2:手动调用iOS原生API实现

如果不想替换现有包,可通过MethodChannel直接调用iOS的MPNowPlayingInfoCenter来更新锁屏信息:

  1. 在Flutter端创建MethodChannel,定义更新媒体信息的方法
  2. 在iOS原生代码中实现对应方法,通过MPNowPlayingInfoCenter.default().nowPlayingInfo设置歌曲标题、艺术家、播放时长、当前进度等字段
  3. 在info.plist中开启audio后台模式,确保应用在后台仍能更新信息
  4. 播放状态变化(播放/暂停、切换歌曲)时,通过MethodChannel触发原生代码更新信息

示例原生代码片段(Swift):

import MediaPlayer

func updateNowPlayingInfo(title: String, artist: String, duration: Double, elapsedTime: Double, isPlaying: Bool) {
    let infoCenter = MPNowPlayingInfoCenter.default()
    var nowPlayingInfo = [String: Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = title
    nowPlayingInfo[MPMediaItemPropertyArtist] = artist
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedTime
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0
    infoCenter.nowPlayingInfo = nowPlayingInfo
}

方案3:检查just_audio_background的配置细节

虽然官方文档标注不支持锁屏显示,仍可确认以下配置是否遗漏:

  • 确保info.plist中添加了UIBackgroundModes并包含audio项
  • 创建MediaItem时,完整设置title、artist、artUri等元数据字段
  • 初始化AudioPlayer时正确关联AudioBackgroundTask

若以上配置均无误但仍不生效,说明确实是just_audio_background的功能限制,建议优先采用前两个方案。

内容的提问来源于stack exchange,提问作者Johnson Lin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 14:42:39