How to play back simple looped and non-looped wav files?


#1

Probably the noobest problem oh this forum, but here’s the situation: I try to take the playback code from the demo app. I need only the following: play back looped and non-looped wav files, control the volume (from a PropertiesFile). There is no sound at all, I got an error when I check the reader for zero in the following code while creating an instance (I’m on Windows XP):

AudioDeviceManager deviceManager;

class SoundPlayer
{
public:
	SoundPlayer (const File &audioFile, bool loop = false)
	{
	    deviceManager.initialise (2, 2, 0, true, String::empty, 0);
		deviceManager.addAudioCallback (&audioSourcePlayer);
		audioSourcePlayer.setSource (&transportSource);
		currentAudioFileSource = 0;

		transportSource.stop ();
		transportSource.setSource (0);
		AudioFormatManager formatManager;
		formatManager.registerBasicFormats ();
		AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
		if (reader !=0) {
			currentAudioFileSource = new AudioFormatReaderSource (reader, true);
			currentAudioFileSource->setLooping (loop);
			transportSource.setSource (currentAudioFileSource, 32768, reader->sampleRate);
			transportSource.setGain (1);
		} else { 
			logMessage ("AudioFormatReader error"); 
		}
	};

	~SoundPlayer ()
	{
		transportSource.setSource (0);
		audioSourcePlayer.setSource (0);
		deviceManager.removeAudioCallback (&audioSourcePlayer);
		deleteAndZero (currentAudioFileSource);
	};

	void play () 
	{
		transportSource.setPosition (0);
		transportSource.start ();
	};

	void stop () 
	{
		transportSource.stop ();
	};

private: 
	AudioSourcePlayer audioSourcePlayer;
	AudioTransportSource transportSource;
	AudioFormatReaderSource * currentAudioFileSource;
	
};

SoundPlayer * splayer;

Is it okay to have a global device manager?

Thanks for any pointers!


#2

If createReaderFor() is returning 0, then it’s probably because the file doesn’t exist, or is a format that it can’t understand… You could step into it if you need to see exactly what’s failing.

sure, as long as you delete it cleanly when your app closes.


#3