Results 1 to 7 of 7

Thread: play different sounds from resources using dim string?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    play different sounds from resources using dim string?

    I have this code:
    Code:
       For i As Integer = 1 To 4
                Dim btn As Control = Me.Controls("button" & i.ToString())
                bttn.Add(btn)
                Dim beepx As String = "My.Resources.beep" & i.ToString()
                beep.Add(beepx)
            Next
            If Form2.sound = True Then
             
                My.Computer.Audio.Play(beep(l), AudioPlayMode.Background)
    
            End If
    the bp is a private array just like the bttn but the audio doesnt work. in the beep(l) the l represents a random number and it should work like this is the random number is 4

    My.Computer.Audio.Play(My.Resources.beep4, AudioPlayMode.Background)

    even though the beep(l) has the value of "My.Resources.beep4" it just gives an exeption on runtime, I figured it would be possible since the my.resources.beepX is the location as string.
    does anyone know whats going on here?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: play different sounds from resources using dim string?

    If you were loading a resource directly you would use ...

    My.Computer.Audio.Play(My.Resources.beep4, AudioPlayMode.Background)

    You've replaced this with ...

    My.Computer.Audio.Play("My.Resources.beep4", AudioPlayMode.Background)

    Fairly obvious why it doesn't work.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: play different sounds from resources using dim string?

    i see, do you know what I have to do to remove the quotes from the string?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: play different sounds from resources using dim string?

    If you were getting a resource name say from a list of resources that were in a ListBox perhaps you could use the following where NameOfResource is a string variable in this case.

    Code:
    Private Sub Button1_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim MyProperties As System.Reflection.PropertyInfo() = GetType(My.Resources.Resources) _
            .GetProperties(System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance Or _
                           System.Reflection.BindingFlags.Static)
    
        Dim Item = (From T In MyProperties Where T.PropertyType Is GetType(UnmanagedMemoryStream) AndAlso _
                    T.Name = NameOfResource).FirstOrDefault
    
        If Item IsNot Nothing Then
            Dim resourceStream As Stream = CType(My.Resources.ResourceManager.GetObject(Item.Name), IO.Stream)
            Dim wavData As Byte()
            ReDim wavData(CInt(resourceStream.Length))
            resourceStream.Read(wavData, 0, CInt(resourceStream.Length))
            My.Computer.Audio.Play(wavData, AudioPlayMode.BackgroundLoop)
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        My.Computer.Audio.Stop()
    End Sub

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: play different sounds from resources using dim string?

    vb.net Code:
    1. Dim Lst As New List(Of Object)
    2.  
    3.    Lst.Add(My.Resources.beep1)
    4.    Lst.Add(My.Resources.beep2)
    5. 'etc
    6.  
    7.         My.Computer.Audio.Play(CType(Lst(i), IO.Stream), AudioPlayMode.Background)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: play different sounds from resources using dim string?

    but i get it from my resources, how can i get that. And I found out its not the "" causing the error. Its the fact its not a string but an unmanagedmemorystream.
    I dont know if its possible to convert a string to unmanagememstream or acces resources differently

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: play different sounds from resources using dim string?

    And the following would apply if you are dynamically loading images with no regard for their name
    Code:
    Dim MyProperties As System.Reflection.PropertyInfo() = _
        GetType(My.Resources.Resources).GetProperties(System.Reflection.BindingFlags.NonPublic Or _
                                                      System.Reflection.BindingFlags.Instance Or _
                                                      System.Reflection.BindingFlags.Static)
    
    ListBox1.DisplayMember = "Name"
    ListBox1.DataSource = (From T In MyProperties Where T.PropertyType Is GetType(UnmanagedMemoryStream)).ToList
    As I have but two audio files on my machine and many images the following is a good example of dynamically working with image resource

    Usage
    Code:
    Public Class Form1
        WithEvents bsData As New BindingSource
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            bsData.DataSource = ResourceImages.GetInstance.DataTable
            cboResourceImages.DisplayMember = "Name"
            cboResourceImages.ValueMember = "Identifier"
            cboResourceImages.DataSource = bsData
            PictureBox1.DataBindings.Add("Image", bsData, "Image")
        End Sub
    End Class
    Implemented using
    Code:
    Public Class ResourceImages
        Private Shared _Instance As ResourceImages
        Private dtImages As DataTable
    
        Public Function DataTable() As DataTable
            If dtImages Is Nothing Then
                GetImages()
            End If
    
            Return dtImages
    
        End Function
        Public Shared Function GetInstance() As ResourceImages
    
            If _Instance Is Nothing Then
                _Instance = New ResourceImages
            End If
    
            Return _Instance
    
        End Function
        ''' <summary>
        ''' Save image by Identifier field value
        ''' </summary>
        ''' <param name="FileName"></param>
        ''' <param name="Identifier"></param>
        ''' <remarks></remarks>
        Public Sub SaveImage(ByVal FileName As String, ByVal Identifier As Int32)
            dtImages.Select("Identifier =" & Identifier)(0).Field(Of Image)("Image").Save(FileName)
        End Sub
        ''' <summary>
        ''' Save image by tweaking the name shown (for this demo) the ComboBox,
        ''' replacing spaces with underscore char, default to bin\debug folder
        ''' and yes we add the image extension.
        ''' </summary>
        ''' <param name="FileName"></param>
        ''' <param name="DisplayName"></param>
        ''' <remarks></remarks>
        Public Sub SaveImage(ByVal FileName As String, ByVal DisplayName As String)
            dtImages.Select("Name ='" & DisplayName & "'")(0).Field(Of Image)("Image").Save(FileName)
        End Sub
        ''' <summary>
        ''' Retrieve images from project resources
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub GetImages()
            dtImages = New DataTable
    
            dtImages.Columns.AddRange(New DataColumn() _
                { _
                    New DataColumn("Identifier", GetType(Int32)),
                    New DataColumn("Name", GetType(System.String)), _
                    New DataColumn("Image", GetType(Image)) _
                } _
            )
    
            dtImages.Columns("Identifier").AutoIncrement = True
            dtImages.Columns("Identifier").AutoIncrementSeed = 1
    
            Dim MyProperties As System.Reflection.PropertyInfo() =
                GetType(My.Resources.Resources).GetProperties(System.Reflection.BindingFlags.NonPublic Or
                                                              System.Reflection.BindingFlags.Instance Or
                                                              System.Reflection.BindingFlags.Static)
    
            Dim BitMaps = (From T In MyProperties Where T.PropertyType Is GetType(Bitmap)).ToList
    
            If BitMaps.Count > 0 Then
    
                For Each pInfo As System.Reflection.PropertyInfo In BitMaps
    
                    dtImages.Rows.Add(New Object() _
                            {
                                Nothing,
                                pInfo.Name.Replace("_", " "),
                                My.Resources.ResourceManager.GetObject(pInfo.Name)
                            }
                        )
    
                Next
    
            End If
        End Sub
        Protected Sub New()
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width