2013-02-01 23 views
7

मैं कुछ ऑडियो फ़ाइलों को मर्ज करने की कोशिश कर रहा हूं (MPMediaPickerController के माध्यम से उठाया गया), लेकिन निर्यात हमेशा त्रुटि कोड -12780 के साथ विफल रहता है।AVAssetExportSession हर बार विफल रहता है (त्रुटि -12780)

जब मैं एवीप्लेयर ऑब्जेक्ट के साथ अपनी रचना को चलाने की कोशिश करता हूं, तो यह सही ढंग से खेलता है। बस निर्यात विफल रहता है।

मैं क्या गलत कर रहा हूं?

AVAssetExportSession *exportSession; 
AVPlayer *player; 

- (void)mergeAudiofiles { 
    // self.mediaItems is an NSArray of MPMediaItems 
    if (self.mediaItems.count == 0) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"No Tracks selected." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 

    // Configure audio session 
    NSError *sessionError; 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryAudioProcessing error:nil]; 
    [session setActive:YES error:&sessionError]; 
    if (sessionError) NSLog(@"Session-Error: %@", sessionError.localizedDescription); 

    // Create composition 
    AVMutableComposition *composition = [AVMutableComposition composition]; 
    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    CMTime position = kCMTimeZero; 

    for (MPMediaItem *item in self.mediaItems) { 
     NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
     AVAsset *asset = [AVAsset assetWithURL:assetURL]; 

     CMTimeRange duration = CMTimeRangeMake(kCMTimeZero, asset.duration); 
     //  duration = CMTimeRangeMake(kCMTimeZero, CMTimeMake(5, 1)); // For player test 

     NSError *error; 
     [track insertTimeRange:duration ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] lastObject] atTime:position error:&error]; 
     if (error) NSLog(@"ERROR! :("); 

     position = CMTimeAdd(position, duration.duration); 
    } 

    // Path to output file 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSURL *exportUrl = [NSURL URLWithString:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]]; 

    NSLog(@"Export URL = %@", exportUrl.description); 

    // Playing works! 
    // """""""""""""" 
    // AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithAsset:composition]; 
    // player = [[AVPlayer alloc] initWithPlayerItem:pitem]; 
    // [player play]; 
    // 
    // return; 

    // Export 
    exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A]; 
    exportSession.outputURL = exportUrl; 
    exportSession.outputFileType = AVFileTypeAppleM4A; 

    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
     switch (exportSession.status) { 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Export failed -> Reason: %@, User Info: %@", 
         exportSession.error.localizedDescription, 
         exportSession.error.userInfo.description); 
       break; 

      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Export cancelled"); 
       break; 

      case AVAssetExportSessionStatusCompleted: 
       NSLog(@"Export finished"); 
       break; 

      default: 
       break; 
     } 
    }]; 
} 

उत्तर

16

करने के लिए अपने निर्यात यूआरएल को बदलने की आवश्यकता:

NSURL *exportUrl = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]]; 

जब रास्तों दायर करने के लिए चर्चा करते हुए आप fileURLWithPath का उपयोग करना चाहिए

यह मेरा कोड है। जब तक मैंने इसे पढ़ नहीं लिया, तब तक मैंने इसे लटका दिया: AVAssetExportSession outputfile

+0

धन्यवाद। मैं अपनी सैनिटी के रिमांड को खोने के करीब था! – GreatWiz

+0

मेरा दिन बचाया ... धन्यवाद +1 – Vats

+0

ओएमजी, आपने अपना जीवन बचाया। आपका बहुत बहुत धन्यवाद! – Tien