Skip to content

5. Picking Audio

Kumar Bibek edited this page Mar 1, 2016 · 3 revisions

Picking audios

Use AudioPicker to pick audio files from your device. You can choose single or multiple audio files from your device.

It is mandatory to set a AudioPickerCallback before triggering the pickAudio(); method, or an exception will be raised.

AudioPicker audioPicker = new AudioPicker(Activity.this);
// audioPicker.allowMultiple();
// audioPicker.
audioPicker.setAudioPickerCallback(new AudioPickerCallback() {
    @Override
    public void onAudiosChosen(List<ChosenAudio> files) {
        // Display Files
    }

    @Override
    public void onError(String message) {
        // Handle errors
    }
});

audioPicker.pickFile();

After this call, you need to submit the onActivityResult(int requestCode, int resultCode, Intent data) to AudioPicker so that the processing might start.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Picker.PICK_AUDIO && resultCode == RESULT_OK) {
        audioPicker.submit(data);
    }
}

The AudioPickerCallback will be triggered once the file has been processed.

The one thing you have to handle is when your Activity is killed when the user is still choosing a photo. In such a scenario, AudioPicker reference will be destroyed since the Activity will be re-created. An additional check is required to handle this scenario. You just have to create a new AudioPicker object, attach the callback and call the AudioPicker.submit(data) method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Picker.PICK_AUDIO && resultCode == RESULT_OK) {
        if(audioPicker == null) {
            audioPicker = new AudioPicker(Activity.this);
            audioPicker.setAudioPickerCallback(this);
        }
        audioPicker.submit(data);
    }
}
Clone this wiki locally