AudioFormat在android.media.AudioFormat中不公开

问题描述 投票:1回答:1

我正在尝试像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);
}

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

enter image description here

当我将光标悬停在on错误上时,它说“AudioFormat在android.media.AudioFormat中不公开。无法从外部包访问”。我该如何解决?我所遵循的链接中的代码是错误的吗?我一直在寻找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。我不知道如何设置它们。如何设置其余变量?

android audio fft audioformat
1个回答
2
投票

如果你看一下the documentation for AudioFormat,你可能会注意到它有一个“Builder类”。

class   AudioFormat.Builder
        Builder class for AudioFormat objects. 

AudioFormat对象的Builder类。使用此类配置和创建AudioFormat实例。通过设置音频编码,通道掩码或采样率等格式特征,可以指示在使用此音频格式的任何地方,这些特性与此设备上的默认行为有所不同。有关可用于配置AudioFormat实例的不同参数的完整说明,请参阅AudioFormat。

这是build()方法。

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

© www.soinside.com 2019 - 2024. All rights reserved.