如何保存AVPlayerItem?我在网上看了,真的找不到任何东西。 AVPlayerItem在资产中包含两个音频文件。如何将其保存到用户文档文件夹?我的代码很快,但也欢迎在 objective-c 中给出答案。

码:

        let type = AVMediaTypeAudio
        let audioFile = NSBundle.mainBundle().URLForResource("school", withExtension: "mp3")
        let asset1 = AVURLAsset(URL: audioFile, options: nil)
        let arr2 = asset1.tracksWithMediaType(type)
        let track2 = arr2.last as AVAssetTrack

        let duration : CMTime = track2.timeRange.duration

        let comp = AVMutableComposition()
        let comptrack = comp.addMutableTrackWithMediaType(type,
            preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

        comptrack.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(0,600), error:nil)
        comptrack.insertTimeRange(CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(5,600), error:nil)



        let type3 = AVMediaTypeAudio
        let s = NSBundle.mainBundle().URLForResource("file2", withExtension:"m4a")
        let asset = AVURLAsset(URL:s, options:nil)
        let arr3 = asset.tracksWithMediaType(type3)
        let track3 = arr3.last as AVAssetTrack

        let comptrack3 = comp.addMutableTrackWithMediaType(type3, preferredTrackID:Int32(kCMPersistentTrackID_Invalid))
        comptrack3.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(10,600)), ofTrack:track3, atTime:CMTimeMakeWithSeconds(0,600), error:nil)



        let params = AVMutableAudioMixInputParameters(track:comptrack3)
        params.setVolume(1, atTime:CMTimeMakeWithSeconds(0,600))
        params.setVolumeRampFromStartVolume(1, toEndVolume:0, timeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(7,600), CMTimeMakeWithSeconds(3,600)))
        let mix = AVMutableAudioMix()
        mix.inputParameters = [params]

        let item = AVPlayerItem(asset:comp)
        item.audioMix = mix

最佳答案

从您的问题很难理解您要做什么,但是我有点怀疑您需要阅读AVAssetExportSession类。

https://developer.apple.com/library/ios/Documentation/AVFoundation/Reference/AVAssetExportSession_Class/index.html#//apple_ref/occ/cl/AVAssetExportSession

关于ios - 将AVPlayerItem保存到文档目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27081506/

10-10 20:42