2

iPhoneデバイスからすべての音楽とビデオを取得しました。これらをアプリケーションに保存するのに行き詰まり、ファイルから生データを取得できません。誰かが私がこれの解決策を見つけるのを手伝ってくれますか?これは私が音楽ファイルを取得するために使用したコードです。

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。このための任意のソリューション。前もって感謝します

4

1 に答える 1

9

これは簡単な作業ではありません。Apple の SDK は、単純なタスク用の単純な API を提供できないことがよくあります。アセットから生の PCM データを取得するために、微調整の 1 つで使用しているコードを次に示します。これを機能させるには、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 コーデック ライブラリを使用しています。

元のソース コードはこちらを参照してください。

于 2012-10-30T09:17:16.937 に答える