2011-01-11 42 views

回答

6

您可以使用ExtAudioFileGetProperty從現有的m4a音頻文件中獲取ASBD。

欲瞭解更多詳情Click here

+0

那麼當您在m4a文件上運行ExtAudioFileGetProperty時,結果如何? – user1021430 2016-03-08 01:32:53

0

你可以用2個(至少)不同的方法獲得文件的ASBD。您可以使用'ExtAudioFileGetProperty'或'AudioFileGetProperty'。

AudioFileGetProperty:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"m4a"]; 
CFURLRef soundFileURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundFilePath]; 

if (soundFileURL != nil) { 

    AudioFileID audioFile; 
    OSStatus theError = noErr; 

    theError = AudioFileOpenURL(soundFileURL, 
           kAudioFileReadPermission, 
           0, 
           &audioFile); 
    if(theError != noErr) { 
     printf("AudioFileOpenURL failed!"); 
     return; 
    } 

    AudioStreamBasicDescription asbd; 
    UInt32 size = sizeof(asbd); 
    theError = AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &size, &asbd); 

    if(theError != noErr) { 
     printf("kAudioFilePropertyDataFormat failed!"); 
     return; 
    } else { 
     printf("Sample Rate : %f\n", asbd.mSampleRate); 
     /* 
     Float64    mSampleRate; 
     AudioFormatID  mFormatID; 
     AudioFormatFlags mFormatFlags; 
     UInt32    mBytesPerPacket; 
     UInt32    mFramesPerPacket; 
     UInt32    mBytesPerFrame; 
     UInt32    mChannelsPerFrame; 
     UInt32    mBitsPerChannel; 
     UInt32    mReserved; 
     */ 
    } 
} 

ExtAudioFileGetProperty:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"m4a"]; 
CFURLRef soundFileURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundFilePath]; 

if (soundFileURL != nil) { 
    OSStatus theError = noErr; 

    ExtAudioFileRef fileRef; 
    theError = ExtAudioFileOpenURL(soundFileURL, &fileRef); 

    if(theError != noErr) { 
     printf("ExtAudioFileOpenURL failed!"); 
     return; 
    } 

    AudioStreamBasicDescription asbd; 
    UInt32 size = sizeof(asbd); 
    theError = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_FileDataFormat, &size, &asbd); 
} 
相關問題