ios - 如何循环一段轨道?

标签 ios objective-c audio avaudioplayer

我有一个包含前奏 (0:00-0:10) 和循环部分 (0:10-0:30) 的音乐文件。有许多库和简单的解决方案可以从结束-> 开始循环播放轨道,但我无法找到一种从结束-> 循环开始循环播放轨道的方法。实现这一目标的最佳方法是什么?

这是我目前根据下面的评论使用的代码。它在循环时仍然会产生一个小间隙:

_player = [AVPlayer playerWithURL:url];
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime loopStart = CMTimeMakeWithSeconds(19.2, 1);
CMTime loopEnd = CMTimeMakeWithSeconds(durationSeconds, 1);
NSArray *times = @[[NSValue valueWithCMTime:loopEnd]];

__weak typeof(AVPlayer *)weakPlayer = _player;
self.playerObserver = [_player addBoundaryTimeObserverForTimes:times queue:NULL usingBlock:^{
    [weakPlayer seekToTime:loopStart];
}];

[_player play];

最佳答案

如果你可以从 AVAudioPlayer 切换到 AVPlayer,你可以使用 addBoundaryObserverForTimes:

来自docs :

Requests invocation of a block when specified times are traversed during normal playback.

AV Foundation Programming Guide 对如何使用此方法的代码示例进行了解释:Tracking Time

关于ios - 如何循环一段轨道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18373456/

相关文章:

ios - 从 ios 中的 UIImage 中删除行

ios - 与 xib 中隐藏的导航栏相关的自动布局无效

objective-c - MBProgressHud 不适用于导航栏

ios - AVAudioSession用于音频视频通话

c# - 如何解释 MP3 文件中的原始 pcm 数据

ios - 如何在 iOS 应用程序中使用 swift 进行 gRPC 调用

ios - 是否有同时处理捏合和平移的手势识别器?

java - 从在线电台流式传输音频

ios - 无法理解 iOS 系统 UIPasteboard

objective-c - Twitter OAuth 和 accessToken 在 Objective-C 中获取状态/user_timeline