0

How I can access m4a file from my iPhone. Right now i am only accessing mp3 file and using MpMediaQuery, but now I want to access m4a file also. I am using this code:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *songMedia in itemsFromGenericQuery)
{
    NSString *songTitle = [songMedia valueForProperty: MPMediaItemPropertyTitle];
    NSString *songTitle2 = [[songMedia valueForProperty: MPMediaItemPropertyAssetURL] absoluteString];
    NSString *artistName = [songMedia valueForProperty: MPMediaItemPropertyArtist];
     //NSString *duration = [songMedia valueForProperty: MPMediaItemPropertyPlaybackDuration];
    NSNumber *seconds=[songMedia valueForProperty: MPMediaItemPropertyPlaybackDuration];
    int time=[seconds intValue]*1000;
    NSString *duration=[NSString stringWithFormat:@"%d",time];
}

Can you please tell me how to do it, how to access m4a file from iPhone music library into my code?

Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52

1 Answers1

0

If the files returned by MPMediaItemPropertyAssetURL are only ever mp3 files it probably means that you dont have m4a files in your ipod library.

If you would like to convert these mp3 files to m4a files for some reason you can use AVAssetExportSession in the following way.

//url comes from  MPMediaItemPropertyAssetURL
   AVAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    // create the export session
    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset: songAsset
                                           presetName:AVAssetExportPresetAppleM4A];
    if (nil == exportSession)
        NSLog(@"Error");
    //presetName:AVAssetExportPresetAppleM4A
    // configure export session  output with all our parameters
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *filePath = [NSString stringWithFormat: @"%@/convertedfile.m4a", basePath];

    exportSession.outputURL = [NSURL fileURLWithPath: filePath]; // output path
    exportSession.outputFileType = AVFileTypeAppleM4A; // output file type

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (AVAssetExportSessionStatusCompleted == exportSession.status)
        {
            NSLog(@"AVAssetExportSessionStatusCompleted");
        }
        else if (AVAssetExportSessionStatusFailed == exportSession.status)
        {
            // a failure may happen because of an event out of your control
            // for example, an interruption like a phone call comming in
            // make sure and handle this case appropriately
            NSLog(@"AVAssetExportSessionStatusFailed");
        }
        else
        {
            NSLog(@"Export Session Status: %d", exportSession.status);
        }
    }];
B_B
  • 23
  • 5