I have tried to play an audiofile by referring to the JUCE AudioDemoPlaybackPage module and adding to a barebones GUI window created by the IntroJucer. My intent is to see a window with "Hello, World!" and hear the audiofile (played automatically when run).
This compiles without error, and when I run it, I see "Hello, World!", but hear nothing. Am I getting close?
Here are my MainComponent.h and MainComponent.cpp files.
MainComponent.h:
class MainContentComponent : public Component { public: MainContentComponent(); ~MainContentComponent(); void paint (Graphics&); void resized(); void playAudioFile(); private: AudioDeviceManager deviceManager; AudioFormatManager formatManager; TimeSliceThread thread; AudioSourcePlayer audioSourcePlayer; AudioTransportSource transportSource; ScopedPointer<AudioFormatReaderSource> currentAudioFileSource; File audioFile; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent) };
Here is MainComponent.cpp:
#include "MainComponent.h "MainContentComponent::MainContentComponent() : thread("audio") { setSize (500, 400); AudioDeviceManager deviceManager; deviceManager.initialise ( 2, 0, nullptr, true, "", 0 ); formatManager.registerBasicFormats(); deviceManager.addAudioCallback (&audioSourcePlayer); audioSourcePlayer.setSource (&transportSource); thread.startThread (3); } MainContentComponent::~MainContentComponent() { transportSource.setSource (nullptr); audioSourcePlayer.setSource (nullptr); deviceManager.removeAudioCallback (&audioSourcePlayer); } void MainContentComponent::paint (Graphics& g) { g.fillAll (Colour (0xffeeddff)); g.setFont (Font (16.0f)); g.setColour (Colours::black); g.drawText ("Hello World!", getLocalBounds(), Justification::centred, true); } void MainContentComponent::resized() { } void MainContentComponent::playAudioFile() { transportSource.stop(); transportSource.setSource (nullptr); currentAudioFileSource = nullptr; const File& audioFile = String("~/Test.wav"); AudioFormatReader* reader = formatManager.createReaderFor (audioFile); if (reader != nullptr) { currentAudioFileSource = new AudioFormatReaderSource (reader, true); transportSource.setSource (currentAudioFileSource, 32768, &thread, reader->sampleRate); } transportSource.setPosition (0); transportSource.start(); }