我正在尝试开发像 Shazam 这样的 Android 应用程序。我搜索了 Shazam 在 Google 上的工作方式,我发现这是可以阅读的。如您所见,它首先录制歌曲。但我的录制代码有问题,因为 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);
}
用于格式化录音。当我将该代码复制到主要活动中时,它显示错误如下:
当我将光标悬停在错误上时,它说“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。我不知道如何设置它们。如何设置其余变量?