locked
How to make mp3 sound files in resources available? RRS feed

  • Question

  • I made a program that has mp3 files in resources when I published it I found that it doesn't work in other pcs only mine now what should I do?
    Friday, August 5, 2011 1:11 AM

Answers

  • Well of course I had to try my theory. I have good news and bad news ;)

    The good news is that it DOES work - but the bad news is that you're limited on the size of the zip file. In my tests, the smallest I tried was around 50 megs - but it choked on it. Given these are .mp3 files, any decent collection will get to be pretty big, so I've given thought to another way we could do this, and it does work.

    It gets a little involved (not that much really) and there's a major caveat that I'll explain at the end of this. In this test, I have placed a password-protected .zip file into the application folder. It's not small!

     

     

    The code that I used follows. In this one, I'm using a component by ChilKat to do the unzipping part. It's not free, so you may want to have a look around for a free one. I know I've seen others here discuss at least one that's open source, but I can't now recall the name of it. I make this point because the methods that I show below won't work for yours - you'll need to find the documentation of the one you choose to use and follow their instruction on making it work properly.


    CODE

      Private tempFolder As String = Application.StartupPath & "\Temp"
      Private zipFile As String = Application.StartupPath & "\mp3.zip"
      Private zip As New Chilkat.Zip()
      Private bgwError As String = ""
    
    
    
      Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    
        If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
          My.Computer.FileSystem.DeleteDirectory(tempFolder, _
              FileIO.DeleteDirectoryOption.DeleteAllContents)
        End If
    
      End Sub
    
    
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim success As Boolean = False
    
        success = zip.UnlockComponent("YourRegistrationNumberHere")
        If (success <> True) Then
          MessageBox.Show("Internal Error", "Error", _
              MessageBoxButtons.OK, MessageBoxIcon.Error)
          Close()
        End If
    
        lbl_AudioFileCount.Text = "Preparing Files..."
        ProgressBar1.Style = ProgressBarStyle.Marquee
    
        Try
          If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
            My.Computer.FileSystem.DeleteDirectory(tempFolder, _
                FileIO.DeleteDirectoryOption.DeleteAllContents)
          End If
    
          My.Computer.FileSystem.CreateDirectory(tempFolder)
        Catch ex As Exception
          MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _
                  "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Close()
        End Try
    
        ListBox1.Enabled = False
        Me.Cursor = Cursors.WaitCursor
        Me.Show()
        Me.Refresh()
    
        bgw_Unzip.RunWorkerAsync()
    
      End Sub
    
    
    
      Private Sub bgw_Unzip_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_Unzip.DoWork
    
        ' Background worker to unzip the audio files
    
        Try
          zip.DecryptPassword = "mypassword"
          zip.OpenZip(zipFile)
          zip.Extract(tempFolder)
    
        Catch ex As Exception
          bgwError = ex.Message
          bgw_Unzip.CancelAsync()
        End Try
    
      End Sub
    
    
    
      Private Sub bgw_Unzip_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw_Unzip.RunWorkerCompleted
    
        If bgwError <> String.Empty Then
          MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & bgwError, _
                  "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
          Try
            If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
              Dim songCnt As Integer = 0
    
              For Each songFile As String In My.Computer.FileSystem.GetFiles(tempFolder, _
                          FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
    
                Dim infoToShow As String = Replace(songFile, tempFolder, "")
                ListBox1.Items.Add(infoToShow)
                songCnt += 1
              Next
    
              lbl_AudioFileCount.Text = "Total Song Count: " & songCnt.ToString("n0")
            Else
              Throw New Exception("Error Enumerating Music Files")
            End If
    
          Catch ex As Exception
            MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _
                    "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Close()
          End Try
        End If
    
        ProgressBar1.Visible = False
        ListBox1.Enabled = True
        Me.Cursor = Cursors.Default
    
      End Sub
    


     

     

    It took about six minutes to unzip the files which, considering that it's some 2-1/2 GIGS in size, isn't that bad really. That does explain, though, why I chose to use a backgroundworker.

    The good part of this is that the zip file is encrypted so even if your users find it, they can't really do much with it. The bad part is how on Earth could you distribute something this size! To that end, I have a suggestion for an alteration of the above:

    Put the encrypted zip file on a website. You could either then download it directly or, if you use ChilKat's zip utility, there's a way to give it a URL and it will extract it just like you see above directly from that URL (and it never exposes the URL in the process).

    • Marked as answer by sunshine2050 Saturday, August 6, 2011 9:59 PM
    Saturday, August 6, 2011 3:54 PM

All replies

  • you should read the answers in the other thread

    (http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/8690c06c-58e1-48f9-8cb5-ed4374e49823/#71a84b68-973b-4166-8287-f386620acd9d)

     + pay attention to the answers instead of completely ignoring people's advice + starting a new thread.


    thanks for any help
    • Proposed as answer by Mike Feng Friday, August 5, 2011 1:15 PM
    Friday, August 5, 2011 1:27 AM
  • To play an audio file from with in the resources

    My.Computer.Audio.Play(My.Resources.MyAudioFile)
    



    Faraz
    Friday, August 5, 2011 1:28 AM
  • To play an audio file from with in the resources

     

    My.Computer.Audio.Play(My.Resources.MyAudioFile)
    
    


     


    Faraz


    Faraz,

    That will only work if the audio file is a .wav file; it will not play an .mp3 file using that method.

     

    @Sunshine --> As Paul said, you need to realize that what others have pointed out may just have merit. I wish you luck with it, but I'm quite certain that I can offer nothing more, so I won't make the attempt.

    Best of luck on it though.

    Friday, August 5, 2011 3:35 AM
  • HI SunShine 

     Please check the following Link 

     http://vbcity.com/forums/t/69247.aspx

     

    Thanks 


    Want to add MVP with my name.
    Friday, August 5, 2011 5:03 AM
  • well I want others to play the media files like me but I don't want them to miss with my program as changing the programmer and so on


    assuming you have a listbox containing the exact resource names, you can write the selected song from my.resources to a temp file + it'll work:

     

    Public Class Form1
    
     Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
     Dim index As Integer = ListBox1.IndexFromPoint(e.Location)
     If index <> -1 Then
      Dim filename As String = My.Computer.FileSystem.GetTempFileName.Replace(".tmp", ".mp3")
      IO.File.WriteAllBytes(filename, DirectCast(My.Resources.ResourceManager.GetObject(ListBox1.SelectedItem.ToString), Byte()))
      AxWindowsMediaPlayer1.URL = filename
      AxWindowsMediaPlayer1.Ctlcontrols.play()
     End If
     End Sub
    
    End Class
    

     


    thanks for any help

    what is temp is ???

    I'll give U a code I made

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
      wmp.URL = "D:\path\Resources\mashary\005.mp3"
    
     End Sub
    

     


    It is very easy just click on button 4 005.mp3 plays I made 114 code like this

    there is no problem with code I suppose as it is working fine with me only with others is the problem

    sorry for ignoring U be4


    Friday, August 5, 2011 5:22 PM
  • well I want others to play the media files like me but I don't want them to miss with my program as changing the programmer and so on


    assuming you have a listbox containing the exact resource names, you can write the selected song from my.resources to a temp file + it'll work:

     

    Public Class Form1
    
     Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
     Dim index As Integer = ListBox1.IndexFromPoint(e.Location)
     If index <> -1 Then
      Dim filename As String = My.Computer.FileSystem.GetTempFileName.Replace(".tmp", ".mp3")
      IO.File.WriteAllBytes(filename, DirectCast(My.Resources.ResourceManager.GetObject(ListBox1.SelectedItem.ToString), Byte()))
      AxWindowsMediaPlayer1.URL = filename
      AxWindowsMediaPlayer1.Ctlcontrols.play()
     End If
     End Sub
    
    End Class
    

     


    thanks for any help

    can U do this codes on a button not list box plz

    Dim filename As String = My.Computer.FileSystem.GetTempFileName.Replace(".tmp", ".mp3")
    


    I want to understand this

    is that important I didn't do it but It works fine with me???

     

    AxWindowsMediaPlayer1.Ctlcontrols.play()
    

    that's why I told U be4 that I don't understand any thing of this
    Friday, August 5, 2011 5:37 PM
  • I really think that you're best to distribute the audio files along with your application - for example, maybe create a subfolder from the application folder and then put them all in there. With as many as you have though, be aware that installation - and especially updates - can take a very long time doing it that way though (assuming your installer is .msi based).

    Paul has a good solution for extracting them to a file. As for playing them from a file, here's another way that I did a while back:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/f0cf8cfc-2c1e-4ec4-ac4f-a11a3aaa05e1

    If you're adament about keeping them in resources, then I'd suggest that you write the binary to a temporary file (like Paul did) then play it in WMP from that file (then later delete the file). The drawback to anything in resources though is the difficulty in iterating through them. It's not impossible as I've seen it done here once, but it's not at all straightforward.

    Good luck with it. :)



    see my code this is very long and works on list box only


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
     wmp.URL = "D:\path\Resources\mashary\005.mp3"
    
     End Sub
    Friday, August 5, 2011 5:40 PM
  • Sunshine,

    Man you've got us all so confused that we don't know whether to scratch our watches or wind our asses ... but I think I may know wherein lies the issue: Terminology.

    Do you have these audio files in the system resources or do you have them in a FOLDER named "Resources"? If the latter, then that sheds new light on this entire topic.

    Friday, August 5, 2011 6:44 PM
  • Sunshine,

    Man you've got us all so confused that we don't know whether to scratch our watches or wind our asses ... but I think I may know wherein lies the issue: Terminology.

    Do you have these audio files in the system resources or do you have them in a FOLDER named "Resources"? If the latter, then that sheds new light on this entire topic.

    I clicked properties for my application  then resources then add exiting item then I found afolder named resources in the solution explorar

     

    I made

    3 folders in it then add exiting item every folder have 114 sound files and they are 3 folders

    these are in system resources I suppose

     

    what about build action it is set as content can the problem be from it???

    Saturday, August 6, 2011 9:07 AM
  • a picture from solution explorarwhich shows where the problem is as I think

    http://imageshack.us/photo/my-images/834/screenshot0608201111143.png/
    Saturday, August 6, 2011 9:19 AM
  • a picture from solution explorarwhich shows where the problem is as I think

    http://imageshack.us/photo/my-images/834/screenshot0608201111143.png/


    Then the method that Paul showed in the previous thread will work for you.

    I'd not have chosen resources for this, but if you're stuck with it, then use the method he explained.

    Saturday, August 6, 2011 11:33 AM
  • ok so how to use his method on my program I can't understand his method clearly

     

    How to make .tmp file ???

     

    how to put the .tmp and .mp3 in this code

     

     

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
     wmp.URL = "D:\path\Resources\mashary\005.mp3"
    
     End Sub
    Saturday, August 6, 2011 11:43 AM
  • a picture from solution explorarwhich shows where the problem is as I think

    http://imageshack.us/photo/my-images/834/screenshot0608201111143.png/


    Then the method that Paul showed in the previous thread will work for you.

    I'd not have chosen resources for this, but if you're stuck with it, then use the method he explained.


    did U saw the picture is there any problem with the underlined item
    Saturday, August 6, 2011 11:46 AM
  • Do this and I'll write something to show you as an example.

    In the program, type in the following:

    My.Resources.

    When you type that last ".", you should see your audio files, although they may have been renamed (but you'll recognize it). Tell me the name of one of the audio files that you see listed there.

    Saturday, August 6, 2011 12:07 PM
  • no thing came out My.Resources.mashary.

    or My.Resources.

     

    ????

    and plz don't ignore

    a picture from solution explorarwhich shows where the problem is as I think

    http://imageshack.us/photo/my-images/834/screenshot0608201111143.png/


    Then the method that Paul showed in the previous thread will work for you.

    I'd not have chosen resources for this, but if you're stuck with it, then use the method he explained.


    did U saw the picture is there any problem with the underlined item
    Saturday, August 6, 2011 12:17 PM
  • Yes, I saw the picture.

    Understand this: They are in a folder on your computer but they will not be on the user's computer's hard drive - they're embedded in resources, or they should be. I don't know why you're not seeing them though - that's very confusing.

    There was nothing there at all?

    If you right-click on the program name in Solution Explorer, the last option is "Properties". Click that then click on the tab for "Resources". Tell me if you see them there?

    Saturday, August 6, 2011 12:24 PM
  • not finding them???
    Saturday, August 6, 2011 12:28 PM
  • not finding them???


    Boy, this is turning into a real mess.

    IF you want them in resources, you'll need to add them there, but I'll ask again - why do you want them in resources? Do you not want the users to have access to the music files other than through your program? If that's the case then I suppose Resources would be the way to go, but if you just don't know of another way, I can explain a MUCH better and easier way to do it!

    Actually I already have explained it but you may have missed that part.

    Saturday, August 6, 2011 12:32 PM
  • something came into my mind I'll try it then telling U the results

     

    even that is there any other way that  the users aren't able access to the music files other than through my program

     

    without putting the files in resources

    Saturday, August 6, 2011 12:39 PM
  • something came into my mind I'll try it then telling U the results

     

    even that is there any other way that  the users aren't able access to the music files other than through my program

     

    without putting the files in resources


    None that I can think of, but - sort of, maybe. I'll explain.

    If you have web space and put them there, then you could use the URL. Just make sure that your space on the web doesn't support open browsing (or just create a default "index.htm" file and drop it there so they can't get around it).

    Be aware that if it's copyrighted information - this is NOT a good idea!

    Saturday, August 6, 2011 12:43 PM
  • something came into my mind I'll try it then telling U the results

     

    even that is there any other way that  the users aren't able access to the music files other than through my program

     

    without putting the files in resources


    None that I can think of, but - sort of, maybe. I'll explain.

    If you have web space and put them there, then you could use the URL. Just make sure that your space on the web doesn't support open browsing (or just create a default "index.htm" file and drop it there so they can't get around it).

    Be aware that if it's copyrighted information - this is NOT a good idea!

    good one

    index.html

    really good idea,but......

    all codes will be no thing

    Saturday, August 6, 2011 12:51 PM
  • I'm not understanding what you want to do?

    If you want them in resources, then add them in there using the page that you went to when you clicked on Properties. Following that, repeat the excercise of:

    My.Resources.

    ...then tell me the name of one of the audio files and I'll build an example.

    Saturday, August 6, 2011 12:55 PM
  • I've been thinking (sometimes a dangerous thing for me to do!), and there may be a way to do this using resources and NOT HAVE to worry with enumeration of the resources.

    How about this: Take all of your audio files and zip them into a single zip file, and put that single zip file into your resources. When the program starts up, it will create a folder:

    My.Computer.FileSystem.CreateDirectory(Application.StartUpPath & "\Temp")

    (Of course first you'd want to check that it doesn't already exist and if it does, delete using the overload to delete all contents). Now extract the zip file into that new folder and use any of the many free zip utilities available for .Net and, with that, extract the contents to the \Temp folder.

    From that point on, you're now enumerating through files.

    When the program is shutting off, you delete the entire folder (using the overload to delete all contents).

    Just a thought - haven't tried it but I'll bet it'll work.

    Saturday, August 6, 2011 1:51 PM
  • I think I'll give up and delete all these things and try to remake the program another 3 days will be "nothing":D

     

    tell me the best way to make the program by using resources that's only

    Saturday, August 6, 2011 3:49 PM
  • Well of course I had to try my theory. I have good news and bad news ;)

    The good news is that it DOES work - but the bad news is that you're limited on the size of the zip file. In my tests, the smallest I tried was around 50 megs - but it choked on it. Given these are .mp3 files, any decent collection will get to be pretty big, so I've given thought to another way we could do this, and it does work.

    It gets a little involved (not that much really) and there's a major caveat that I'll explain at the end of this. In this test, I have placed a password-protected .zip file into the application folder. It's not small!

     

     

    The code that I used follows. In this one, I'm using a component by ChilKat to do the unzipping part. It's not free, so you may want to have a look around for a free one. I know I've seen others here discuss at least one that's open source, but I can't now recall the name of it. I make this point because the methods that I show below won't work for yours - you'll need to find the documentation of the one you choose to use and follow their instruction on making it work properly.


    CODE

      Private tempFolder As String = Application.StartupPath & "\Temp"
      Private zipFile As String = Application.StartupPath & "\mp3.zip"
      Private zip As New Chilkat.Zip()
      Private bgwError As String = ""
    
    
    
      Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    
        If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
          My.Computer.FileSystem.DeleteDirectory(tempFolder, _
              FileIO.DeleteDirectoryOption.DeleteAllContents)
        End If
    
      End Sub
    
    
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim success As Boolean = False
    
        success = zip.UnlockComponent("YourRegistrationNumberHere")
        If (success <> True) Then
          MessageBox.Show("Internal Error", "Error", _
              MessageBoxButtons.OK, MessageBoxIcon.Error)
          Close()
        End If
    
        lbl_AudioFileCount.Text = "Preparing Files..."
        ProgressBar1.Style = ProgressBarStyle.Marquee
    
        Try
          If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
            My.Computer.FileSystem.DeleteDirectory(tempFolder, _
                FileIO.DeleteDirectoryOption.DeleteAllContents)
          End If
    
          My.Computer.FileSystem.CreateDirectory(tempFolder)
        Catch ex As Exception
          MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _
                  "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Close()
        End Try
    
        ListBox1.Enabled = False
        Me.Cursor = Cursors.WaitCursor
        Me.Show()
        Me.Refresh()
    
        bgw_Unzip.RunWorkerAsync()
    
      End Sub
    
    
    
      Private Sub bgw_Unzip_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_Unzip.DoWork
    
        ' Background worker to unzip the audio files
    
        Try
          zip.DecryptPassword = "mypassword"
          zip.OpenZip(zipFile)
          zip.Extract(tempFolder)
    
        Catch ex As Exception
          bgwError = ex.Message
          bgw_Unzip.CancelAsync()
        End Try
    
      End Sub
    
    
    
      Private Sub bgw_Unzip_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw_Unzip.RunWorkerCompleted
    
        If bgwError <> String.Empty Then
          MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & bgwError, _
                  "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
          Try
            If My.Computer.FileSystem.DirectoryExists(tempFolder) Then
              Dim songCnt As Integer = 0
    
              For Each songFile As String In My.Computer.FileSystem.GetFiles(tempFolder, _
                          FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
    
                Dim infoToShow As String = Replace(songFile, tempFolder, "")
                ListBox1.Items.Add(infoToShow)
                songCnt += 1
              Next
    
              lbl_AudioFileCount.Text = "Total Song Count: " & songCnt.ToString("n0")
            Else
              Throw New Exception("Error Enumerating Music Files")
            End If
    
          Catch ex As Exception
            MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _
                    "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Close()
          End Try
        End If
    
        ProgressBar1.Visible = False
        ListBox1.Enabled = True
        Me.Cursor = Cursors.Default
    
      End Sub
    


     

     

    It took about six minutes to unzip the files which, considering that it's some 2-1/2 GIGS in size, isn't that bad really. That does explain, though, why I chose to use a backgroundworker.

    The good part of this is that the zip file is encrypted so even if your users find it, they can't really do much with it. The bad part is how on Earth could you distribute something this size! To that end, I have a suggestion for an alteration of the above:

    Put the encrypted zip file on a website. You could either then download it directly or, if you use ChilKat's zip utility, there's a way to give it a URL and it will extract it just like you see above directly from that URL (and it never exposes the URL in the process).

    • Marked as answer by sunshine2050 Saturday, August 6, 2011 9:59 PM
    Saturday, August 6, 2011 3:54 PM
  • is this THE BEST WAY to make the program using resources ?

     

    If it isn't plz show me good way to do it to not fall in another problem

    Saturday, August 6, 2011 9:52 PM
  • is this THE BEST WAY to make the program using resources ?

     

    If it isn't plz show me good way to do it to not fall in another problem


    I was trying to show you a way to avoid using resources, but give you the same effect of protected files. Even if you use Paul's plan, at some point or another, the file itself will be exposed to the user (if they know when to look and where). This is just a macro to that micro - it extracts all of them at once.

    Since you've abandoned the program and you're looking for ideas on the new one, you may want to start another thread and title it exactly that: "What's the best way to create a WMV-based program that has the audio files in resources", or something.


    Saturday, August 6, 2011 9:56 PM
  • WMV you mean wmp

     

    Saturday, August 6, 2011 9:58 PM
  • WMV you mean wmp

     


    Yes, sorry!
    Saturday, August 6, 2011 10:01 PM