【问题标题】:Not getiing M4a file from iphone using MpMediaQuery没有使用 MpMediaQuery 从 iphone 获取 M4a 文件
【发布时间】:2015-07-25 21:55:17
【问题描述】:

如何从我的 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 文件访问到我的代码中?

【问题讨论】:

  • 代码格式和语法。

标签: ios xcode mp3 m4a mpmediaquery


【解决方案1】:

如果 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);
        }
    }];

【讨论】:

    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    相关资源
    最近更新 更多