[英]How to get and set volume in Linux using ALSA using C#?
I want to get the system volume of Linux using C# code.我想使用 C# 代码获取 Linux 的系统卷。 How to do this?
这该怎么做?
Extra Info:额外信息:
-->Is there any library/dll that I can use. --> 有没有我可以使用的库/dll。
-->I have come across alsa-sharp but I did not find any function regarding volume. -->我遇到过 alsa-sharp,但我没有找到任何有关音量的功能。
--> I have come across C code which but I am not familiar on how I can use that in C#. --> 我遇到过 C 代码,但我不熟悉如何在 C# 中使用它。
Better late than never, but I came across this while setting up an RPi-based internet radio and the following code, which you can also find at this git , works for me.迟到总比不到好,但我在设置基于 RPi 的网络广播时遇到了这个问题,以下代码(您也可以在这个 git 上找到)对我有用。
It's based on this answer and the native interop code from alsa-sharp by https://github.com/atsushieno.它基于此答案以及 https://github.com/atsushieno来自 alsa-sharp的本机互操作代码。
Note: You might need to experiment with the "card" and "selemName" parameters.注意:您可能需要试验“card”和“selemName”参数。
using System;
using System.Runtime.InteropServices;
namespace Alsa
{
public sealed class VolumeControl : IDisposable
{
private readonly long _min;
private readonly long _max;
private readonly IntPtr _sid;
private readonly IntPtr _selem;
private readonly IntPtr _handle;
private const string LibraryName = "libasound";
public VolumeControl(string card = "default", string selemName = "PCM")
{
snd_mixer_open(ref _handle, 0);
snd_mixer_attach(_handle, card);
snd_mixer_selem_register(_handle, default, default);
snd_mixer_load(_handle);
snd_mixer_selem_id_malloc(ref _sid);
snd_mixer_selem_id_set_index(_sid, 0);
snd_mixer_selem_id_set_name(_sid, selemName);
_selem = snd_mixer_find_selem(_handle, _sid);
snd_mixer_selem_get_playback_volume_range(_selem, ref _min, ref _max);
}
public void SetVolumePercent(int volume)
{
snd_mixer_selem_set_playback_volume_all(_selem, (int)(volume * _max / 100));
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
private void ReleaseUnmanagedResources()
{
snd_mixer_selem_id_free(_sid);
snd_mixer_close(_handle);
}
~AlsaSoundOutput()
{
ReleaseUnmanagedResources();
}
[DllImport(LibraryName)]
internal static extern int snd_mixer_open(ref IntPtr mixer, int mode);
[DllImport(LibraryName, CharSet = CharSet.Ansi)]
internal static extern int snd_mixer_attach(IntPtr mixer, [MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport(LibraryName)]
internal static extern int snd_mixer_selem_register(IntPtr mixer, IntPtr options, IntPtr classp);
[DllImport(LibraryName)]
internal static extern int snd_mixer_load(IntPtr mixer);
[DllImport(LibraryName)]
internal static extern int snd_mixer_selem_id_malloc(ref IntPtr selem);
[DllImport(LibraryName)]
internal static extern void snd_mixer_selem_id_set_index(IntPtr selem, uint val);
[DllImport(LibraryName, CharSet = CharSet.Ansi)]
internal static extern void snd_mixer_selem_id_set_name(IntPtr selem, [MarshalAs(UnmanagedType.LPStr)] string value);
[DllImport(LibraryName)]
internal static extern IntPtr snd_mixer_find_selem(IntPtr mixer, IntPtr selem);
[DllImport(LibraryName)]
internal static extern int snd_mixer_selem_get_playback_volume_range(IntPtr selem, ref long min, ref long max);
[DllImport(LibraryName)]
internal static extern int snd_mixer_selem_set_playback_volume_all(IntPtr selem, int value);
[DllImport(LibraryName)]
internal static extern void snd_mixer_selem_id_free(IntPtr selem);
[DllImport(LibraryName)]
internal static extern int snd_mixer_close(IntPtr mixer);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.