Sign in to follow this  
nosajghoul

direct show & multiple .MP3 files

Recommended Posts

Im trying to use directshow to play mp3 files, and obviously, it aint workin. :P Out of curriosity (and desperation?) I made multiple filter graphs, altho I have a feeling this isnt the way to do it. Heres the dshow source that matters :

#include "cShow.h"
extern HRESULT ghr;

void cShow::InitShow()
{
	isplaying = 0;
	CoInitialize(NULL);

	ghr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
	IID_IGraphBuilder,(void**)&pGraph1);

	ghr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
	IID_IGraphBuilder,(void**)&pGraph2);

	ghr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
	IID_IGraphBuilder,(void**)&pGraph3);

	ghr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
	IID_IGraphBuilder,(void**)&pGraph4);


    pGraph1->QueryInterface(IID_IMediaControl, (void **)&pMediaControl1);
    pGraph1->QueryInterface(IID_IMediaEvent, (void **)&pEvent1);

	pGraph2->QueryInterface(IID_IMediaControl, (void **)&pMediaControl2);
    pGraph2->QueryInterface(IID_IMediaEvent, (void **)&pEvent2);

	pGraph3->QueryInterface(IID_IMediaControl, (void **)&pMediaControl3);
    pGraph3->QueryInterface(IID_IMediaEvent, (void **)&pEvent3);

	pGraph4->QueryInterface(IID_IMediaControl, (void **)&pMediaControl4);
    pGraph4->QueryInterface(IID_IMediaEvent, (void **)&pEvent4);
}


void cShow::StartMusic(int num)
{
	isplaying = true;
	long evCode;

	switch(num)
	{
	case 1:
		pGraph1->RenderFile(L"space2.mp3", NULL);
		pMediaControl1->Run();
		pEvent1->WaitForCompletion(0, &evCode);
		break;
	case 2:
		pGraph2->RenderFile(L"space3.mp3", NULL); 
		pMediaControl2->Run();
		pEvent2->WaitForCompletion(0, &evCode);
		break;
	case 3:
		pGraph3->RenderFile(L"space4.mp3", NULL); 
		pMediaControl3->Run();
		pEvent3->WaitForCompletion(0, &evCode);
		break;
	case 4:
		pGraph4->RenderFile(L"space1.mp3", NULL); 
		pMediaControl4->Run();
		pEvent4->WaitForCompletion(0, &evCode);
		break;
	}
}

void cShow::StopMusic(int num)
{
	isplaying = false;

	switch(num)
	{
	case 1:
		pMediaControl1->Stop();
		break;
	case 2:
		pMediaControl2->Stop();
		break;
	case 3:
		pMediaControl3->Stop();
		break;
	case 4:
		pMediaControl4->Stop();
		break;
	}
	
}

and heres the logic for when to play what music :
	case 1: //start screen
		if(show->songplaying != 1)
		{
			show->StopMusic(show->songplaying);
			show->songplaying = 1;
			show->isplaying = false;
		}
		if(!(show->isplaying))
		{
			show->isplaying = true;
			show->songplaying = 1;
			show->StartMusic(show->songplaying);
		}

.
.
.
.
	case 5:
		Play(level);
		if(show->songplaying != level%3 + 1)
		{
			show->StopMusic(show->songplaying);
			show->songplaying = level%3 + 1;
			show->isplaying = false;
		}

		if(!(show->isplaying))
		{
			show->isplaying = true;
			show->songplaying = level%3 + 1;
			show->StartMusic(show->songplaying);
		}
		break;


The results are that multiple mp3's end up playing simultaneously (damn thats hard to spell), although it does start out as it should. The problem is going back to the start screen and also stopping the music. -Jason

Share this post


Link to post
Share on other sites
Everytime you call cShow::StartMusic() you're calling pGraph1->RenderFile(), that means you're creating the necessary filters to play the mp3 file multiple times.

What you should do:

1 - Call cShow::InitShow()
2 - Create a function cShow::LoadSongs() and call it
3 - Call cShow::StartMusic() and cShow::StopMusic() as you need
4 - Create a function cShow::UnloadSongs() and call it
5 - Call cShow::DeinitShow()

here's how your cShow::LoadSongs() and cShow::StartMusic() functions should look like now:

void cShow::LoadSongs()
{
pGraph1->RenderFile(L"space2.mp3", NULL);
pGraph2->RenderFile(L"space3.mp3", NULL);
pGraph3->RenderFile(L"space4.mp3", NULL);
pGraph4->RenderFile(L"space1.mp3", NULL);
}

void cShow::StartMusic(int num)
{
isplaying = true;
long evCode;

switch(num)
{
case 1:
pMediaControl1->Run();
pEvent1->WaitForCompletion(0, &evCode);
break;
case 2:
pMediaControl2->Run();
pEvent2->WaitForCompletion(0, &evCode);
break;
case 3:
pMediaControl3->Run();
pEvent3->WaitForCompletion(0, &evCode);
break;
case 4:
pMediaControl4->Run();
pEvent4->WaitForCompletion(0, &evCode);
break;
}
}


Hope I helped.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this