Playing audio file in Android

Hi, i’m trying to perform filter on a file from Android and play it real-time, but i can not manage to firstly play a .wav file that i passed from my kotlin code to work. Here’s my code:

extern "C" {
JNIEXPORT void JNICALL Java_com_juce_test_MainActivityKt_playAudio(JNIEnv *env, jclass clazz, jstring filePath) {
    auto convertedPath = env->GetStringUTFChars(filePath, JNI_FALSE);
    juce::String audioPath((juce::CharPointer_UTF8(convertedPath)));
    juce::File audioFile(audioPath);

    juce::AudioFormatManager formatManager;
    formatManager.registerBasicFormats();

    juce::AudioFormatReader *audioFormatReader(
            formatManager.createReaderFor(audioFile));

    std::unique_ptr<juce::AudioFormatReaderSource> tempSource(
            new juce::AudioFormatReaderSource(audioFormatReader, true));

    if (audioFormatReader != nullptr) {
        auto *transportSource = new juce::AudioTransportSource();
        transportSource->setSource(tempSource.get());
        transportSource->setPosition(0.0);
        transportSource->start();
    }

    env->ReleaseStringUTFChars(filePath, convertedPath);
}
}

I also checked that the juce::File does exists, the length of the audio is correct and isPlaying() return true. However no sound was playing, nothing appear in log console as well.
Appreciate any help. Thanks in advance

I am not seeing any code in that would actually play the audio. Getting the audio to actually play is a bit more involved.

Thanks for your reply, i thought a call to start() should play the audio

No, the AudioTransportSource doesn’t itself play the audio, you also need an AudioDeviceManager instance, an AudioSourcePlayer and everything set up so that they stay alive as long as the audio needs to be played. Putting it all into one function probably isn’t going to work…

1 Like

Thank you, i’ll try to figure it out. Btw can i ask which class should i use if i want to apply effect to the AudioSourcePlayer?