Getting NSData out of music file in iPhone
Asked Answered
M

1

2

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

Marquita answered 30/10, 2012 at 7:57 Comment(0)
W
9

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.

Weathered answered 30/10, 2012 at 9:17 Comment(5)
This code takes more than 2 sec for single audio file on iphone 4. Is there any way to make this file reading faster ?Triturate
Hi, for the compression, I used zlib but it barely reduced the file size. I used the code here- #8425512State
Do you know any reason why?State
I checked it and for mp3 this works very well. However, files from iTunes return empty NSData. When I use AVAssetExportSession I can get file on my storage. But I'd like to get it as NSData the same as MP3 files. Do you know reasone or solution?Opinionative
i have used this code, it return me 15 MB data while i selected one Mp3 with 5MB. Can you help me so that i can convert the Mp3 in actual weight .. ThanksPuissant

© 2022 - 2024 — McMap. All rights reserved.