-
Dec 30th, 2012, 02:45 PM
#1
Thread Starter
Addicted Member
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?
-
Dec 30th, 2012, 03:36 PM
#2
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!
-
Dec 30th, 2012, 03:45 PM
#3
Thread Starter
Addicted Member
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?
-
Dec 30th, 2012, 04:07 PM
#4
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
-
Dec 30th, 2012, 04:11 PM
#5
Re: play different sounds from resources using dim string?
vb.net Code:
Dim Lst As New List(Of Object)
Lst.Add(My.Resources.beep1)
Lst.Add(My.Resources.beep2)
'etc
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!
-
Dec 30th, 2012, 04:27 PM
#6
Thread Starter
Addicted Member
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
-
Dec 30th, 2012, 04:38 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|