2012-10-30 4 views
2

에 음악 파일에서있는 NSData 얻기 내 아이폰 장치에서 모든 musics 및 비디오를 검색했다. 나는 지금 내 응용 프로그램에 저장에 갇혀있다, 나는 파일에서 원시 데이터를 얻을 수 없습니다. 어느 누구도이 문제에 대한 해결책을 찾을 수 있습니까? 이것은 음악 파일을 가져 오는 데 사용 된 코드입니다.아이폰

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

NSData로 변환하는 방법 ?? 이 내가이 나에게 쓸모가 없었 사용하여 데이터

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

을 얻기 위해 노력하는 것이다. 나는 LocalAssestURL에서 데이터를 얻는다. 이것을위한 어떤 해결책. 사전

답변

9

이 감사 사소한 일이 아니다 - 애플의 SDK를 종종 간단한 작업에 대한 간단한 API를 제공하지 못한다. 다음은 자산에서 원시 PCM 데이터를 가져 오기 위해 제가 사용하고있는 코드입니다. 당신은이 작업을 진행하기 위해 프로젝트에 AVFoundation와 CoreMedia 프레임 워크를 추가해야합니다 :

여기
#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]; 

data 트랙의 원시 PCM 데이터를 포함합니다; 당신은 그것을 압축하기 위해 어떤 종류의 인코딩을 사용할 수 있습니다. 예를 들어 FLAC 코덱 라이브러리를 사용합니다.

original source code here를 참조하십시오.

+2

awesomely done111 – Kamarshad

+0

이 코드는 아이폰 4의 단일 오디오 파일에 2 초 이상 걸립니다.이 파일을 더 빨리 읽을 수있는 방법이 있습니까? –

+1

@iDev No. [15 chars] –