我有一个应用程序,可以播放基于谷歌文档的播放列表,了解如何创建和音频应用程序https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app
我想添加一个均衡器,像这个https://github.com/Yalantis/Horizon,但我找不到如何获得所需的信息,我以前从未使用过声音,所以我有点迷路了。
根据文档,我应该首先:"初始化地平线对象,参数指的是你的声音:">
mHorizon = Horizon(
glSurfaceView, ResourcesCompat.getColor(resources, R.color.grey2),
RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_ENCODING_BIT //Where to get these 3 constants?
)
然后:"更新Horizon调用updateView方法,用大块的声音数据继续:">
val buffer = ByteArray(//Where to get the bytes?)
mHorizon!!.updateView(buffer)
我如何获取这些数据?我查看了android文档,但什么也找不到。
您需要向Exoplayer添加一个自定义RendererFactory来获取音频字节。请参阅以下代码:
val rendererFactory = RendererFactory(this, object : TeeAudioProcessor.AudioBufferSink {
override fun flush(sampleRateHz: Int, channelCount: Int, encoding: Int) {
}
override fun handleBuffer(buffer: ByteBuffer) {
//pass bytes to the your function
}
})
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, rendererFactory,DefaultTrackSelector())
您将在ByteBuffer中获得字节,要将其转换为ByteArray,请使用以下代码:
try{
val arr = ByteArray(buffer.remaining())
buffer[arr] //pass this array to the required function
}
catch(exception:Exception)
{
// handle exception here
}