我需要一个接受Media项的函数,并返回该特定媒体项的NSData.
提前致谢
解决方法 您可以使用此代码MPMediaItem *item = // obtain the media itemNSautoreleasePool *pool = [[NSautoreleasePool alloc] init];// Get raw PCM data from the trackNSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];NSMutableData *data = [[NSMutableData alloc] init];const uint32_t sampleRate = 16000; // 16k sample/secconst uint16_t bitDepth = 16; // 16 bit/sample/channelconst 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,AVlinearPCMIsfloatKey,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 subsequentlywhile ([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,size,outBytes);CMSampleBufferInvalIDate(buffer);CFRelease(buffer);[data appendBytes:outBytes length:size];free(outBytes);总结
以上是内存溢出为你收集整理的iphone – 来自MPMediaItem的NSData全部内容,希望文章能够帮你解决iphone – 来自MPMediaItem的NSData所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)