在Linux下使用Openal来播放声音类

头文件

/*
 * SoundPlay.h
 *  1:需要下载开发openal开发包(Software implementation of the OpenAL API(devlopment files))和alut开发包
 *  2:添加头文件路径:/usr/include/AL
 *  3:添加库:openal和alut
 */
#ifndef SOUNDPLAY_H_
#define SOUNDPLAY_H_

#include 
#include 
#include 

#include 
#include 
#include 

#define numBuffer 4   //可以同时播放4个声音
#define numSource 4   //可以同时播放4个声音

using namespace std;
class SoundPlay {
public:
	SoundPlay();
	virtual ~SoundPlay();
	void PlaySound(string fileName,int index,float volume);//当index=0:循环播放;index!=0:播放1次
	void SetVolume(int index,float volume);
	void ReleaseSound(int index);
	void MusicPause(int index);
	void MusicContinue(int index);
	bool CheckPlayState(int i);
private:
	void PlayLongSound(const char* fileName,int index);
	void OpenDevice();
	void CloseDevice();
	string GetALCErrorString(ALenum err);
private:
	ALuint buffers[numBuffer];
	ALuint sources[numSource];
	ALCcontext* cc;
	ALCdevice* dev;
	bool checkstate;
	ALenum result;
};
#endif /* SOUNDPLAY_H_ */


cpp文件

/*
 *  SoundPlay.cpp
 */
#include "SoundPlay.h"

SoundPlay::SoundPlay()
{
	alutInit(NULL,0);
	if ((result=alGetError()) != AL_NO_ERROR)
		cout<<"alutInit--"<


你可能感兴趣的:(在Linux下使用Openal来播放声音类)