Windows IoT Raspberry Pi 3 C# Enumerate USB Audio Adapter

280 views Asked by At

I intend to connect 2 or more USB audio adapter (each with mic & line) to my raspberry pi 3. Therefore I need to enumerate the audio devices for audio render and audio capture respectively and display them on a listbox similar to audioinsample . I do not understand how to come about it. I tried playing with the codes below, exception handler occurred. Please advise. Thanks.

        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>

Updated: I have modified my XAML code .. it works.. but seems like I can't get the stack panel margin arranged correctly. Layot from my XAML The alignment on the actual display is not right

I have my code below. Any advise? Thanks.

<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>

Updated 09-10-2017

I don't understand that my margin is too big. It is based on the size which i wanted

1

There are 1 answers

9
Michael Xu On BEST ANSWER

Because without your code in xaml,I am not sure your way of data binding.There are several reasons cause this problem.Please reference below source, maybe you need do some modification so that can fit your reqiurement.In addition, you should add xmlns:device="using:Windows.Devices.Enumeration" in the tag of page.

<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>