can you access audio resources by using a variable name instead of the resource name?

Mike Benton 96 Reputation points
2021-02-11T18:57:06.27+00:00

I am just starting to code a program that will read a text file one character at a time and play a wav
file from My.Resources. The resources wav files are the morse code dots and dashes for each letter.
The resource items are named a,b,c etc. Is there a way to access the resource items using a variable
name instead of the name given to each resource? I hope this makes sense.'

Here is my code so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text As String = "abcdefgh"
Dim car As String
Dim i As Integer
Dim j As Integer
Dim acode As String
Dim ltr As String
For i = 1 To Len(text)
car = Mid$(text, i, 1)
For j = 97 To 122
acode = Asc(car)
If j = Val(acode) Then
ltr = Chr(acode) 'I want to use a variable like car or ltr in place of the resource name a

                My.Computer.Audio.Play(My.Resources.a, AudioPlayMode.WaitToComplete)  
            End If  

        Next  
    Next  

here is the solution explorer:
67181-image.png

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,756 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mike Benton 96 Reputation points
    2021-02-16T17:58:34.713+00:00

    I did make two wave files, one for the space between dot and dashes and one for the space between words. That is working just fine. I just need to work out some logic
    for the numbers and punctuation and then all characters will be working. Thanks so much for you input. It is exactly what I needed.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Adrian Bowles 86 Reputation points
    2021-02-15T20:44:16.737+00:00

    You are doing lots of conversions that I don't think are needed and can be much simpler. I've split it down into two methods one which is what you have tried to do - which I assume is play something like morse code wav's for each letter in the textbox and a 2nd function to simply verify the character is a alphabet character.

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Clickr
            For Each c In TextBox1.Text.ToUpper.Trim
                If IsAlpha(c) Then
                    Try
                        Dim resource = My.Resources.ResourceManager.GetObject(c)
                        My.Computer.Audio.Play(resource, AudioPlayMode.WaitToComplete)
                    Catch ex As Exception
                        'In case theres a problem playing resources
                    End Try
                End If
            Next
        End Sub
    
        Public Function IsAlpha(strValue As Char) As Boolean
            'Verifys the character is an alphabet character only.   All characters are converted to uppercase
            Select Case Asc(strValue.ToString.ToUpper)
                Case 65 To 90
                    IsAlpha = True
                Case Else
                    IsAlpha = False
    
            End Select
            Return IsAlpha
        End Function
    
    1 person found this answer helpful.

  2. Adrian Bowles 86 Reputation points
    2021-02-14T21:19:43.927+00:00

    So the following code has two wav files loaded as resource called 1 and 2 . When this code runs it will play each in the sequence. I think this should demonstrate dynamically getting the wave resoruces.

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            '//Just Get Each letter from the string
    
            For Each i In "1211"
                Dim resource = My.Resources.ResourceManager.GetObject(i)
                My.Computer.Audio.Play(resource, AudioPlayMode.WaitToComplete)
            Next
    
        End Sub
    

  3. Adrian Bowles 86 Reputation points
    2021-02-15T19:29:13.82+00:00

    So what is happening here - there are 4 versions of the play function. It doesn't know which to use (Overload resolution)

    In my original example - I simply added the 1.wav and 2.wav resources. So the names were 1 and 2 in the resources. If I added two files A.wav and B.wav the names would be A and B.

    Stick a breakpoint in your code and see what the runtime type of resource is. It should probably be System.IO.UnManagedStream or something like that if you hover over it at runtime. If it's not, then you are not setting resource correctly

    Examples To show using letters

    For Each i In "ABAA"  
        Dim resource = My.Resources.ResourceManager.GetObject(i)  
        My.Computer.Audio.Play(resource, AudioPlayMode.WaitToComplete)  
    Next  
    

    Examples showing exception similar to yours

    For Each i In "ABAA"  
        Dim resource = nothing   'Not set to a value  
        My.Computer.Audio.Play(resource, AudioPlayMode.WaitToComplete)  
    Next  
    

  4. Adrian Bowles 86 Reputation points
    2021-02-16T21:14:02.71+00:00

    Well thats pretty straightforward - adding more wav file resources for numbers and whitespace and then have methods to detect and play the appropriate sources. These will have different names than the character itself because it has to be a valid identifier. But the code for numbers and pauses between words should be something similar to

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For Each c In TextBox1.Text.ToUpper.Trim
                If IsAlpha(c) Then
                    Try
                        Dim resource = My.Resources.ResourceManager.GetObject(c)
                        My.Computer.Audio.Play(resource, AudioPlayMode.WaitToComplete)
                    Catch ex As Exception
                        '//In case theres a problem playing resources
                    End Try
                ElseIf IsWhitespace(c) Then
                    Try
                        My.Computer.Audio.Play(My.Resources.ResourceManager.GetObject("BetweenWords"), AudioPlayMode.WaitToComplete)
                    Catch ex As Exception
                        'In case theres a problem playing resources
                    End Try
                ElseIf IsNumber(c) Then
                    Try
                        My.Computer.Audio.Play(My.Resources.ResourceManager.GetObject(GetNumberResourceString(c)), AudioPlayMode.WaitToComplete)
                    Catch ex As Exception
                        'In case theres a problem playing resources
                    End Try
                Else
                    ' Punctuation Character
                End If
    
                My.Computer.Audio.Play(My.Resources.ResourceManager.GetObject("Pause"), AudioPlayMode.WaitToComplete)
    
            Next
        End Sub
    
        Public Function IsAlpha(strValue As Char) As Boolean
            Dim bResult As Boolean = False
            '//Verifys the character is an alphabet character only.   All characters are converted to uppercase
            Select Case Asc(strValue.ToString.ToUpper)
                Case 65 To 90
                    IsAlpha = True
                Case Else
                    IsAlpha = False
    
            End Select
            Return bResult
        End Function
    
        Public Function IsNumber(strValue As Char) As Boolean
            Dim bResult As Boolean = False
    
            'Verifys the character is an alphabet character only.   All characters are converted to uppercase
            Select Case Asc(strValue.ToString.ToUpper)
                Case 48 To 57
                    bResult = True
                Case Else
                    bResult = False
            End Select
            Return bResult
        End Function
    
        Public Function IsWhiteSpace(strValue As Char) As Boolean
            Dim bResult As Boolean = False
    
            'Verifys the character is an alphabet character only.   All characters are converted to uppercase
            Select Case Asc(strValue.ToString.ToUpper)
                Case 32
                    bResult = True
                Case Else
                    bResult = False
            End Select
            Return bResult
        End Function
        Public Function GetNumberResourceString(strValue As Char) As Boolean
            Dim ResourceString = "Number_" & strValue
            Return ResourceString
        End Function
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.