android.media.AudioFormat 中的 AudioFormat 不是 public

AudioFormat is not public in android.media.AudioFormat

我正在尝试开发像 Shazam 这样的 Android 应用程序。我搜索了 Shazam 是如何在 Google 上工作的,我找到了 this to read。如您所见,它首先录制歌曲。但我在录制代码时遇到了问题,因为 Android Studio 显示该代码有红色下划线的错误。

这是我的代码:

private AudioFormat getFormat() {
    float sampleRate = 44100;
    int sampleSizeInBits = 16;
    int channels = 1;          //mono
    boolean signed = true;     //Indicates whether the data is signed or unsigned
    boolean bigEndian = true;  //Indicates whether the audio data is stored in big-endian or little-endian order
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}

是用来格式化录音的。当我将该代码复制到 main activity 时,它显示如下错误:

当我将光标悬停在 on error 上时,它显示 "AudioFormat is not public in android.media.AudioFormat. Cannot be accessed from outside package"。我该如何解决? link中的代码是不是我下面写错了?我一直在搜索 Android 的教程代码来开发类似于 Shazam 应用程序的东西。

为 Andrew Cheong 的回答编辑

我知道为什么因为 Cheong 的回答所以我这样使用

private AudioFormat getFormat() {
        float sampleRate = 44100;
        int sampleSizeInBits = 16;
        int channels = 1;          //mono
        boolean signed = true;     //Indicates whether the data is signed or unsigned
        boolean bigEndian = true;  //Indicates whether the audio data is stored in big-endian or little-endian order

        return new AudioFormat.Builder().setSampleRate(Math.round(sampleRate)).build();
    }

但是正如你在代码中看到的,我只能找到setSampleRate()来设置采样率。我找不到其他方法来设置 sampleSizeInBits、channels、signed 和 bigEndian。我不知道如何设置它们。如何设置其余变量?

如果您查看 the documentation for AudioFormat,您可能会注意到它有一个 "Builder Class."

class   AudioFormat.Builder
        Builder class for AudioFormat objects. 

Builder class for AudioFormat objects. Use this class to configure and create an AudioFormat instance. By setting format characteristics such as audio encoding, channel mask or sample rate, you indicate which of those are to vary from the default behavior on this device wherever this audio format is used. See AudioFormat for a complete description of the different parameters that can be used to configure an AudioFormat instance.

这是 build() 方法。

这是应用程序设计中的一种"pattern",如果你不是设计模式的学生,它有点抽象/难以理解,但无论如何here's a relevant article