THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 23/10/2012 at 16:08, xxxxxxxx wrote:
Hi, ScottA
I really not good at English but I explain that.
Originally posted by xxxxxxxx
Bool myResDialog::Execute(BaseDocument *doc)
{
//My thread is named "SoundThread"
//The instance of it I named "PlaySoundThread"
SoundThread PlaySoundThread;
PlaySoundThread.Start(THREADMODE_ASYNC,THREADPRIORITY_NORMAL);//<---Does not work
dlg.Open(DLG_TYPE_ASYNC,PLUGIN_ID, -1, -1, 300,150);
return TRUE;
}
Bool myResDialog::Execute(BaseDocument *doc)
is a function.
so, SoundThread PlaySoundThread; <-- variale is local
PlaySoundThread(allocated memory) is destroyed at Execute() return;
so, you have to use pointer. like this
(SoundThread* PlaySoundThread = gNew SoundThread ;)
then memory is out there but you have to retun memory your-self to OS at Thread is end.
SoundThread* PlaySoundThread;
Bool myResDialog::Execute(BaseDocument *doc)
{
//My thread is named "SoundThread"
//The instance of it I named "PlaySoundThread"
PlaySoundThread = gNew SoundThread ;
PlaySoundThread->Start(THREADMODE_ASYNC,THREADPRIORITY_NORMAL); //if uesd pointer it Still exist in memory
AsyncDlg = gNew dlg;
dlg->Open(DLG_TYPE_ASYNC,PLUGIN_ID, -1, -1, 300,150);
return TRUE;
}
then you can aceess to thread by pointer variable that PlaySoundThread and dlg
so, this is complete code of play sound.
//#include "windows.h" // for check freeing memory with pop up
#include "c4d.h"
#include "c4d_symbols.h"
#include "lib_snd.h"
class wavThread : public C4DThread
{
void Main();
public:
wavThread();
~wavThread();
BaseSound *sound;
GePlaySnd *playsound;
LReal PositionEstimate;
Bool isPlaying;
LONG error;
const CHAR* GetThreadName();
};
wavThread::wavThread()
{
}
wavThread::~wavThread()
{
//GePrint("Deleted!!!!!!");
}
void wavThread::Main()
{
sound = BaseSound::Alloc();
if (!sound) {error = 1; End();}
if (!sound->Load(Filename("C:\\GangnamStyle.wav"))) error = 2;
GeSndInfo sndinfo(sound);
playsound = GePlaySnd::Alloc();
if (!playsound) {error = 3;End();}
playsound->Open(sound);
Real second = sndinfo.GetLength().Get();
playsound->Scrub(0, second);
while(isPlaying) //Playing music don't need this but this is support pause command.
{
GeSleep(1000);
GePrint("pos: " + RealToString(playsound->GetPositionEstimate()));
if(playsound->GetPositionEstimate() > second) break;
}
playsound->Close();
if(sound) BaseSound::Free(sound);
if(playsound) GePlaySnd::Free(playsound);
}
const CHAR* wavThread::GetThreadName()
{
return "wavSound";
}
class SoundDlg;
SoundDlg *dlg;
class SoundDlg : public GeDialog
{
private:
wavThread th;
public :
virtual Bool CreateLayout(void);
virtual Bool Command(LONG id,const BaseContainer &msg);
Bool KillTread();
Bool IsRunning();
};
Bool SoundDlg::IsRunning()
{
if(th.IsRunning()) return TRUE;
return 0;
}
Bool SoundDlg::KillTread()
{
th.isPlaying=0;
return 1;
}
Bool SoundDlg::CreateLayout()
{
GroupBegin(0,BFH_SCALEFIT|BFV_SCALEFIT,2,0,"",BFV_GRIDGROUP_ALLOW_WEIGHTS);
AddButton(1000,BFH_SCALEFIT|BFV_SCALEFIT,0,0,"Play Sound");
AddButton(1001,BFH_SCALEFIT|BFV_SCALEFIT,0,0,"Pause Sound");
AddButton(1002,BFH_SCALEFIT|BFV_SCALEFIT,0,0,"Stop Sound");
AddButton(1003,BFH_SCALEFIT|BFV_SCALEFIT,0,0,"Thread Stop");
GroupEnd();
return 1;
}
Bool SoundDlg::Command(LONG id,const BaseContainer &msg)
{
switch (id)
{
case 1000:
if(th.IsRunning())
{
GePrint("running");
th.playsound->StartAt(th.PositionEstimate); break;
}else{
GePrint("new start");
th.isPlaying = 1;
th.Start();
break;
}
case 1001:
if(th.IsRunning())
{
th.PositionEstimate = th.playsound->GetPositionEstimate();
th.playsound->Stop();
}
break;
case 1002:
if(th.IsRunning())
{
th.playsound->Stop();
th.PositionEstimate =0;
}
break;
case 1003:
if(th.IsRunning())
{
th.playsound->Stop();
th.playsound->Close();
th.isPlaying = 0;
th.End();
}
break;
}
return TRUE;
}
class PlaySoundCmd : public CommandData
{
public:
virtual Bool Execute(BaseDocument *doc);
virtual LONG GetState(BaseDocument *doc);
};
Bool PlaySoundCmd::Execute(BaseDocument *doc)
{
if(dlg) gDelete(dlg);
dlg = gNew SoundDlg;
dlg->Open(DLG_TYPE_ASYNC,1000011,-1,-1);
return TRUE;
}
LONG PlaySoundCmd::GetState(BaseDocument *doc)
{
return CMD_ENABLED;
}
#define ID_PlaySound 1000008
Bool RegisterPlaySoundCmd(void)
{
return RegisterCommandPlugin(ID_PlaySound,String("Play Sound"),0/*PLUGINFLAG_HIDEPLUGINMENU*/,NULL,String(),gNew PlaySoundCmd);
}
void FreeSounddlg()
{
if (dlg)
{
if(dlg->IsRunning()) dlg->KillTread();
gDelete(dlg);
//MessageBox(NULL,"Olleh !!","Notification",MB_OK);
// for check freeing memory with pop up. this is win api
}
}
main.cpp
#include "c4d.h"
void FreeSounddlg();
Bool PluginStart(void)
{
if (!RegisterPlaySoundCmd()) return FALSE;
return TRUE;
}
Bool PluginMessage(LONG id, void *data)
{
switch (id)
{
case C4DPL_INIT_SYS:
if (!resource.Init()) return FALSE; // don't start plugin without resource
return TRUE;
case C4DMSG_PRIORITY:
return TRUE;
case C4DPL_ENDACTIVITY:
FreeSounddlg(); // you have to do this Freein memory
return TRUE;
}
return FALSE;
}
Even You don't have to use Thread that play sound.
because dialog's variable is still exist until freeing the dialog.
but I used Thread for help to you.
Important thing is below
// declaration
class SoundDlg;
SoundDlg *dlg;
// Allocate and get pointer address(dlg)
Bool PlaySoundCmd::Execute(BaseDocument *doc)
{
if(dlg) gDelete(dlg);
dlg = gNew SoundDlg;
dlg->Open(DLG_TYPE_ASYNC,1000011,-1,-1);
return TRUE;
}
// Freeing memory
main.cpp
case C4DPL_ENDACTIVITY:
FreeSounddlg(); // you have to do this Freeing memory
return TRUE;
I hope this is of some help to you.