OGeek|极客世界-中国程序员成长平台

标题: ios - 如何循环播放轨道的一部分? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:45
标题: ios - 如何循环播放轨道的一部分?

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

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

_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];



Best Answer-推荐答案


如果你可以从 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4