Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Extract embedded .wav's?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Resolved [RESOLVED] [2005] Extract embedded .wav's?

    How can I extract a .wav to the disk from my resources?

  2. #2
    Addicted Member
    Join Date
    May 2008
    Location
    Denmark
    Posts
    178

    Re: [2005] Extract embedded .wav's?

    We assume that its saved as bytes.
    Create i FileStream object, call the function .Write(). Then you're actually done.
    vb.net Code:
    1. Dim fs As System.IO.FileStream = New System.IO.FileStream("Path/<filename>", IO.FileMode.Create)
    2. fs.Write(My.Resources.<filename>, 0, My.Resources.<filename>.Length)

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    Error 1 Value of type 'System.IO.UnmanagedMemoryStream' cannot be converted to '1-dimensional array of Byte'.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    what's the alternative to saving a file inside of your application, and possibly compressing those files?

  5. #5
    Addicted Member
    Join Date
    May 2008
    Location
    Denmark
    Posts
    178

    Re: [2005] Extract embedded .wav's?

    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] Extract embedded .wav's?

    try this:

    vb Code:
    1. Dim byteArray() As Byte = My.Resources.resourcename
    2. Dim fs As New IO.FileStream(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"), IO.FileMode.Create)
    3. fs.Write(byteArray, 0, byteArray.Length)
    4. fs.Close()
    5. My.Computer.Audio.Play(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"))

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Extract embedded .wav's?

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    I don't want to play it, just extract it at the necessary time.

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    Quote Originally Posted by .paul.
    try this:

    vb Code:
    1. Dim byteArray() As Byte = My.Resources.resourcename
    2. Dim fs As New IO.FileStream(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"), IO.FileMode.Create)
    3. fs.Write(byteArray, 0, byteArray.Length)
    4. fs.Close()
    5. My.Computer.Audio.Play(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"))
    Error 1 Value of type 'System.IO.UnmanagedMemoryStream' cannot be converted to '1-dimensional array of Byte'.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Extract embedded .wav's?

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    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

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] Extract embedded .wav's?

    try this:

    vb Code:
    1. Dim size As Long = My.Resources.test.Length - 1
    2. Dim buffer(CInt(size)) As Byte
    3. Dim offset As Integer = 0
    4. Dim count As Integer = CInt(size)
    5. Dim returnValue As Integer
    6.  
    7. returnValue = My.Resources.test.Read(buffer, offset, count)
    8. Dim fs As New IO.FileStream(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "sound.wav"), IO.FileMode.Create)
    9. fs.Write(buffer, 0, buffer.Length)
    10. fs.Close()

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: [2005] Extract embedded .wav's?

    thanks, works perfect!

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