Pergunta

I have retrieved all musics and videos from my iPhone device. I am now stuck in saving those in to my application, i am unable to get the raw data out of the file. Can any one help me to find a solution for this. this is the code i used to get the music files.

MPMediaQuery *deviceiPod = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [deviceiPod items];
for (MPMediaItem *media in itemsFromGenericQuery){
 //i get the media item here.
}

How to convert it to NSData ?? This is what i tried to get the data

audioURL = [media valueForProperty:MPMediaItemPropertyAssetURL];//here i get the asset url
NSData *soundData = [NSData dataWithContentsOfURL:audioURL];

using this was useless for me. i dint get the data from the LocalAssestURL. Any solution for this. Thanks in advance

Foi útil?

Solução

This is not a trivial task - Apple's SDKs often fail to provide simple APIs for simple tasks. Here's the code I'm using in one of my tweaks in order to get raw PCM data out of the asset. You'll need to add the AVFoundation and the CoreMedia framework to your project in order to get this working:

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

MPMediaItem *item = // obtain the media item
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Get raw PCM data from the track
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSMutableData *data = [[NSMutableData alloc] init];

const uint32_t sampleRate = 16000; // 16k sample/sec
const uint16_t bitDepth = 16; // 16 bit/sample/channel
const uint16_t channels = 2; // 2 channel/sample (stereo)

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];
[reader release];
[pool release];

Here data will contain the raw PCM data of the track; you can use some kind of encoding to compress it, for example I use the FLAC codec library.

See the original source code here.

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