在iPhone中将NSData从音乐文件中删除 [英] Getting NSData out of music file in iPhone

查看:157
本文介绍了在iPhone中将NSData从音乐文件中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从iPhone设备中检索了所有音乐和视频。我现在被困在保存到我的应用程序中,我无法从文件中获取原始数据。任何人都可以帮我找到解决方案。这是我用来获取音乐文件的代码。

I have retrieved all musics and videos from my iPhone device. I am now stuck in saving those in to my application, i am unable to get the raw data out of the file. Can any one help me to find a solution for this. this is the code i used to get the music files.

MPMediaQuery *deviceiPod = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [deviceiPod items];
for (MPMediaItem *media in itemsFromGenericQuery){
 //i get the media item here.
}

如何将其转换为NSData?
这是我试图获取数据

How to convert it to NSData ?? This is what i tried to get the data

audioURL = [media valueForProperty:MPMediaItemPropertyAssetURL];//here i get the asset url
NSData *soundData = [NSData dataWithContentsOfURL:audioURL];

使用它对我来说毫无用处。我从 LocalAssestURL 获取数据。任何解决方案。提前致谢

using this was useless for me. i dint get the data from the LocalAssestURL. Any solution for this. Thanks in advance

推荐答案

这不是一项微不足道的任务 - Apple的SDK经常无法为简单的任务提供简单的API。这是我在我的一个调整中使用的代码,以便从资产中获取原始PCM数据。您需要将AVFoundation和CoreMedia框架添加到您的项目中才能使其正常工作:

This is not a trivial task - Apple's SDKs often fail to provide simple APIs for simple tasks. Here's the code I'm using in one of my tweaks in order to get raw PCM data out of the asset. You'll need to add the AVFoundation and the CoreMedia framework to your project in order to get this working:

#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];

此处数据将包含原始PCM数据轨道;您可以使用某种编码来压缩它,例如我使用FLAC编解码器库。

Here data will contain the raw PCM data of the track; you can use some kind of encoding to compress it, for example I use the FLAC codec library.

请参阅原始源代码

这篇关于在iPhone中将NSData从音乐文件中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆