none
Play Music from memory?

    Question

  • Is it possible I can read in a file as such to a byte array or other method & have the file playback from memory & not a file on the filesystem?

    I am trying to do this because the file may be deleted right away if it's not needed & if I can add it to a byte array I can clean up before it starts playing & clear the files but if I have to keep them if the program is ended abruptly or crashes then it never gets a chance to clean up & I don't want to chance files being left behind for a long time till my program is re-opened to clear them...

    So is there a way to do it...?

    NOTE:The files i'm trying to play currently are .mp3 files if that makes a difference for the method(s) I have to use if any exist to allow it to play from memory...

    • Edited by Michael Key Monday, January 23, 2012 4:08 PM
    Monday, January 23, 2012 4:07 PM

Answers

  • Well there is so much there, obviously....it's a big project that is able to play and decode many different types of files & do lots of everything else but I don't want to copy all of NAudio either...

    Someone pointed me towards using Media Foundation which may work so I am going to try using that method & see if it allows me to do anything easier....

    • Marked as answer by Michael Key Wednesday, February 01, 2012 2:34 PM
    Wednesday, February 01, 2012 2:34 PM

All replies

  • Yes easy, you can use the resources to do that and write a WAV file (be aware Microsoft format).

    You do therefore 

    Project > Project Properties> Resources drop your wav file in that.

    then after instancing a soundplayer.

    Dim theMusicStream As System.IO.Stream = My.Resources.MyMusic
    Dim player As SoundPlayer = New SoundPlayer(theMusicStream)
    player.Play
    


    Be aware that on the page for this is showed a converted C# sample which uses reflection, that is not needed with VB as there is in the designer created a special My class for that.

     


    Success
    Cor
    Monday, January 23, 2012 4:31 PM
  • Cor, I said in a note at the bottom of my post the file types are .mp3 & the files are read into the programs memory...not stored directly in the programs resource files...

    As an example...I have a client/server application that people can listen to music together(working) it sends all the information & assembles into a byte array of the file(working) but is there a way without writing the file from the byte array to the file possibly to play the file from the programs memory(in this case the byte array as it is now)

    Monday, January 23, 2012 4:53 PM
    • Proposed as answer by Mike FengModerator Tuesday, January 31, 2012 6:49 AM
    • Unproposed as answer by Michael Key Tuesday, January 31, 2012 1:34 PM
    Monday, January 23, 2012 7:33 PM
  • I can look into that soon but if anyone has any code(working) in vb for a way to play an mp3 file that's loaded into memory, either as a stream or byte array please tell me....because I have it as both a memory stream & a byte array....

    I don't want to need to write the files to play them first because as an example(you have encrypted music files so nobody can open them up in windows media player) & you don't want to expose the files to the person un-encrypted even for playback...

    Monday, January 23, 2012 8:28 PM
  • Michael,

    Why does it has to be MP3 as it is stored in memory.

    Be aware the resources are memory or in other words embeded in the assembly.

    It seems that you and I understood something different about resources.

     


    Success
    Cor
    Monday, January 23, 2012 9:23 PM
  • Cor the file is read in over the internet byte for byte(does not save a file) and the data is stored in a byte array....and a memory structure I have working with wav's as per the code given already but that doesn't help for mp3's....

    The files can technically be written but I don't want the files stored on the hard drive decoded because it could be taken/stolen...I have the file however decoded in the array already & written into a memory stream for accessing but I don't know how to make it play this back...

    Hope you understand I would create the file from the byte array but I don't want the un-encrypted version of it on the file system because I don't want people to copy it, at least so easily....anything can be stolen but i'd rather not make it as easy as copy pasting the file from the hard drive....

    ---Anyways if I had wav versions of the same file or I knew a way to translate them easily in VB for playback I could do that but mp3 is the file I want to end up playing....

    • Edited by Michael Key Monday, January 23, 2012 9:52 PM
    Monday, January 23, 2012 9:49 PM
  • Michael,

    But the resources are not in the file system, they are like I wrote already embedded in the bytes of the assembly.

    I don't understand why you all the time are talking about a file system while I write about the assembly (the exe).

    There are enough freeware converters on Internet to convert MP3 to Microsoft WAV.

    If you had followed what I wrote in my first message to try, you had seen this already much earlier.

     


    Success
    Cor

    Tuesday, January 24, 2012 6:51 AM
  • Cor, I have them as a byte array, not the My.Resources namespace embedded in that method, the contents of files have been sent over the internet to the program & are stored in said byte array....

    I can also convert the byte array to a memory stream, or technically a file...but I don't want a decoded version of the file on the filesystem or else id save it & use that file.....

    Tuesday, January 24, 2012 2:31 PM
  • Michael,

    Then show how you get that byteArray in your program.

    However, I still think that you don't understand that a My.Resource is a byte array included in your program.

    You are all the time for me writing that the My.Resources is a kind of file system or whatever but not included in the program.

     

     


    Success
    Cor
    Tuesday, January 24, 2012 2:52 PM
  • I am working on exactly the same problem right now.  Please post if you solve, thanks!
    Tuesday, January 24, 2012 3:08 PM
  • Cor Lighthert, I only meant that the file is stored in a normal byte array, not a resource I included with the program upon deployment, etc...I use networking to transfer the byte information of a file to my program & that's stored as

    IO.MemoryStream

    &

    Private ByteArray As Byte()

    I have converted the ByteArray of .wav files to the MemoryStream to play it with the method above but I need to play MP3's too...

    I could store the file using the ByteArray there but I don't want an uncompressed version on the filesystem which is easily copyable...and only uncompressed versions would even play using any methods I know of....such as WMP control embedded into my program for example...

    Tuesday, January 24, 2012 3:10 PM
  • Michael,

    You don't include the resource with deployment.

    It is an integral part of the program.

    Why don't you try my sample, it takes 10 minutes to do so.

    If you do what I wrote you are playing music from memory, not from a file not even a resource.resx file.

    If you sent an MP3 file over the line it will always be copy-able

     


    Success
    Cor
    Tuesday, January 24, 2012 3:27 PM
  • I sent an encrypted version over the network using a custom encryption method....anything can be copied but they first have to find out the encryption method & decrypt it & get a hold of the data...with it un-encrypted on the filesystem(to allow playback) it's pointless because they can just copy paste it & have to do no more work than that, no packet sniffers, no reading the memory of the program to find the data...nothing....just copy & paste....

    I meant I didn't go to the "Resources" part of the program & drag the file into there....so the file is not in the My.Resources namespace since it wasn't a file included with my program, the files data is instead sent over the internet & stored as in the previous post....

    ---Your sample WORKS FINE for .wav I want to play .MP3

    Now your the one not paying attention....your code only plays .wav

    Unless you gave code how to convert EXISTING MP3 files to WAV this is useless because I don't create the files I want to play...I can't just tell it to create the music in WAV file since im not the one who created it...

    Tuesday, January 24, 2012 3:34 PM
  • I have converted the ByteArray of .wav files to the MemoryStream to play it with the method above but I need to play MP3's too...

    So, I guess the code relating to playing MP3s using NAudio from a memorystream in the link I posted is still of no use to you then?

    --
    Andrew

    Tuesday, January 24, 2012 3:38 PM
  •  

    ---Your sample WORKS FINE for .wav I want to play .MP3

     

    That I've done probably already 10 messages ago to you.

    Be aware it is nowhere in your original question.


    Success
    Cor
    Tuesday, January 24, 2012 3:39 PM
  • The NOTE part at the bottom, not in the topic but the message clearly states...The files im trying to play are .mp3 if that makes a difference...

    Cor, you didn't provide a solution working for .mp3's your code for the soundplayer when I try to play MP3's says theres not a valid .wav header when attempting to play mp3's & clearly states in intellisense that it only plays .wav files....

    Apparentally you missed reading all of my post when you read it but the NOTE I included clearly did say what type of file even though I had left it out of the topic(on the question) & in the rest of the text above the note....

    I also obviously didn't say .wav anywhere in the topic or anything either so an assumption of file type or ANY method would be useless speculation at that point & should've instead of lead to a question of what type of files or if I had any control over the creation over the files(like what file types I could possibly use on creating them)

    ---Hopefully you either explain to me how yours is the solution or try & be helpful and give me a solution that might actually work....I am sorry the topic didn't say .mp3 but we need to get passed that & think of solutions that will work....I have been searching for hours(minus sleep) for the solution since I posted this(and a few hours before that) either converting the .mp3 over to the file-type of .wav or playing .mp3 without writing the file to the filesystem(hard drive) but have come up with nothing....that's why i'm asking the question here because I don't know how I would do it.

    Tuesday, January 24, 2012 3:52 PM
  • The NOTE part at the bottom, not in the topic but the message clearly states...The files im trying to play are .mp3 if that makes a difference...

     

    You mean this one.

    NOTE:The files i'm trying to play currently are .mp3 files if that makes a difference for the method(s) I have to use if any exist to allow it to play from memory...

    I've read that in another way then you write now. No you cannot use Mp3 files for playing from memory. So you have to use another format to play from memory or you have to convert it, but you did not even read one message from me in this thread. You are only telling that resources don't do it for you. 



    Success
    Cor


    Tuesday, January 24, 2012 4:33 PM
  • I read your messages completely(every word) but your understanding of what i'm saying is different than the meaning I am trying to make with what i'm saying...

    Maybe I was a little wordy there & made it seem like I could use a different filetype if there was another method for another type of file, but it's not the case....I only meant that I wanted to play mp3's from memory & if that is helpful in determining what method/code I had to use (such as many different methods existed but didn't support mp3) then you would know which method to give me that would be helpful to play mp3's & as per this, it still didn't help because the methods given were still not for mp3's & instead were for .wav which was not my intention in the wording of that note....

    Anyways OK so as to your knowledge there is no way to play MP3's without writing the un-encrypted file to disk, I got that now...I will make a new question about the possibility of converting the files to a format that will play from memory (.wav)

    Tuesday, January 24, 2012 4:49 PM
  • I am working on exactly the same problem right now.  Please post if you solve, thanks!

    Using NAudio (naudio.codeplex.com - you have to compile it yourself from the supplied C# - you only need to build the NAudio project in the solution and add a reference to the compiled dll in your project) and converting the code from how-to-play-a-mp3-file-using-naudio into VB:

    Imports NAudio.Wave
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim songFile = "C:\temp\Kalimba.mp3"
            Dim songBytes() As Byte
            songBytes = File.ReadAllBytes(songFile)
            Dim ms = New MemoryStream(songBytes)
    
            Dim waveOutDevice As IWavePlayer
            waveOutDevice = New WaveOut
    
            Using rdr = New Mp3FileReader(ms)
                Using wavStream = WaveFormatConversionStream.CreatePcmStream(rdr)
                    Using baStream = New BlockAlignReductionStream(wavStream)
                        Using waveOut = New WaveOut(WaveCallbackInfo.FunctionCallback())
    
                            waveOut.Init(baStream)
                            waveOut.Play()
                            While waveOut.PlaybackState = PlaybackState.Playing
                                Threading.Thread.Sleep(100)
                            End While
    
                        End Using
                    End Using
                End Using
            End Using
    
        End Sub
    
    End Class
    
    


    It'll need some "adjustment" to make the UI responsive while it's playing. Kalimba.mp3 is one of the demo tracks that comes with Windows.

    HTH,

    Andrew

     

    Tuesday, January 24, 2012 8:27 PM
  • Using NAudio (naudio.codeplex.com - you have to compile it yourself from the supplied C# - you only need to build the NAudio project in the solution and add a reference to the compiled dll in your project)

    If you have a version of VS which works with NuGet, I think you can skip the compilation, see the author's blog at http://mark-dot-net.blogspot.com/search/label/NAudio.

    --
    Andrew

    Tuesday, January 24, 2012 8:47 PM
  • OK well I can't do a compilation in C#, don't have VS even just the express edition VB...

    I would need code that works directly without C# for dll compiling.

    I also don't want to use the .dll anyways...

    I want to be able to code this myself, if its too hard ill just skip to keep trying to figure out how to make it play an mp3 from memory instead...even if I have to do stuff like maybe DirectSound, or something like that...if that exists still or something like DirectAudio, I don't know what all there is or what's supported but methods like these have been used for mp3 from a MemoryStream before according to searches throughout the internet...

    Does anyone know if they indeed do work, any of the two & how to use them?

    Tuesday, January 24, 2012 10:01 PM
  • The DirectX SDK is supposed to support this too but I can't make it work, if I run what I have in the program I get either LoaderLock or it says the assembly im compiling with doesn't support it too....LoaderLock problem is usual in debug but when I run it normally as release version or w/e it just crashes....no explanation of the problem....
    Tuesday, January 24, 2012 11:41 PM
  • That would be fine if it didn't contain NAudio, I can't compile NAudio, if someone can give the code of NAudio to translate into VB I could do that...but I can't compile NAudio myself to a .dll or anything.
    Wednesday, January 25, 2012 1:30 PM
  • That would be fine if it didn't contain NAudio, I can't compile NAudio, if someone can give the code of NAudio to translate into VB I could do that...but I can't compile NAudio myself to a .dll or anything.

    I'm not sure if you're saying you wouldn't use it even if you could find NAudio.dll to download.

    I strongly advise you to at least try looking through the documentation if you're going to use it.

    --
    Andrew

    Wednesday, January 25, 2012 2:08 PM
  • Andrew, I don't want to use the .dll directly, rather the code behind it converted to VB, is there a way to view the code without downloading it so I can convert & use the code alone...

    Basically it's a wrapper & i'd rather use the code directly, not the wrapper....the wrapper can make it easier but I usually just create my own wrappers for things like this...thanks...

    Wednesday, January 25, 2012 2:11 PM
  • Hi Michael,

    If you really want to learn something rather than show off, look at Andrew's suggestion, yes, the sample is written by C#, but you totally have no idea with C#? I don't think so, so many online tools can translate the C# code to VB, you can help you by yourself.

    Have a nice day. 


    Call me ghost for short, Thanks
    To get the better anwser, it should be a better question.
    Wednesday, January 25, 2012 2:33 PM
  • Don't know what you mean show off, I however don't have C# and was wondering the availibility of the code to convert it(i convert it by hand because usually the translators only get close & not copy+paste capable code....) so its just easier to translate it myself...

    As for the code, I found it but i'm not even sure which bits do what I want, because the wrapper & everything is built to do everything...mp3's midi's and lots of other types of files....

    I don't want the bloat of having everything there when I want only want the one type of file....

    Wednesday, January 25, 2012 2:37 PM
  • You write that you've found the source code, are happy to translate it from C# by hand, and write wrappers yourself, so I don't think I'm going to be much help to you in reducing the dll down from the massive 382KB it takes.

    --
    Andrew

    Wednesday, January 25, 2012 2:55 PM
  • The size isn't the problem with it but rather that I already have so many different codes already that I have created wrappers for(often large) because something in VB wasn't available to you natively...not a real shocker there because lots of stuff i've been doing happens to be stuff just like that but i'm tired of having to download all this stuff when it should be a short & easy code to get just the portion I want working & not have to download all the bloat(the many many parts I don't want to use) off every single one of these things i'm trying to make...

    It adds up quickly....I just can't find out which parts I need because in the code I see a demo portion(calling up the .dll) itself & not really the code it's calling up, the project im seeing however maybe only the part at which he explains how to use it, not the code of the actual .dll(wrapper)

    If you know a different code which I can view online & hopefully has a lot less(plays mp3 only for example, not with all the rest of the stuff that I don't care to ever use...) that would be helpful too, because I don't want to download yet another thing like this to use only a very small portion of what it does...

    Wednesday, January 25, 2012 3:03 PM
  • Annoying that this seems so tricky, because you can just drag a WindowsMedia component onto a form and say:

    AxWindowsMediaPlayer1.URL = "http://me.com/sample2.mp3"

    My problem is that the audio files I want to play are accessible through an url like the one above, but they necessitate an authorization code in the header of the web request to access them.  I can get the file using an httpWebRequest with the proper authorization headers, but I am not sure how I redirect that stream into the URL parameter above.

     


    • Edited by Screener Wednesday, January 25, 2012 7:51 PM
    Wednesday, January 25, 2012 7:50 PM
  • For my use the files are sent from client/server type programming model & not from a web server so the URL part probably wouldn't be of help in that situation as far as I am aware....
    Wednesday, January 25, 2012 9:55 PM
  • You can also refer to it as a file on the local hard drive, something like:

    AxWindowsMediaPlayer1.URL = "c:\sample2.mp3"

    Thursday, January 26, 2012 9:13 PM
  • Tried getting the file with an httpWebRequest, but no response:

        Private Sub AxWindowsMediaPlayer2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxWindowsMediaPlayer2.Enter
    
            AxWindowsMediaPlayer1.URL = getRecording("http://tedmurph.com/recording.mp3")
    
        End Sub
    
        Private Function getRecording(ByVal url As String) As String
    
            Dim webRequest As HttpWebRequest
            webRequest = Net.WebRequest.Create(url)
    
            webRequest.Method = "GET"
            webRequest.Accept = "audio/*"
    
            Dim webResponse As HttpWebResponse = Nothing
            webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
    
            Dim response As StreamReader
            response = New StreamReader(webResponse.GetResponseStream())
    
            Dim audiofile As String
            audiofile = response.ReadToEnd
    
            Return audiofile
    
        End Function
    


    That's the basic problem, though -- how do I feed an audio file retrieved with an httpWebRequest into the AxWindowsMediaPlayer control?

     


    • Edited by Screener Sunday, January 29, 2012 10:19 PM
    Sunday, January 29, 2012 10:18 PM
  • So, NAudio does do the job here (thank you Andrew Morton).  If you are working in VB, you can download the most recently compiled dll from naudio.codeplex.com and add it as a reference in your project.  All of the Naudio methods are then available within your VB project.

    In my case, I pulled the recording using an httpWebRequest, then moved the responseStream into a MemoryStream for use in Andrew's NAudio code snippet above: 

        Private Function getRecording(ByVal url As String) As MemoryStream
    
            Dim webRequest As HttpWebRequest
            webRequest = Net.WebRequest.Create(url)
    
            webRequest.Method = "GET"
            webRequest.Accept = "audio/*"
    
            Dim webResponse As HttpWebResponse = Nothing
            webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            Dim responseStream As Stream = webResponse.GetResponseStream()
    
            Dim audioStream = New MemoryStream
            Dim buffer(10000) As Byte
            Dim bytesRead As Integer = 0
            Do
                bytesRead = responseStream.Read(buffer, 0, buffer.Length)
                audioStream.Write(buffer, 0, bytesRead)
            Loop While bytesRead > 0
            audioStream.Position = 0
    
            Return audioStream
    
        End Function

     

    • Edited by Screener Tuesday, January 31, 2012 1:27 PM
    • Proposed as answer by Screener Tuesday, January 31, 2012 1:27 PM
    • Unproposed as answer by Michael Key Tuesday, January 31, 2012 1:33 PM
    • Proposed as answer by Mike FengModerator Wednesday, February 01, 2012 10:12 AM
    • Unproposed as answer by Michael Key Wednesday, February 01, 2012 2:32 PM
    Tuesday, January 31, 2012 1:24 PM
  • Need to use something other than NAudio or need code that NAudio uses for it to work so I can use it in my own project, I need to play mp3 only...from memorystream...
    Tuesday, January 31, 2012 1:35 PM
  • Hi Michael,

    Did you take a look at the project NAudio in the http://naudio.codeplex.com/ , If you download the the source file NAudio.1.5.Source.zip here: http://naudio.codeplex.com/releases/view/79035, and then open the solution, and you can find the NAudio project is the code you wanted, if you don't want to reference the DLL which the other community member suggested, you can add this project to your solution. And then add the project dependencies. If you don't familiar with the C# code, the online tools is helpful to help you to convert the C# code to VB.net code: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    If you got some problem during you convert the C# code to VB.net code, please feel free to post the un-convert code here and I will try to resolve it.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, February 01, 2012 10:11 AM
  • Well there is so much there, obviously....it's a big project that is able to play and decode many different types of files & do lots of everything else but I don't want to copy all of NAudio either...

    Someone pointed me towards using Media Foundation which may work so I am going to try using that method & see if it allows me to do anything easier....

    • Marked as answer by Michael Key Wednesday, February 01, 2012 2:34 PM
    Wednesday, February 01, 2012 2:34 PM