我正在尝试开发一个像Shazam这样的Android应用程序。我搜索了Shazam是如何在谷歌上工作的,我找到了这个要读。正如你所看到的,它首先记录了这首歌。但是我的录音代码有问题,因为Android的红色下划线显示了错误。
这是我的代码:
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);
}用于格式化音频记录。当我将该代码复制到主活动中时,它将显示以下错误:

当我将光标悬停在“on错误”时,它的意思是"AudioFormat在android.media.AudioFormat中不是公共的,不能从外部包访问“。我怎么才能修好它?我所遵循的链接中的代码是否错误?我一直在为Android搜索教程代码,以开发类似Shazam的应用程序。
编辑张家祥的答案
我知道为什么,因为张先生的回答,所以我用这样的方法
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、通道、签名和bigEndian。我不知道怎么设置它们。如何设置其余的变量?
发布于 2016-09-22 04:26:54
如果您查看AudioFormat文档,您可能会注意到它有一个"Builder“。
class AudioFormat.Builder
Builder class for AudioFormat objects. AudioFormat对象的生成器类。使用该类配置和创建AudioFormat实例。通过设置格式特征(如音频编码、信道掩码或采样率),您可以指示这些特征中哪些将与此设备上使用此音频格式的默认行为有所不同。有关可用于配置AudioFormat实例的不同参数的完整说明,请参见AudioFormat。
这是build()方法。
这是应用程序设计中的一种“模式”,如果您不是设计模式的学生,而是这是一篇相关文章,那么它就有点抽象/难以理解。
https://stackoverflow.com/questions/39630215
复制相似问题