Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to read audio data from audio file (-4) #49

Closed
jglezmaestro opened this issue Apr 30, 2014 · 1 comment
Closed

Failed to read audio data from audio file (-4) #49

jglezmaestro opened this issue Apr 30, 2014 · 1 comment

Comments

@jglezmaestro
Copy link

Crash on iPhone 4 iOS 6 when try to getWaveformDataWithCompletionBlock from an ipod-library URL-scheme URL. On iPhone 5 iOS 7.1 all works fine.

@syedhali
Copy link
Owner

Please see my response to issue #12. You can still access iPod songs as EZAudioFile, but you first have to use the AVAssetExportSession to move the audio to a valid url that EZAudioFile can use.

//
// Creates an EZAudioFile from an MPMediaItem representing a song coming from
// an iOS device's iPod library. Since an MPMediaItem's URL is always prefixed
// with an ipod:// path we must use the AVAssetExportSession to first export
// the song to a file path that the EZAudioFile can actually find in the app's bundle.
//
- (void)openMediaItem:(MPMediaItem *)item
           completion:(void(^)(EZAudioFile *audioFile, NSError *error))completion
{
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
    NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
    if (url)
    {
        //
        // Create an AVAudioExportSession to export the MPMediaItem to a non-iPod
        // file path url we can actually use for an EZAudioFile
        //
        AVURLAsset *asset = [AVURLAsset assetWithURL:url];
        AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:asset
                                                                           presetName:AVAssetExportPresetAppleM4A];
        exporter.outputFileType = @"com.apple.m4a-audio";
        NSString *exportURLPath = [[self applicationDocumentsDirectory] stringByAppendingFormat:@"/%@.m4a", title];
        NSURL *exportURL = [NSURL fileURLWithPath:exportURLPath];
        exporter.outputURL = exportURL;

        //
        // Delete any existing path in the bundle if one already exists
        //
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:exportURLPath])
        {
            NSError *error;
            [fileManager removeItemAtPath:exportURLPath error:&error];
            if (error)
            {
                NSLog(@"error deleting file: %@", error.localizedDescription);
            }
        }

        //
        // Export the audio data using the AVAudioExportSession to the
        // exportURL in the application bundle
        //
        [exporter exportAsynchronouslyWithCompletionHandler:^{
            AVAssetExportSessionStatus status = [exporter status];
            switch (status)
            {
                case AVAssetExportSessionStatusCompleted:
                {
                    EZAudioFile *file = [EZAudioFile audioFileWithURL:exportURL];
                    completion(file ,nil);
                    break;
                }
                case AVAssetExportSessionStatusFailed:
                {
                    completion(nil, exporter.error);
                    break;
                }
                default: 
                {
                    NSLog(@"Exporter status not fialed or complete: %ld", status);
                    break;
                }
            }
        }];
    }
    else
    {
        NSError *error = [NSError errorWithDomain:@"com.myapp.sha"
                                             code:404
                                         userInfo:@{ NSLocalizedDescriptionKey : @"Media item's URL not found" }];
        completion(nil, error);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants