2012-07-12 53 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