I'm attempting to create two instances of this class which will eventually play music files using Win32's mciSendString features. However to test it since this is the first time I've attempted to use std::thread, I wrote a test(void) method which outputs the class ID which I'd expect to print me a series of 1's and 2's like 12122111112212121212...
I'm getting the following error, the test(void) method indeed exists?
Error 1 error C2064: term does not evaluate to a function taking 0 arguments
#include <iostream>
#include <thread>
typedef enum MusicStatus {
MUSIC_PLAYING = 0,
MUSIC_PAUSED,
MUSIC_STOPPED,
MUSIC_IDLE
} MusicStatus, *pMusicStatus;
class MusicPlayer
{
public:
MusicPlayer(void) {
m_bIsPlaying = false;
m_bIsPaused = false;
}
bool isPaused(void) {
return m_bIsPaused;
}
bool isPlaying(void) {
return m_bIsPlaying;
}
MusicStatus getState(void) {
if ( !m_bIsPlaying && !m_bIsPaused && !m_bIsStopped )
return MUSIC_IDLE;
if ( m_bIsPlaying )
return MUSIC_PLAYING;
if ( m_bIsPaused )
return MUSIC_PAUSED;
if ( m_bIsStopped )
return MUSIC_STOPPED;
return MUSIC_STOPPED;
}
void test(void) {
for ( int m = 0; m < 100; m++ ) {
std::cout << this->n;
}
}
int n;
private:
bool m_bIsPlaying, m_bIsPaused, m_bIsStopped;
};
int main(int argc, char* argv[])
{
MusicPlayer A;
MusicPlayer B;
A.n = 1;
B.n = 2;
std::thread t1(A);
std::thread t2(B);
t1.join();
t2.join();
A.test();
B.test();
system("PAUSE");
return 0;
}
Update: I've made some adjustment, now I'm having an issue with argument list, error: MusicPlayer::play_sound function call missing argument list
#include <iostream>
#pragma comment(lib, "Strmiids.lib")
#include <thread>
#include <dshow.h>
#include "Lib/NSL.h"
typedef enum MusicStatus {
MUSIC_PLAYING = 0,
MUSIC_PAUSED,
MUSIC_STOPPED,
MUSIC_IDLE
} MusicStatus, *pMusicStatus;
class MusicPlayer
{
public:
MusicPlayer() {
m_bIsPlaying = false;
m_bIsPaused = false;
m_bIsStopped = false;
}
bool isPaused() {
return m_bIsPaused;
}
bool isPlaying() {
return m_bIsPlaying;
}
MusicStatus getState() {
if ( !m_bIsPlaying && !m_bIsPaused && !m_bIsStopped )
return MUSIC_IDLE;
if ( m_bIsPlaying )
return MUSIC_PLAYING;
if ( m_bIsPaused )
return MUSIC_PAUSED;
if ( m_bIsStopped )
return MUSIC_STOPPED;
return MUSIC_STOPPED;
}
void playAudio(std::string strFilePath) {
m_strFilePath = strFilePath;
std::thread audioThread(play_sound);
audioThread.join();
}
private:
bool m_bIsPlaying, m_bIsPaused, m_bIsStopped;
std::string m_strFilePath;
void play_sound() {
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(s2ws(m_strFilePath).c_str(), NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
};
int main(void)
{
MusicPlayer A;
A.playAudio("music.mp3");
system("pause");
return 0;
}