从 C 代码设置 ALSA 主音量 [英] Set ALSA master volume from C code

查看:30
本文介绍了从 C 代码设置 ALSA 主音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个简单的 C 代码示例来设置 ALSA 混音器的主音量,但找不到任何简单的东西来处理这个所谓的常见操作.

I've been looking for a simple C code example to set the master volume of the ALSA mixer but could not find anything simple for this supposedly common operation.

我完全不熟悉 ALSA,因此制作我自己的最小示例需要时间.如果有人可以提供,我会很高兴.

I'm totally unfamiliar with ALSA, so making my own minimal example will take time. I would be happy if anyone could provide one.

推荐答案

以下对我有用.参数 volume 应在 [0, 100] 范围内给出.当心,没有错误处理!

The following works for me. The parameter volume is to be given in the range [0, 100]. Beware, there is no error handling!

void SetAlsaMasterVolume(long volume)
{
    long min, max;
    snd_mixer_t *handle;
    snd_mixer_selem_id_t *sid;
    const char *card = "default";
    const char *selem_name = "Master";

    snd_mixer_open(&handle, 0);
    snd_mixer_attach(handle, card);
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);

    snd_mixer_selem_id_alloca(&sid);
    snd_mixer_selem_id_set_index(sid, 0);
    snd_mixer_selem_id_set_name(sid, selem_name);
    snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);

    snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
    snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);

    snd_mixer_close(handle);
}

这篇关于从 C 代码设置 ALSA 主音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆