从iPhone中的音乐文件中获取NSData
我已从iPhone设备中检索到所有音乐和视频。我现在正忙于将这些数据保存到我的应用程序中,我无法从文件中获取原始数据。有人能帮我找到解决办法吗。这是我用来获取音乐文件的代码从iPhone中的音乐文件中获取NSData,iphone,nsdata,assets,mpmediaitem,Iphone,Nsdata,Assets,Mpmediaitem,我已从iPhone设备中检索到所有音乐和视频。我现在正忙于将这些数据保存到我的应用程序中,我无法从文件中获取原始数据。有人能帮我找到解决办法吗。这是我用来获取音乐文件的代码 MPMediaQuery *deviceiPod = [[MPMediaQuery alloc] init]; NSArray *itemsFromGenericQuery = [deviceiPod items]; for (MPMediaItem *media in itemsFromGenericQuery){ //
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
获取数据。任何解决办法。提前感谢这不是一项简单的任务-苹果的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];
#导入。iphone 4上的单个音频文件的此代码需要2秒以上的时间。有没有办法让这个文件读得更快?嗨,对于压缩,我使用了zlib,但它几乎没有减少文件大小。我使用了这里的代码-你知道为什么吗?我检查了它,对于mp3来说,它工作得非常好。但是,iTunes中的文件返回空数据。当我使用AVAssetExportSession时,我可以在我的存储器中获取文件。但我想把它作为NSData和MP3文件一样。你知道原因或解决方案吗?我用过这个代码,它返回我15 MB的数据,而我选择了一个5 MB的Mp3。你能帮我把Mp3换算成实际重量吗。。谢谢