0

我正在尝试将 EZAudio(https://github.com/syedhali/EZAudio)Objective-C 库转换为 Swift 3。我正在其中执行“EZAudioPlayFileExample”部分。

Objective-C

- (void)openFileWithFilePathURL:(NSURL*)filePathURL
{
    self.audioFile = [EZAudioFile audioFileWithURL:filePathURL];
    self.filePathLabel.text = filePathURL.lastPathComponent;

    //
    // Plot the whole waveform
    //
    self.audioPlot.plotType = EZPlotTypeBuffer;
    self.audioPlot.shouldFill = YES;
    self.audioPlot.shouldMirror = YES;

    //
    // Get the audio data from the audio file
    //
    __weak typeof (self) weakSelf = self;
    [self.audioFile getWaveformDataWithCompletionBlock:^(float **waveformData,
                                                         int length)
    {
        [weakSelf.audioPlot updateBuffer:waveformData[0]
                          withBufferSize:length];
    }];
}

斯威夫特 3

func openFileWithFilePathURL(filePathURL: NSURL) {
        self.audioFile = EZAudioFile(url: filePathURL as URL!)

        //
        // Plot the whole waveform
        //
        self.plot.plotType = .buffer
        self.plot.shouldFill = true
        self.plot.shouldMirror = true

        //
        // Get the audio data from the audio file
        //
        weak var weakSelf = self
        self.audioFile.getWaveformData(completionBlock: {(_ waveformData: Float, _ length: Int) -> Void in
            weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length) // error in this line as Float as no subscript members
        })
    }

我知道 Float 参数没有像 [0] 这样的下标成员。但我想转换 Objective-C 代码。或者这里的任何人都使用这个库的 Swift 版本。注意:我想要其中的“EZAudioPlayFileExample”。

4

1 回答 1

0

float **waveformData实际上是一个unsafemutablepointer. 所以你必须unsafemutablepointer在 swift 3.0 中使用。您可以在 swift 中使用 unsafemutablepointer,例如

UnsafeMutablePointer<UnsafeMutablePointer<Float>>

你可以使用喜欢

self.audioFile.getWaveformData(completionBlock: {(_ waveformData: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, _ length: Int) -> Void in
            weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length)  
        })

我刚刚下载了 EZAudio 的旧版本 swift 实现并转换为 swift 3.0。它可以在这个链接
上找到, 我不知道这会对你有什么帮助,但你可以在此基础上进行构建。您也可以参考gitHub 页面上报告的这个问题

于 2017-07-25T13:44:35.583 回答