Question

I am having a problem extracting the file directory of an audio file which is stored in my project resource folder. In my project, I have a mysounds.resx file in which I added a file (abc.mp3).

            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = "E:/xyz.mp3";
            wplayer.settings.setMode("loop",false);
            wplayer.controls.play();

Here, when I give the "E:/xyz.mp3" directory in wplayer.URL, it plays fine. But what I want to do is to get the file path from mysounds.resx file in which I stored abc.mp3 and I want to use files paths from mysounds.resx file, not any absolute paths.

Is there anyone who can help me? I am not very good in C#. I really need this work around. Thank you in advance.

Was it helpful?

Solution

Writing an audio file in a Resource to a temporary file, and then playing it using WMPLib.

//Set up the temp path, I'm using a GUID for the file name to avoid any conflicts
var temporaryFilePath = String.Format("{0}{1}{2}", System.IO.Path.GetTempPath(), Guid.NewGuid().ToString("N"), ".mp3") ;

//Your resource accessor, my resource is called AudioFile
using (var memoryStream = new MemoryStream(Properties.Resources.AudioFile))
using(var tempFileStream = new FileStream(temporaryFilePath, FileMode.Create, FileAccess.Write))
{
    //Set the memory stream position to 0
    memoryStream.Position = 0;

    //Reads the bytes from the audio file in resource, and writes them to the file
    memoryStream.WriteTo(tempFileStream);
}

//Play your file
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = temporaryFilePath;
wplayer.settings.setMode("loop", false);
wplayer.controls.play();

//Delete the file after use
if(File.Exists(temporaryFilePath))
    File.Delete(temporaryFilePath);

OTHER TIPS

a) OK, first add audio file (.wav) into project resource.

  1. Open "Solution Explorer" from menu toolbar ("VIEW") or simply press Ctrl+Alt+L.
  2. Click on drop-down list of "Properties".
  3. Then select "Resource.resx" and press enter.

open project resource

  1. Now select "Audio" from the combobox list.

add audio files to resource

  1. Then click on "Add Resource", choose audio files (.wav) and click "Open".

browsing for audio files

  1. Select audio file(s) and change "Persistence" properties to "Embedded in .resx".

embedding audio files to resource

b) Now, just write this code to play the audio.

In this code I'm playing audio on form load event.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Media; // at first you've to import this package to access SoundPlayer

namespace WindowsFormsApplication1
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private void login_Load(object sender, EventArgs e)
        {
            playaudio(); // calling the function
        }

        private void playaudio() // defining the function
        {
            SoundPlayer audio = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name
            audio.Play();
        }
    }
}

That it.
All done, now run the project (press f5) and enjoy your sound.
All the best, Good bye. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top