I am using VB Express 2012 (VB.net) to make a game for educational use in schools that has .wav sound effects and .mp3 background music. My code is given below, and works fine on any personal computer, but hits a roadblock for computers where permission is not granted to write / delete files to the folder containing the application execute file due to my method for playing mp3's using WMPLib.WindowsMediaPlayer. Since wmp requires a URL to an external file for mp3 files (Why???), I have to write the mp3s from my.resource folder to the client computer to obtain a valid URL, then delete them upon closing the program. This fails if permission is not granted.
Is there another way to play an mp3 file from my.resources from the released application using the wmp.dll that does NOT require writing the file to the client computer? Streaming of some sort perhaps? I have never delved into that area before. By the way, it is unreasonable to convert my mp3s to wav files since the size of the application would be way too large. (I have many mp3s, although my code snippet only shows one as an example. Here's my Code:
For the sound effects I use
My.Computer.Audio.Play(My.Resources.soundeffect, AudioPlayMode.Background)
For the background music, in order for the songs to play uninterrupted by sound effects I added the wmp.dll reference and use the following code:
Dim WithEvents Player As New WMPLib.WindowsMediaPlayer
Player.settings.setMode("Loop", True)
Dim appPath As String
'get the location of application execute file
appPath = Application.StartupPath & "\" & "SongNameHere" & ".mp3"
'write the mp3 file to the folder containing the execute file so wmp accepts the URL
My.Computer.FileSystem.WriteAllBytes(appPath,My.Resources.ResourceManager.GetObject("SongNameHere"), False)
'assign the valid URL to the wmp player, it autostarts.
Player.URL = appPath
And, just in case it is important, on .formclosing I delete the songs with the following code:
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\" & "SongNameHere" & ".mp3")
Thank you to Mr. D. Digaetano for providing the following work around (key solution is bolded):
"...There is no native way to play a resource-packed MP3 file with .NET. It's not even allowed in C#.
The only way to get at the MP3 file is to create a stream that operates on the resource, but the Windows Media Player ActiveX object doesn't support streams as input.
Many people seem to use external DLLs like BASS, and then use the .NET wrapper code provided with those libraries to access their MP3s. However, I've used BASS in the past, and it's not free to use if you intend on selling your software. Also, my code that used it unpacked the MP3s to the current folder and just played them from there, which is what you wanted to avoid due to filesystem restrictions.
I'm not sure why Microsoft would forbid this, but if you read the second paragraph of the 'Remarks' section here, you'll see there's some resistance to bundling media files with your executables.
Perhaps the easiest solution is to get the MP3 files written to a directory that allows writes, such as a temporary folder. Try the following:
Imports System.IO
Dim tempDir As String = Path.GetTempPath() and see if you can
My.Computer.FileSystem.WriteAllBytes(tempDir, My.Resources.ResourceManager.GetObject("SongNameHere"), False)
---"