2

How do i go about saving an AVPlayerItem? I looked online and really couldn't find anything. The AVPlayerItem contains two audio files in an asset. How do i save this to the users documents folder? My code is in swift but answers in objective c are welcome also.

Code:

        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 
  • Why do you need to save it? – matt Nov 22 '14 at 19:37
  • I want the user to be able to have saved on their phone for them to listen and i want to share it to social networks also – tryingtolearn Nov 22 '14 at 19:41
  • What on earth would it mean so "share an AVPlayerItem" to social networks? It isn't a "thing", like a photo. It's just a class that AVFoundation uses for some internal calculation / description. What are you _really_ trying to do? "Listen" to _what_? If you want to save a sound file, you need to make a sound file; an AVPlayerItem is not a sound file. – matt Nov 22 '14 at 20:02
  • i am trying to mix the two files into one audio file. Save that one audio file to user phone and share. How do i make a sound file for the two audio files combined into one? – tryingtolearn Nov 22 '14 at 20:09
  • I believe I've answered that question. – matt Nov 22 '14 at 20:10

1 Answers1

1

It's hard to understand from your question what you're trying to do, but I have a vague suspicion that you need to read up on the AVAssetExportSession class.

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • i took your advice but ran into a problem if you dont mind looking at the question. Thanks. http://stackoverflow.com/questions/27083974/invalid-file-output-avassetexport – tryingtolearn Nov 23 '14 at 00:14