Pergunta

I'm trying to copy an item from the iPod Library to my local storage space - for later playback. I've got the item URl but it's (ipod-library://item/item.mp3?id=2398084975506389321) any idea how to access the actual file?

Thanks, Rick

Foi útil?

Solução

This will work 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];
}

use MPMediaPickerController to pick the media

Outras dicas

This is how I'm doing it in 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];

Here data will contain the raw PCM data of the track. Please note that you cannot directly access the file of a song or video, only its data through this method. You can compress it using e. g. FLAC (that's how I'm processing it in my tweak).

Since MonoTouch has an 1:1 mapping to Objective-C class and method names, this should be fairly easy to copy over. :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top