Android平滑音频波形
创始人
2024-10-09 13:01:16
0

在 Android 中,可以使用 AudioTrack 类以平滑的方式显示音频波形。

以下是一个简单的示例代码:

private void renderWaveform(AudioTrack audioTrack, short[] waveform) {
    final int sampleRate = audioTrack.getSampleRate();
    final int channelCount = audioTrack.getChannelCount();
    final int waveformSize = waveform.length;

    final float[] waveformPoints = new float[waveformSize * 4];

    for (int i = 0; i < waveformSize; i++) {
        final float level = waveform[i] / 32767.0f;
        final int x = (int) (((float) i / (float) waveformSize) * (float) getWidth());
        final int y = (int) ((0.5f + (level * 0.5f)) * (float) getHeight());

        waveformPoints[(i * 4)] = x;
        waveformPoints[(i * 4) + 1] = (y * channelCount) - 1;
        waveformPoints[(i * 4) + 2] = x;
        waveformPoints[(i * 4) + 3] = (y * channelCount) + 1;
    }

    final Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(2.0f);

    final Canvas canvas = new Canvas();
    final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);

    canvas.drawLines(waveformPoints, paint);

    audioTrack.write(bitmapToByteArray(bitmap), 0, bitmap.getByteCount());
}

private byte[] bitmapToByteArray(Bitmap bitmap) {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.ARGB_8888, 100, outputStream);
    return outputStream.toByteArray();
}

相关内容

热门资讯

必备辅助推荐!we-poker... 必备辅助推荐!we-poker软件(辅助挂)竟然真的是有挂(有挂教程辅助器)小薇(辅助器软件下载)致...
程序员教你!hhpoker万能... 程序员教你!hhpoker万能辅助器(辅助挂)真是是有挂(有挂规律辅助插件)1、很好的工具软件,可以...
记者揭秘!wepoker有脚本... 记者揭秘!wepoker有脚本吗(辅助挂)本来真的有挂(有挂详细辅助工具)该软件可以轻松地帮助玩家将...
科普分享!hhpoker哪个俱... 科普分享!hhpoker哪个俱乐部靠谱(辅助挂)都是存在有挂(有挂方法辅助工具)1、上手简单,内置详...
必备科技!hhpoker的辅助... 必备科技!hhpoker的辅助是真的吗(辅助挂)确实是有挂(有挂神器辅助工具)是不是有人用挂微扑克w...
我来教教你!aapoker辅助... 我来教教你!aapoker辅助工具安全吗(辅助挂)好像存在有挂(有挂头条辅助脚本)1、游戏颠覆性的策...
科技分享!aapoker辅助器... 科技分享!aapoker辅助器是真的吗(辅助挂)原来存在有挂(有挂透视辅助器)运辅助工具,进入游戏界...
解密关于!pokemmo手机脚... 解密关于!pokemmo手机脚本辅助器(辅助挂)一贯有挂(有挂神器辅助攻略)1)辅助插件:进一步探索...
必备科技!wepoker怎么设... 必备科技!wepoker怎么设置盖牌(辅助挂)一直真的有挂(有挂方式辅助脚本)1)有没有挂:进一步探...
专业讨论!wepoker透视脚... 专业讨论!wepoker透视脚本下载(辅助挂)都是真的有挂(有挂规律辅助工具)1、每一步都需要思考,...