目标:我需要循环播放音频文件的任意切片。
当前解决方案:
我目前正处于第二个解决方案循环的阶段,但尚未达到我可以测试UI是否锁定的程度。
问题:我正在查看AVFoundation中的可用方法,并想到了: “是否可以引用音频文件的精确块并循环,根据需要更改块?“ 。我找不到的东西,我认为这可能是由于我缺乏正确搜索它的命名法,是这样的例子。
我想尽可能使用内置框架。
答案 0 :(得分:0)
在最终找到一些好的例子之后,我提出了:
let composition = AVMutableComposition()
var playerItem:AVPlayerItem!
var qPlayer:AVQueuePlayer!
var currentAudioTrack:AVAssetTrack!
var compAudioTrack:AVMutableCompositionTrack!
var uurl:URL!
var asset:AVURLAsset!
...
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
} catch let error {
print("i am error:", error)
}
compAudioTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
uurl = (sysMusicPlayer.nowPlayingItem?.assetURL)!
asset = AVURLAsset(url: uurl)
currentAudioTrack = asset.tracks(withMediaType: AVMediaTypeAudio)[0]
assetTimeScale = currentAudioTrack.naturalTimeScale
let a:CMTime = CMTime(seconds: 10, preferredTimescale: assetTimeScale)
let b:CMTime = CMTime(seconds: 42, preferredTimescale: assetTimeScale)
try! compAudioTrack.insertTimeRange(CMTimeRange(start: a, end: b), of: currentAudioTrack, at: CMTimeMake(0, 1))
playerItem = AVPlayerItem(asset: composition)
qPlayer = AVQueuePlayer(playerItem: playerItem)
qPlayer.play()
这是基本功能,让我可以通过
轻松更改歌曲的当前部分 compAudioTrack.segments.removeAll()
try! compAudioTrack.insertTimeRange(timeRangeToInsert, of: currentAudioTrack, at: CMTimeMake(0, 1))
qPlayer.insert(playerItem, after: nil)