2012-07-12 65 views
0

我知道现在有可能从用户的音乐库中取出一首歌曲,并通过改变速度,音高等进行编辑,但似乎无法找到有关如何访问原始数据的有用教程的链接一首歌。有人能指导我这样的事情,或者有可能是这样的一个图书馆?谢谢!iOS - 访问可修改的原始音乐库数据?

回答

2

下面是一些源代码,你可以开始:

- (void) doSomethingWithAssett:(AVURLAsset *)songAsset { 

NSError * error = nil; 


AVAssetReader * reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error]; 

if (error) { 
    NSLog(@"%@", [error description]); 
} 

AVAssetTrack * songTrack = [songAsset.tracks objectAtIndex:0]; 

NSDictionary* outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys: 

            [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey, 
            //  [NSNumber numberWithInt:44100.0],AVSampleRateKey, /*Not Supported*/ 
            //  [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, /*Not Supported*/ 

            [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, 
            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, 
            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, 
            [NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved, 

            nil]; 


AVAssetReaderTrackOutput* output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:outputSettingsDict]; 

[reader addOutput:output]; 

UInt32 sampleRate,channelCount; 

NSArray* formatDesc = songTrack.formatDescriptions; 
for(unsigned int i = 0; i < [formatDesc count]; ++i) { 
    CMAudioFormatDescriptionRef item = (__bridge CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i]; 
    const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item); 
    if(fmtDesc) { 

     sampleRate = fmtDesc->mSampleRate; 
     channelCount = fmtDesc->mChannelsPerFrame; 

     // NSLog(@"channels:%u, bytes/packet: %u, sampleRate %f",fmtDesc->mChannelsPerFrame, fmtDesc->mBytesPerPacket,fmtDesc->mSampleRate); 
    } 
} 


UInt32 bytesPerSample = 2 * channelCount; 
SInt16 normalizeMax = 0; 

NSMutableData * fullSongData = [[NSMutableData alloc] init]; 
[reader startReading]; 


UInt64 totalBytes = 0; 


SInt64 totalLeft = 0; 
SInt64 totalRight = 0; 
NSInteger sampleTally = 0; 

NSInteger samplesPerPixel = sampleRate/50; 


while (reader.status == AVAssetReaderStatusReading){ 

    AVAssetReaderTrackOutput * trackOutput = (AVAssetReaderTrackOutput *)[reader.outputs objectAtIndex:0]; 
    CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer]; 

    if (sampleBufferRef){ 
     CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef); 

     size_t length = CMBlockBufferGetDataLength(blockBufferRef); 
     totalBytes += length; 


     @autoreleasepool { 
      NSMutableData * data = [NSMutableData dataWithLength:length]; 
      CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, data.mutableBytes); 


      SInt16 * samples = (SInt16 *) data.mutableBytes; 

你有样,与他们做 东西...

+0

难道苹果的App Store指导的这个秋天犯规“9.1应用程序不使用MediaPlayer框架访问音乐库中的媒体将被拒绝“? – 2014-05-05 17:49:17