ras*_*hii 2 iphone assets nsdata mpmediaitem
我从iPhone设备上检索了所有音乐和视频.我现在被困在保存到我的应用程序中,我无法从文件中获取原始数据.任何人都可以帮我找到解决方案.这是我用来获取音乐文件的代码.
MPMediaQuery *deviceiPod = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [deviceiPod items];
for (MPMediaItem *media in itemsFromGenericQuery){
//i get the media item here.
}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为NSData?这就是我试图获取数据的原因
audioURL = [media valueForProperty:MPMediaItemPropertyAssetURL];//here i get the asset url
NSData *soundData = [NSData dataWithContentsOfURL:audioURL];
Run Code Online (Sandbox Code Playgroud)
使用它对我来说毫无用处.我得到了来自的数据LocalAssestURL.任何解决方案.提前致谢
小智 9
这不是一项微不足道的任务 - Apple的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];
Run Code Online (Sandbox Code Playgroud)
这里data将包含轨道的原始PCM数据; 您可以使用某种编码来压缩它,例如我使用FLAC编解码器库.
| 归档时间: |
|
| 查看次数: |
3651 次 |
| 最近记录: |