Hello dear programmers.
I am using a global variable to run a file from the command line. This is probably not correct, but it works on both Mac and Windows. The only thing I can’t figure out is how to enable autoplay when clicking on a file. If I use the function in the main, then the player stops working correctly.
The code:
Main.cpp
#include <JuceHeader.h>
#include "PlayingSoundFilesTutorial_01.h"
//==============================================================================
String pathWay; //global variable
//==============================================================================
class Application : public JUCEApplication
{
public:
................................
void initialise (const String& commandLine) override
{
mainWindow.reset (new MainWindow (getApplicationName()));
String potentialPath = commandLine.unquoted();
File file;
if (File::isAbsolutePath (potentialPath))
{
file = File (potentialPath);
pathWay = file.getFullPathName();
}
}
void shutdown() override {...}
===============================================================
void systemRequestedQuit() override {...}
void anotherInstanceStarted (const String& commandLine) override
{
String potentialPath = commandLine.unquoted();
File file;
if (File::isAbsolutePath (potentialPath))
{
file = File (potentialPath);
pathWay = file.getFullPathName();
}
} //==========================================================================
class MainWindow : public DocumentWindow
{
................................
};
private:
std::unique_ptr<MainWindow> mainWindow;
};
//==============================================================================
START_JUCE_APPLICATION (Application)
MainContentComponent.h
#pragma once
extern String pathWay;
//==============================================================================
class MainContentComponent: public AudioAppComponent,
public ChangeListener
{...}
public: {...}
~MainContentComponent() override {...}
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override {...}
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override {...}
void releaseResources() override {...}
................................
private:
void openButtonClicked()
{
chooser = std::make_unique<FileChooser> ("Select a Wave file to play...",
File{},
"*.wav, *.aiff, *.mp3, *.mp4");
auto chooserFlags = FileBrowserComponent::openMode
| FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
{
auto file = fc.getResult();
if (file != File{})
{
auto* reader = formatManager.createReaderFor (file);
if (reader != nullptr)
{
auto newSource = std::make_unique<AudioFormatReaderSource> (reader, true);
transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate);
playButton.setEnabled (true);
readerSource.reset (newSource.release());
DBG(reader->getFormatName());
DBG(file.getFullPathName());
pathWay = file.getFullPathName(); //global variable
grabKeyboardFocus();
if (autoPlay == 1)
changeState (Starting);
else
changeState (Stopped);
}
}
});
}
void playButtonClicked()
{
File file {pathWay}; //global variable
if (file.exists())
{
auto* reader = formatManager.createReaderFor (file);
if (reader != nullptr)
{
auto newSource = std::make_unique<AudioFormatReaderSource> (reader, true);
transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate);
playButton.setEnabled (true);
readerSource.reset (newSource.release());
DBG(reader->getFormatName());
DBG(file.getFullPathName());
changeState (Starting);
}
}
else
changeState (Starting);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};
Thank you all in advance for your response.