带.raw输入的音频波形

时间:2017-04-10 18:40:12

标签: android audio inputstream

我想要显示音频波形,我得到了这个代码它需要.raw音频输入和显示音频波形但是当我把.3gp,.mp3音频我得到白噪声可以任何人帮助我怎么能使它工作.3gp因为我需要使用.3gp音频运行它。

InputStream is = getResources()。openRawResource(R.raw.test1);

test1是.raw文件

最终WaveformView mPlaybackView =(WaveformView)findViewById(R.id.playbackWaveformView);

    short[] samples = null;
    try {
        samples = getAudioSample();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (samples != null) {
        final FloatingActionButton playFab = (FloatingActionButton) findViewById(R.id.playFab);

        mPlaybackThread = new PlaybackThread(samples, new PlaybackListener() {
            @Override
            public void onProgress(int progress) {
                mPlaybackView.setMarkerPosition(progress);
            }
            @Override
            public void onCompletion() {
                mPlaybackView.setMarkerPosition(mPlaybackView.getAudioLength());
                playFab.setImageResource(android.R.drawable.ic_media_play);
            }
        });
        mPlaybackView.setChannels(1);
        mPlaybackView.setSampleRate(PlaybackThread.SAMPLE_RATE);
        mPlaybackView.setSamples(samples);

        playFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mPlaybackThread.playing()) {
                    mPlaybackThread.startPlayback();
                    playFab.setImageResource(android.R.drawable.ic_media_pause);
                } else {
                    mPlaybackThread.stopPlayback();
                    playFab.setImageResource(android.R.drawable.ic_media_play);
                }
            }
        });
    }
}

@Override
protected void onStop() {
    super.onStop();

    mPlaybackThread.stopPlayback();
}

private short[] getAudioSample() throws IOException{


    InputStream is =getResources().openRawResource(R.raw.test1);

    byte[] data;
    try {
        data = IOUtils.toByteArray(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }

    ShortBuffer sb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    short[] samples = new short[sb.limit()];
    sb.get(samples);
    return samples;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:1)

白噪声是什么意思?

.3gp,.mp3是压缩音频,因此您需要解压缩流然后再显示。