1

When selecting a song from the users library on either iPhone or iPad devices running iOS7 my app crashes out

The line it stops on is

FourCharCode formatID = audioDesc->mFormatID;

With a EXC_BAD_ACCESS code

Trying to check the console for more info, this is the last step it mentions;

appMake[8392:60b] Select music AssetUrl = (null)

Code below for the export session part;

- (void)exportAssetAsSourceFormat:(MPMediaItem *)item {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];


    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                           initWithAsset:songAsset
                                           presetName:AVAssetExportPresetPassthrough];

    NSArray *tracks = [songAsset tracksWithMediaType:AVMediaTypeAudio];
    AVAssetTrack *track = [tracks objectAtIndex:0];
    id desc = [track.formatDescriptions objectAtIndex:0];
    const AudioStreamBasicDescription *audioDesc = CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)desc);
    FourCharCode formatID = audioDesc->mFormatID;

    NSString *fileType = nil;
    NSString *ex = nil;

    switch (formatID) {

        case kAudioFormatLinearPCM:
        {
            UInt32 flags = audioDesc->mFormatFlags;
            if (flags & kAudioFormatFlagIsBigEndian) {
                fileType = @"public.aiff-audio";
                ex = @"aif";
            } else {
                fileType = @"com.microsoft.waveform-audio";
                ex = @"wav";
            }
        }
            break;

        case kAudioFormatMPEGLayer3:
            fileType = @"com.apple.quicktime-movie";
            ex = @"mp3";
            break;

        case kAudioFormatMPEG4AAC:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;

        case kAudioFormatAppleLossless:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;

        default:
            break;
    }

    exportSession.outputFileType = fileType;

    NSString *exportPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:[EXPORT_NAME stringByAppendingPathExtension:ex]] retain];
    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }
    exportSession.outputURL = [NSURL fileURLWithPath:exportPath];

    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"export session completed");
            //return YES;
            [self performSelectorOnMainThread:@selector(gotoMainView:)
                                   withObject:[EXPORT_NAME stringByAppendingPathExtension:ex]
                                waitUntilDone:NO];
        } else {
            NSLog(@"export session error");
            //return NO;
        }

        [exportSession release];
    }];

    [pool release];
}

Media picker code;

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
{

    // Dismiss the media item picker.
    [self dismissModalViewControllerAnimated: YES];

    if ([mediaItemCollection count] < 1) {
        return;
    }
    [selectedItem release];
    selectedItem = [[[mediaItemCollection items] objectAtIndex:0] retain];
    NSURL* filePath = [selectedItem valueForProperty: MPMediaItemPropertyAssetURL];
    NSLog(@"Select music AssetUrl = %@", filePath);
    [viewLoading setHidden:NO];

    [self UnloadSound];
    [NSThread detachNewThreadSelector:@selector(exportAssetAsSourceFormat:) toTarget:self withObject:selectedItem];
}
Larme
  • 24,190
  • 6
  • 51
  • 81
user3355723
  • 235
  • 2
  • 11
  • By any chance, is the music on the cloud (iTunes Matches)? I got issue with music on the cloud. – Larme Mar 08 '14 at 12:21
  • Hey yeah it is actually on the cloud - wonder how to fix? – user3355723 Mar 08 '14 at 12:28
  • I filtered for non-cloud music. You can get more info there: http://stackoverflow.com/questions/8145509/mpmediaitem-and-itunes-match – Larme Mar 08 '14 at 12:34
  • Is there anything I can add to the code above to fix, I read your link but have to admit it left me more confused! Would appreciate any pointers for the existing code above. – user3355723 Mar 08 '14 at 21:27

0 Answers0