0

我正在尝试将一个项目从 iPod 库复制到我的本地存储空间 - 以供以后播放。我有项目 URl,但它是 (ipod-library://item/item.mp3?id=2398084975506389321) 知道如何访问实际文件吗?

谢谢,瑞克

4

2 回答 2

3

这将工作https://gist.github.com/3304992

-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{
    NSString *tempPath = NSTemporaryDirectory();
    int i=1;
    for (MPMediaItem *theItem in mediaItemCollection.items) {
        NSURL *url = [theItem valueForProperty:MPMediaItemPropertyAssetURL];
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetPassthrough];
        exporter.outputFileType = @"com.apple.coreaudio-format";
        NSString *fname = [[NSString stringWithFormat:@"%d",i] stringByAppendingString:@".caf"];
        ++i;
        NSString *exportFile = [tempPath stringByAppendingPathComponent: fname];
        exporter.outputURL = [NSURL fileURLWithPath:exportFile];
        [exporter exportAsynchronouslyWithCompletionHandler:^{
            //Code for completion Handler
        }];
    }
    [picker dismissViewControllerAnimated:YES completion:Nil];
}

使用 MPMediaPickerController 选择媒体

于 2012-09-15T11:10:51.557 回答
1

这就是我在 Objective-C 中的做法:

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudio.h>

// or [NSURL URLWithString:@"ipod-library://item/item.mp3?id=2398084975506389321"]
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSMutableData *data = [[NSMutableData alloc] init];

const uint32_t sampleRate = 16000;
const uint16_t bitDepth = 16;
const uint16_t channels = 2;

NSDictionary *opts = [NSDictionary dictionary];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:assetURL options:opts];
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:NULL];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
    [NSNumber numberWithFloat:(float)sampleRate], AVSampleRateKey,
    [NSNumber numberWithInt:bitDepth], AVLinearPCMBitDepthKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
    [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
    nil];

AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:[[asset tracks] objectAtIndex:0] outputSettings:settings];
[asset release];
[reader addOutput:output];
[reader startReading];

// read the samples from the asset and append them subsequently
while ([reader status] != AVAssetReaderStatusCompleted) {
    CMSampleBufferRef buffer = [output copyNextSampleBuffer];
    if (buffer == NULL) continue;

    CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(buffer);
    size_t size = CMBlockBufferGetDataLength(blockBuffer);
    uint8_t *outBytes = malloc(size);
    CMBlockBufferCopyDataBytes(blockBuffer, 0, size, outBytes);
    CMSampleBufferInvalidate(buffer);
    CFRelease(buffer);
    [data appendBytes:outBytes length:size];
    free(outBytes);
}
[output release];

这里data将包含曲目的原始 PCM 数据。请注意,您不能直接访问歌曲或视频的文件,只能通过此方法访问其数据。您可以使用例如 FLAC 来压缩它(这就是我在我的调整中处理它的方式)。

由于 MonoTouch 与 Objective-C 的类和方法名称有 1:1 的映射关系,所以这应该很容易复制。:)

于 2012-09-08T19:17:44.167 回答