How to extract file path from an audio file stored in project resource folder in C#

徘徊边缘 提交于 2019-12-25 12:37:12

问题


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.


回答1:


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);



回答2:


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.

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

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

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

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. :)



来源:https://stackoverflow.com/questions/21537899/how-to-extract-file-path-from-an-audio-file-stored-in-project-resource-folder-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!