【问题标题】:iPhone play caf audio backwards [duplicate]iPhone向后播放caf音频[重复]
【发布时间】:2012-08-11 06:39:19
【问题描述】:

我正在编写一个应用程序,我需要在其中录制音频并向后播放。我已经使用 AVAudioRecorder 将音频录制到一个 caf 文件中,并且我已经能够使用 AVAudioPlayer 和 MPMoviePlayerController 向前播放它。我尝试将 MPMoviePlayerController.currentPlaybackRate 设置为 -1,但它不会产生任何噪音。通过研究,我发现我需要逐字节反转音频文件,但我不知道该怎么做。有没有办法将caf文件读取到数组并从数组中写入?任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios audio caf


    【解决方案1】:

    我开发了一个示例应用程序,它记录用户所说的内容并向后播放。我已经使用 CoreAudio 来实现这一点。 Link to app code.

    因为每个样本的大小为 16 位(2 字节)(单声道)(这取决于您用于记录的属性)。 您可以通过从记录结束开始并向后读取将其复制到不同的缓冲区来一次加载每个样本。当您到达数据的开头时,您已经反转了数据并且播放将被反转。

    // set up output file
    AudioFileID outputAudioFile;
    
    AudioStreamBasicDescription myPCMFormat;
    myPCMFormat.mSampleRate = 16000.00;
    myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
    myPCMFormat.mFormatFlags =  kAudioFormatFlagsCanonical;
    myPCMFormat.mChannelsPerFrame = 1;
    myPCMFormat.mFramesPerPacket = 1;
    myPCMFormat.mBitsPerChannel = 16;
    myPCMFormat.mBytesPerPacket = 2;
    myPCMFormat.mBytesPerFrame = 2;
    
    
    AudioFileCreateWithURL((__bridge CFURLRef)self.flippedAudioUrl,
                           kAudioFileCAFType,
                           &myPCMFormat,
                           kAudioFileFlags_EraseFile,
                           &outputAudioFile);
    // set up input file
    AudioFileID inputAudioFile;
    OSStatus theErr = noErr;
    UInt64 fileDataSize = 0;
    
    AudioStreamBasicDescription theFileFormat;
    UInt32 thePropertySize = sizeof(theFileFormat);
    
    theErr = AudioFileOpenURL((__bridge CFURLRef)self.recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile);
    
    thePropertySize = sizeof(fileDataSize);
    theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);
    
    UInt32 dataSize = fileDataSize;
    void* theData = malloc(dataSize);
    
    //Read data into buffer
    UInt32 readPoint  = dataSize;
    UInt32 writePoint = 0;
    while( readPoint > 0 )
    {
        UInt32 bytesToRead = 2;
    
        AudioFileReadBytes( inputAudioFile, false, readPoint, &bytesToRead, theData );
        AudioFileWriteBytes( outputAudioFile, false, writePoint, &bytesToRead, theData );
    
        writePoint += 2;
        readPoint -= 2;
    }
    
    free(theData);
    AudioFileClose(inputAudioFile);
    AudioFileClose(outputAudioFile);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多