Voilà comment je le fais en 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];
Ici data
contiendra les données PCM brutes de la piste. S'il vous plaît noter que vous ne pouvez pas accéder directement au fichier d'une chanson ou une vidéo, seulement ses données grâce à cette méthode. Vous pouvez le compresser en utilisant e. g. FLAC (that's how I'm processing it in my tweak).
Étant donné que MonoTouch a un mappage 1: 1 avec les noms de classe et de méthode Objective-C, il devrait être assez facile à copier. :)
public void ItemsPicked (expéditeur d'objet, ItemsPickedEventArgs e) { MPMediaItem [] mediaItems; NSUrl mediaURL = nouveau NSUrl(); mediaItems = e.MediaItemCollection.Items; mediaURL = (NSUrl) mediaItems [0] .ValueForProperty (MPMediaItemProperty.AssetUrl); asset = AVUrlAsset.FromUrl (mediaURL, null); exportURL = NSUrl.FromFilename (Path.Combine (appDelegate.store.mediaPath, "tmp.m4a")); export = new AVAssetExportSession (asset, AVAssetExportSession.PresetAppleM4A); export.OutputFileType = AVFileType.AppleM4A; export.OutputUrl = exportURL; export.ExportAsynchronously (CopyComplete); } – Rick