HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 来自MPMediaItem的NSData大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都可以帮我如何将从媒体库中选择的歌曲转换为NSData?
我需要一个接受Media项的函数,并返回该特定媒体项的NSData.

提前致谢

解决方法

您可以使用此代码

@H_446_15@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,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 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,size,outBytes); CMSampleBufferInvalidate(buffer); CFRelease(buffer); [data appendBytes:outBytes length:size]; free(outBytes);

大佬总结

以上是大佬教程为你收集整理的iphone – 来自MPMediaItem的NSData全部内容,希望文章能够帮你解决iphone – 来自MPMediaItem的NSData所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。