iphone – 来自MPMediaItem的NSData

iphone – 来自MPMediaItem的NSData,第1张

概述任何人都可以帮我如何将从媒体库中选择的歌曲转换为NSData? 我需要一个接受Media项的函数,并返回该特定媒体项的NSData. 提前致谢 您可以使用此代码 MPMediaItem *item = // obtain the media itemNSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];// Get raw PC 任何人都可以帮我如何将从媒体库中选择的歌曲转换为NSData?
我需要一个接受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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存