How can I extract a .wav to the disk from my resources?
Printable View
How can I extract a .wav to the disk from my resources?
We assume that its saved as bytes.
Create i FileStream object, call the function .Write(). Then you're actually done.
vb.net Code:
Dim fs As System.IO.FileStream = New System.IO.FileStream("Path/<filename>", IO.FileMode.Create) fs.Write(My.Resources.<filename>, 0, My.Resources.<filename>.Length)
Error 1 Value of type 'System.IO.UnmanagedMemoryStream' cannot be converted to '1-dimensional array of Byte'.
what's the alternative to saving a file inside of your application, and possibly compressing those files?
Oh its a unmanagedstream... Hm.. sorry i don't know about that one :(
Well the alternative in saving a wav file is to delete the .wav exstension before you put it into the resources. Then it will be saved in bytes, and when you extract it you just put the .wav on again.
try this:
vb Code:
Dim byteArray() As Byte = My.Resources.resourcename Dim fs As New IO.FileStream(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"), IO.FileMode.Create) fs.Write(byteArray, 0, byteArray.Length) fs.Close() My.Computer.Audio.Play(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"))
Why exactly do you want to save it as a file? You can play it without saving it. The SoundPlayer class can be instantiated using a Stream and My.Computer.Audio.Play will accept a Byte array or Stream as well as a file path.
I don't want to play it, just extract it at the necessary time.
Error 1 Value of type 'System.IO.UnmanagedMemoryStream' cannot be converted to '1-dimensional array of Byte'.Quote:
Originally Posted by .paul.
I was under the impression, as it appears that Link and .paul. were, that WAV files embedded as resources were returned by My.Resources as Byte arrays. Apparently that isn't the case. You now know that they are returned as UnmanagedMemoryStream objects. So, what did you find out when you read the Help documentation for the UnmanagedMemoryStream class?
What's that? You didn't read the Help documentation? You're not alone, more's the pity. It should be the first place people look but it's apparently the last place far too many people think of. The UnmanagedMemoryStream class, like all streams, has a Read method that will read the contents to a Byte array and a Length property to tell you how many Bytes it contains. Once you've got a Byte array you can use a FileStream to write it to disk.
You might choose to use a single big read and a single big write, or you may choose to perform multiple read and write operations until all the data has been transferred.
I tried to read the page on the msdn (http://msdn.microsoft.com/en-us/libr...ream.read.aspx) for it and the only example is in c#, I don't really understand it enough to convert to vb
i tried this and got "Object reference not set to an instance of an object." for test(i) = My.Resources.enraged.ReadByte()
Code:Dim test() As Byte
Dim i As Integer
For i = 0 To My.Resources.test.Length - 1
test(i) = My.Resources.test.ReadByte()
Next
try this:
vb Code:
Dim size As Long = My.Resources.test.Length - 1 Dim buffer(CInt(size)) As Byte Dim offset As Integer = 0 Dim count As Integer = CInt(size) Dim returnValue As Integer returnValue = My.Resources.test.Read(buffer, offset, count) Dim fs As New IO.FileStream(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"), IO.FileMode.Create) fs.Write(buffer, 0, buffer.Length) fs.Close()
thanks, works perfect!