【问题标题】:Windows IoT Raspberry Pi 3 C# Enumerate USB Audio AdapterWindows IoT Raspberry Pi 3 C# 枚举 USB 音频适配器
【发布时间】:2017-09-29 10:46:31
【问题描述】:

我打算将 2 个或更多 USB 音频适配器(每个都带有麦克风和线路)连接到我的树莓派 3。因此我需要分别枚举音频渲染和音频捕获的音频设备并将它们显示在 listbox 类似到audioinsample。 我不明白这是怎么回事。 我尝试使用下面的代码,发生异常处理程序。 请指教。 谢谢。

        captureDeviceList = new ObservableCollection<DeviceInformation>();
        audioCaptureList.ItemsSource = captureDeviceList;

        renderDeviceList = new ObservableCollection<DeviceInformation>();
        audioRenderList.ItemsSource = renderDeviceList;


  private async void enumerateAudioDevice()
    {
        var renderDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioRender);

        if (renderDevices.Count > 0)
        {
            for (var i = 0; i < renderDevices.Count; i++)
            {
                renderDeviceList.Add(renderDevices[i]);
            }
            audioRenderList.SelectedItem = renderDevices[0];

        }


        var captureDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioCapture);
        if (captureDevices.Count > 0)
        {
            for (var i = 0; i < captureDevices.Count; i++)
            {
                captureDeviceList.Add(captureDevices[i]);
            }
            audioCaptureList.SelectedItem = captureDevices[0];

        }
    }

<PivotItem Header="Info">
            <Grid>
                <ListBox x:Name="audioRenderList" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="288" Margin="0,25,0,0" FontSize="10"/>
                <ListBox x:Name="audioCaptureList" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="288" Margin="318,25,0,0" FontSize="10"/>
                <TextBlock x:Name="renderDeviceCount" HorizontalAlignment="Left" Margin="248,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="40"/>
                <TextBlock x:Name="captureDeviceCount" HorizontalAlignment="Left" Margin="566,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="40" RenderTransformOrigin="0.425,-0.5"/>
                <TextBlock HorizontalAlignment="Left" Margin="318,0,0,0" TextWrapping="Wrap" Text="Capture Devices" VerticalAlignment="Top"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Render Devices" VerticalAlignment="Top"/>
                <ListBox x:Name="usbList" HorizontalAlignment="Left" Height="100" Margin="0,158,0,0" VerticalAlignment="Top" Width="288"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="USB Devices" VerticalAlignment="Top" Margin="0,133,0,0"/>
                <TextBlock x:Name="usbDeviceCount" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="248,133,0,0" Width="40"/>

            </Grid>
        </PivotItem>

更新: 我已经修改了我的 XAML 代码.. 它可以工作.. 但似乎我无法正确安排堆栈面板边距。

下面有我的代码。有什么建议吗? 谢谢。

<PivotItem Header="Info">
            <Grid>
                <ListBox x:Name="audioRenderList" Margin="10,28,358,144" Width="250" Height="90">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="device:DeviceInformation">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{x:Bind Path=Name, Mode=OneWay}" Margin="0,0,0,0" FontSize="18"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox x:Name="audioCaptureList" Margin="344,28,10,144" Width="250" Height="90">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="device:DeviceInformation">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{x:Bind Path=Name, Mode=OneWay}" Margin="0,0,0,0" FontSize="18"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <TextBlock x:Name="renderDeviceCount" HorizontalAlignment="Left" Margin="220,4,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="40"/>
                <TextBlock x:Name="captureDeviceCount" HorizontalAlignment="Left" Margin="560,4,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="40" RenderTransformOrigin="0.425,-0.5"/>
                <TextBlock HorizontalAlignment="Left" Margin="350,4,0,0" TextWrapping="Wrap" Text="Capture Devices" VerticalAlignment="Top"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Render Devices" VerticalAlignment="Top" Margin="10,4,0,0"/>
                <ListBox x:Name="usbList" Margin="10,156,358,16" Width="250" Height="90">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="device:DeviceInformation">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{x:Bind Path=Name, Mode=OneWay}" Margin="0,0,0,0" FontSize="18" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="USB Storage" VerticalAlignment="Top" Margin="10,131,0,0"/>
                <TextBlock x:Name="usbDeviceCount" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="220,131,0,0" Width="40" RenderTransformOrigin="0.575,-0.75"/>

            </Grid>
        </PivotItem>
    </Pivot>

2017 年 9 月 10 日更新

【问题讨论】:

  • 哪里抛出了异常?我用你上面的代码试过了,没有异常。你能显示audioRenderList & audioCaptureList的数据绑定代码吗b> 在你的 xaml 中?
  • 顺便问一下,你确定代码 'captureDeviceList.Add(renderDevices[i]); '是你的期望吗?按照对变量名的理解,应该是'renderDeviceList.Add(renderDevices[i]); '
  • 我重新启动了树莓派 ..n 设法毫无例外地运行代码...&您对 renderdevicelist.add 的看法是正确的。我将编辑该部分。但是,我仍然无法在列表框中显示它们..
  • 请在您的页面(*.xaml 文件)中显示列表框元素的代码。您是否通过在 renderDevices.Count 处设置断点检查了设备的数量>captureDevices.Count?某些设备与 Raspberry PI 不兼容。
  • 我设法在列表框上显示.. 但它们都显示相同的东西“windows.devices.enumaration.devicenformation”.. 我如何识别和显示详细的设备信息?

标签: c# audio raspberry-pi3 windows-iot-core-10 enumerate-devices


【解决方案1】:

由于没有您在 xaml 中的代码,我不确定您的数据绑定方式。导致此问题的原因有多种。请参考下面的源代码,也许您需要进行一些修改以适合您的要求。此外,您应在页面标签中添加 xmlns:device="using:Windows.Devices.Enumeration"

<Page
x:Class="AudioInSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AudioInSample"
xmlns:device="using:Windows.Devices.Enumeration"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="White">
    <StackPanel Margin="10" MinWidth="500">

        <ListBox x:Name="audioCaptureList">
            <ListBox.ItemTemplate>
                <DataTemplate  x:DataType="device:DeviceInformation">
                    <StackPanel>
                        <TextBlock Text="{x:Bind Path=Name, Mode=OneWay}" Margin="12, 15, 12, 0" FontSize="18.667" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <ListBox x:Name="audioRenderList">
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="device:DeviceInformation">
                    <StackPanel>
                        <TextBlock Text="{x:Bind Path=Name, Mode=OneWay}" Margin="12, 15, 12, 0" FontSize="18.667" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


    </StackPanel>
</Grid>

【讨论】:

  • 嗨 Micheal,我在我的问题中添加了我的 xaml 代码更新。我会试试你的建议。
  • 嗨 Micheal,我已经尝试了代码.. 它可以工作.. 但是当我更新我的问题的 jpeg 时似乎存在边距问题。可以建议吗?
  • 在您的页面中,需要修改元素的'Margin'值以适应布局。关于Margin属性的更多信息,请参考:msdn.microsoft.com/en-us/library/…跨度>
  • 列表框边距?或堆栈面板边距?因为我尝试了 stackpanel & textblock margin.. 没有帮助。
  • 请更改列表框的边距。 [Margin="10,28,358,144"] 似乎太大了。
猜你喜欢
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多