0

如何从我的 iPhone 访问 m4a 文件。现在我只访问 mp3 文件和使用MpMediaQuery,但现在我也想访问 m4a 文件。我正在使用这段代码:

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];
}

你能告诉我怎么做吗,如何将 iPhone 音乐库中的 m4a 文件访问到我的代码中?

4

1 回答 1

0

如果 MPMediaItemPropertyAssetURL 返回的文件只是 mp3 文件,则可能意味着您的 ipod 库中没有 m4a 文件。

如果您出于某种原因想将这些 mp3 文件转换为 m4a 文件,您可以通过以下方式使用 AVAssetExportSession。

//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);
        }
    }];
于 2015-06-10T09:51:08.197 回答