Abdul Samad Khan
Abdul Samad Khan

Reputation: 9

Audio waveform with .raw input

i want to display audio waveform, i got this code it takes .raw audio input and shows audio waveform but when i put .3gp,.mp3 audio i get white noise can anybody help how can i make it work with .3gp as i need to run it using .3gp audio.

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

test1 is a .raw file

final 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);
}

Upvotes: 0

Views: 212

Answers (1)

S G
S G

Reputation: 802

What do you mean by white noise?

.3gp,.mp3 are compressed audio so you are required to uncompress the stream and then display.

Upvotes: 1

Related Questions