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 に答える