|
发表于 2005-5-30 16:18:04
|
显示全部楼层
[quote:82f1b0fba8="alpher"]下一版,会添上。
双人合作。背景音乐,声效
不过要等段时间,因为我还不知道怎么播放声音(因为不想使用gnome,ML1.2不带gnome的)[/quote]
你看看/usr/include/esd.h文件,胡正的黑客背单词就是用这个实现音效的,但他用的是C++。
黑客背单词中的sndserv.h
[code:1]#ifndef __RW_SNDSERV_H__
#define __RW_SNDSERV_H__
#include <glib.h>
enum PLAY_METHOD {PM_MIX,PM_STOP_PRE,PM_AFTER_PRE};
class CSndserv
{
int fd;
public:
gboolean disable;
bool canplay;
CSndserv ();
~CSndserv ();
void play (const gchar *filename);
};
void play_file(const gchar *filename,PLAY_METHOD method);
#endif
[/code:1]
黑客背单词中的sndserv.cpp
[code:1]#include "sndserv.h"
#include "reciteword.h"
#include <cstdio>
#include <cstdlib>
#ifdef G_OS_WIN32
#include "windows.h"
#else
#include <esd.h>
#endif
extern CReciteWord* g_pReciteWord;
CSndserv::CSndserv()
{
#ifdef G_OS_WIN32
canplay = true;
#else
fd=esd_open_sound(NULL);//"localhost";
canplay = (fd>=0);
#endif
if (!canplay)
g_print("esd initialization failed,no sound will be play!\n");
}
CSndserv::~CSndserv()
{
#ifdef G_OS_WIN32
#else
if (fd>=0) esd_close(fd);
#endif
}
void
CSndserv::play(const gchar *filename)
{
#ifdef G_OS_WIN32
#else
if (fd>=0)
esd_play_file(NULL,filename,0); // ???
//esd_play_file ("reciteword", filename, 1);
#endif
}
//static CSndserv sndserv;
static gpointer play_file_mix(gpointer data)
{
g_pReciteWord->sndserv.play((gchar *)data);
g_free(data);
return NULL;
}
/*
static void play_file_after_pre(gpointer data,gpointer user_data)
{
g_pReciteWord->sndserv.play((gchar *)user_data);
return NULL;
}
class CThreadPool
{
GThreadPool *pool;
public:
CThreadPool();
~CThreadPool();
};
CThreadPool::CThreadPool()
{
pool = g_thread_pool_new(play_file_after_pre,NULL,1,FALSE,NULL);
}
CThreadPool::~CThreadPool()
{
g_thread_pool_free(pool,);
}
*/
void play_file(const char *filename,PLAY_METHOD method)
{
if ((!g_pReciteWord->sndserv.canplay)||(g_pReciteWord->sndserv.disable))
return;
//esd_play_file ("reciteword", filename, 1); //it can't return quickly :( when in typing,press wrong key will cause freezing.
// the system() function is inefficiency, should change to use thread to call esd_play_file().
/*gchar command[256];
sprintf(command,"esdplay %s &",filename);
system(command);*/
switch (method)
{
case PM_MIX:
{
#ifdef G_OS_WIN32
PlaySound(filename, 0, SND_ASYNC | SND_FILENAME);
#else
GThread * thread;
gchar *dup_filename = g_strdup(filename); //as in the new thread, filename may have already be freeed.
thread = g_thread_create(play_file_mix,(gpointer)dup_filename,false,NULL); //use GThreadPool may be more efficient.
#endif
break;
}
case PM_STOP_PRE: // it is hard to done,may need write my own esd_play_file.
{
break;
}
case PM_AFTER_PRE: // use GThreadPool can do this,but,it does seems very useful before PM_STOP_PRE is done.
{
//GThreadPool *threadpool;
break;
}
}
}
[/code:1] |
|