帮你快速理解、总结文档立即下载

iOS

最近更新时间:2026-11-09 14:52:03
我的收藏

功能概览

录制支持边录边播的背景音乐:录制过程中按设定速率播放,音乐信息会记录进每个录制片段(TAVRecordSegment.bgmPath 与 bgmStartPosition),便于后续编辑阶段对齐。BGM 能力由 ITAVBGMManager 提供,通过 [recorder getBGMManager] 获取。

相关接口类

接口类名
描述
ITAVBGMManager
设置、定位、循环、音量、倍速与清除 BGM。
TAVRecordSegment
片段模型,记录该段使用的 BGM 与起始位置。
/// 获取 BGM 管理接口
- (id<ITAVBGMManager>)getBGMManager;

接口方法

方法
说明
- (void)setBGM:(NSString *)bgmPath startTime:(CMTime)startTime duration:(CMTime)duration
设置背景音乐与播放区间。
- (void)seekToTime:(CMTime)time
跳转到指定播放位置。
- (void)setLoop:(BOOL)loop
是否循环播放。
- (void)setVolume:(float)volume
音量,范围 [0.0, 1.0]。
- (void)setPlaybackRate:(float)rate
BGM 播放速率。
- (void)clearBGM
清除背景音乐并释放相关资源。

设置与播放控制

id<ITAVBGMManager> bgmManager = [recorder getBGMManager];

// 设置 BGM,从头播放到文件结尾
[bgmManager setBGM:@"/path/to/bgm.mp3" startTime:kCMTimeZero duration:kCMTimeInvalid];

// 从 30 秒处开始播放
[bgmManager seekToTime:CMTimeMakeWithSeconds(30, 600)];

// 循环播放、调整音量
[bgmManager setLoop:YES];
[bgmManager setVolume:0.8];

// 录制结束后清理
[bgmManager clearBGM];

只截取音乐的一段

// 播放第 10s ~ 40s 的内容
CMTime start = CMTimeMakeWithSeconds(10, 600);
CMTime duration = CMTimeMakeWithSeconds(30, 600);
[bgmManager setBGM:@"/path/to/bgm.mp3" startTime:start duration:duration];
说明:
startTime 传 kCMTimeZero 表示从头开始;duration 传 kCMTimeInvalid 表示播放到文件结尾。

变速录制时的 BGM 速率

startRecordWithSpeed:enableMic: 传入录制速度 speed 时,SDK 会自动把 BGM 播放速率同步为 1.0/speed,使 BGM 节奏与最终视频对齐,业务方通常无需手动设置。
若业务需要自行接管,可通过 setPlaybackRate: 显式指定:
// 录制为 2 倍速时,手动把 BGM 设为 0.5 倍速
[bgmManager setPlaybackRate:0.5];
[recorder startRecordWithSpeed:2.0 enableMic:YES];

片段中的 BGM 信息

每段录制会把当前 BGM 写入片段:
字段
类型
说明
TAVRecordSegment.bgmPath
NSString *
该段使用的 BGM 路径。
TAVRecordSegment.bgmStartPosition
CMTime
该段 BGM 的起始位置。
因此「多段录制 + 中途换 BGM」时,各段会保留各自的信息;需要连续播放同一首 BGM 时,应在分段之间用 seekToTime: 校正位置,避免每段都从头播放。

注意事项

录制前设置的 BGM 才会写入片段;录制过程中切换 BGM,建议在两段之间进行。
变速录制时片段的录制速度与 BGM 播放速率相互独立:setPlaybackRate: 控制音乐自身速度,startRecordWithSpeed:enableMic: 控制视频速度。
clearBGM() 只影响后续录制,不会修改已生成片段的 bgmPath。
音量取值 [0.0, 1.0],0.0 静音、1.0 原始音量。