ios - 将 AVPlayerItem 保存到文档目录

标签 ios objective-c swift avaudioplayer avplayer

如何保存 AVPlayerItem?我在网上看了看,真的找不到任何东西。 AVPlayerItem 在 Assets 中包含两个音频文件。我如何将其保存到用户文档文件夹?我的代码很快,但也欢迎在 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/

相关文章:

ios - 从任何 URL 获取特定图像,就像 Facebook 那样

ios - 在 swift 中部分匹配字符串与正则表达式

ios - 如何在 iOS 中制作自定义文件夹/相册

iOS 7 套接字连接被拒绝错误 61

ios - 导航栏的默认返回行为不起作用

ios - 游戏在检查 Sprite 时偶尔会崩溃

xcode - 找到 X 轴的中间以放置标签 Swift Xcode 6

iphone - 我是否需要加入 iOS Developer Program 才能通过 TestFlight 发送我的应用构建?

ios - 中心按钮标签栏

objective-c - 在 Cocoa 中隐藏分段控件