基于alsa的音量控制代码

发布时间:2016-12-11 16:13:16 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"基于alsa的音量控制代码",主要涉及到基于alsa的音量控制代码方面的内容,对于基于alsa的音量控制代码感兴趣的同学可以参考一下。

//头文件 #include <alsa/asoundlib.h> //类定义 class Sound {   public:     Sound();     ~Sound();     int sound_read();     void sound_write(int);   private:     snd_mixer_t * mixer;     snd_mixer_elem_t *pcm_element; }; //初始化----------------------------------------------------- Sound::Sound() {     snd_mixer_open(&mixer, 0);     snd_mixer_attach(mixer, "default");     snd_mixer_selem_register(mixer, NULL, NULL);     snd_mixer_load(mixer);     //找到Pcm对应的element,方法比较笨拙     pcm_element = snd_mixer_first_elem(mixer);     pcm_element = snd_mixer_elem_next(pcm_element);     pcm_element = snd_mixer_elem_next(pcm_element);     //     long int a, b;     long alsa_min_vol, alsa_max_vol;     ///处理alsa1.0之前的bug,之后的可略去该部分代码     snd_mixer_selem_get_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_LEFT, &a);     snd_mixer_selem_get_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_RIGHT, &b);     snd_mixer_selem_get_playback_volume_range(pcm_element,                                               &alsa_min_vol,                                               &alsa_max_vol);     ///设定音量范围     snd_mixer_selem_set_playback_volume_range(pcm_element, 0, 100);      } //读音量值----------------------------------------------------- int Sound::sound_read() {     long ll, lr;     //处理事件     snd_mixer_handle_events(mixer);     //左声道     snd_mixer_selem_get_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_LEFT, &ll);     //右声道     snd_mixer_selem_get_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_RIGHT, &lr);     return (ll + lr) >> 1; } //写入音量----------------------------------------------------- void Sound::sound_write(int leftright) {     //左音量     snd_mixer_selem_set_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_LEFT,                                         leftright);     //右音量     snd_mixer_selem_set_playback_volume(pcm_element,                                         SND_MIXER_SCHN_FRONT_RIGHT,                                         leftright); } //退出----------------------------------------------------- Sound::~Sound() {     snd_mixer_close(mixer); } 

上一篇:java中"null"与""的区别
下一篇:SQL中的Case When Else End的用法

相关文章

相关评论