1

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

mylim
  • 313
  • 4
  • 16
  • Where is the exception thrown?I have tried with your above code,there is no exception.Can you show the data binding code of **audioRenderList** & **audioCaptureList** in your xaml? – Michael Xu Oct 02 '17 at 01:56
  • By the way, are you sure that the code 'captureDeviceList.Add(renderDevices[i]); ' is your expectation?According with the understanding of variable name,it should be 'renderDeviceList.Add(renderDevices[i]); ' – Michael Xu Oct 02 '17 at 02:26
  • I restarted the raspberry pi .. n manage to run the code without exception... & You are right about the renderdevicelist.add. I will edit that portion.however, I still couldn't display them on the listbox .. – mylim Oct 02 '17 at 03:04
  • Please show the codes of listbox element in your page(*.xaml file).Have you checked the count of your devices by set breakpoints at **renderDevices.Count** & **captureDevices.Count**?Some device are not compatible with Raspberry PI. – Michael Xu Oct 02 '17 at 03:16
  • I manage to display on the listbox.. but all of them displaying the same thing "windows.devices.enumaration.devicenformation" .. how do i identify and display the detailed device information? – mylim Oct 05 '17 at 04:44
  • I am doing this because I will have at least 2 USB audio device connected at one time.. so I need to identify them.. thanks. – mylim Oct 05 '17 at 05:21

1 Answers1

1

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>

Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Hi Micheal,I have added my xaml code update in my question. I will try out your suggestion. – mylim Oct 06 '17 at 03:33
  • Hi Micheal, I have tried the code.. it works.. but there seem to have margin issue as i updated the jpeg on my question. Can advise? – mylim Oct 09 '17 at 02:07
  • In your page, the **'Margin'** value of elements need to be modified to fit the layout.About more informatoin of Margin property,please refence :https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – Michael Xu Oct 09 '17 at 02:30
  • listbox margin? or the stackpanel margin? because I tried stackpanel & textblock margin.. doesn't help. – mylim Oct 09 '17 at 02:32
  • Please change the margin of listbox. [Margin="10,28,358,144"] seems too big. – Michael Xu Oct 09 '17 at 02:37
  • Hi Micheal, I have tried resizing but it's still the same. I uploaded another picture which my listbox margin are considered reasonable as it is based on the allocation i did. – mylim Oct 09 '17 at 04:13
  • You should redefine the layout of the grid by using Grid.ColumnDefinitions and Grid.RowDefinitions,please reference the sample(http://mikaelkoskinen.net/post/uwp-xaml-responsive-layout-using-grid-and-adaptivetrigger). – Michael Xu Oct 09 '17 at 09:32
  • Hi Micheal, I managed to get it working. I am not sure if it's a bug. I basically created the entire new project copy and paste the exact code over.. & it works.. – mylim Oct 09 '17 at 09:49
  • Hi Micheal, based on my enumerateAudioDevice() routine, can I create an event for DeviceWatcher calling the enumerateAudioDevice() ? – mylim Oct 14 '17 at 08:49