在Linux下使用Openal来播放声音类
来源:互联网 发布:淘宝货源软件 编辑:程序博客网 时间:2024/06/14 12:21
本文转载博客:http://www.linuxidc.com/Linux/2012-06/62639.htm
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
头文件
- /*
- * 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 <al.h>
- #include <alc.h>
- #include <alut.h>
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #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--"<<result<<endl;
- OpenDevice();
- //PlaySound("Media/Music/BGSound1.wav",0,100);
- }
- SoundPlay::~SoundPlay()
- {
- CloseDevice();
- }
- void SoundPlay::OpenDevice()
- {
- ALCchar DeviceName[] = "ALSA Software";//ALSA Software,DirectSound3D
- dev=alcOpenDevice(DeviceName);
- cc=alcCreateContext(dev,NULL);
- alcMakeContextCurrent(cc);
- }
- void SoundPlay::CloseDevice()
- {
- ALCcontext* context = alcGetCurrentContext();
- ALCdevice* device = alcGetContextsDevice( context );
- alcMakeContextCurrent( NULL );
- alcDestroyContext( context );
- alcCloseDevice( device );
- }
- void SoundPlay::ReleaseSound(int index)
- {
- alDeleteSources(1,&sources[index]);
- alDeleteBuffers(1,&buffers[index]);
- }
- void SoundPlay::PlayLongSound(const char* fileName,int index)
- {
- alGenSources(1,&sources[index]);
- alGenBuffers(1,&buffers[index]);
- ALvoid *data;
- ALsizei size=0,freq=0;
- ALenum format;
- ALboolean loop;
- alutLoadWAVFile((ALbyte *)fileName,&format,&data,&size,&freq,&loop);
- alBufferData(buffers[index],format,data,size,freq);
- if ((result=alGetError()) != AL_NO_ERROR)
- cout<<"PlayLongSound alBufferData errno"<<result<<endl;
- alutUnloadWAV(format,data,size,freq);
- alSourcei(sources[index],AL_BUFFER,buffers[index]);//用音源关联缓冲器
- if(index==0)
- alSourcei(sources[index],AL_LOOPING,AL_TRUE);
- alSourcePlay(sources[index]);
- }
- void SoundPlay::PlaySound(string fileName,int index,float volume)
- {
- alGenSources(1,&sources[index]);
- alGenBuffers(1,&buffers[index]);
- ReleaseSound(index);
- PlayLongSound(fileName.data(),index);
- alSourcef(sources[index],AL_GAIN,volume);
- }
- void SoundPlay::SetVolume(int index,float volume)//volume取值范围(0~1)
- {
- alSourcef(sources[index],AL_GAIN,volume);
- }
- void SoundPlay::MusicPause(int index)
- {
- alSourcePause(sources[index]);
- alSourceStop(sources[index]);
- }
- void SoundPlay::MusicContinue(int index)
- {
- alSourcePlay(sources[index]);
- }
- bool SoundPlay::CheckPlayState(int i)
- {
- ALint state;
- alGetSourcei(sources[i], AL_SOURCE_STATE, &state);
- if(state != AL_PLAYING)
- {
- checkstate=false;
- return true;
- }
- return false;
- }
- int main()
- {
- SoundPlay sp;
- sp.PlaySound("Media/Music/BGSound1.wav",0,100);
- while(1){}
- return 0;
- }
阅读全文
0 0
- 在Linux下使用Openal来播放声音类
- 在Linux下使用Openal来播放声音类
- Linux下使用ALSA来播放声音 - 基本使用方法
- OpenAL播放器使用
- openAL在mac下播放音源结束时判断处理
- 使用openal播放WAV音频
- IOS使用OpenAL播放PCM
- IOS使用OpenAL播放音频文件
- 使用openal播放WAV音频
- openal不能播放声音,自己遇到的问题
- construct2在ios下播放声音
- linux下ALSA播放声音的源程序
- linux平台下读取声音并播放
- linux下ALSA播放声音的源程序
- 解决linux下cocos2dx不能播放声音
- 使用openal与mpg123播放MP3,(转)
- 使用FFMPEG解码和OpenAL播放音乐
- OpenAL声音的衰减
- jQuery.bind用法
- Sublime text c++环境配置
- elasticsearch -2.3.x head插件安装(windows系统)
- 【版本更新】完整UI库Kendo UI发布R3 2017|附下载
- 使用MDL
- 在Linux下使用Openal来播放声音类
- leetcode15 3Sum (set)
- 理解AsyncTask
- ORA-01045: user lacks CREATE SESSION privilege; logon denied
- redis集群删除节点
- Java中CharSequence和String
- php7 特性
- 第4周项目2 建立“单链表”算法库
- App内存泄露分析之绝世高手篇