【问题标题】:How to extract file path from an audio file stored in project resource folder in C#如何从存储在 C# 项目资源文件夹中的音频文件中提取文件路径
【发布时间】:2014-02-03 21:13:49
【问题描述】:

我在提取存储在我的项目资源文件夹中的音频文件的文件目录时遇到问题。在我的项目中,我有一个 mysounds.resx 文件,我在其中添加了一个文件 (abc.mp3)。

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

在这里,当我在 wplayer.URL 中输入“E:/xyz.mp3”目录时,它可以正常播放。但我想做的是从存储 abc.mp3 的 mysounds.resx 文件中获取文件路径,并且我想使用 mysounds.resx 文件中的文件路径,而不是任何绝对路径。

有人可以帮助我吗?我在 C# 方面不是很好。我真的需要解决这个问题。提前谢谢你。

【问题讨论】:

  • 您是否将整个 MP3 文件添加为资源,或者您是否添加了带有 MP3 文件路径的 string 资源?如果是前者,那么您必须将资源写入临时文件,并将该 FilePath 作为 URL 传递。
  • 我将整个 MP3 文件添加为资源。您能否参考任何代码片段,以便我了解如何将资源写入临时文件并将该 FilePath 作为 URL 传递?对不起,我是 C# 的新手。这会很有帮助。谢谢
  • 请参阅下面的解决方案以获取代码 sn-p。干杯。

标签: c# audio directory resx wmplib


【解决方案1】:

将资源中的音频文件写入临时文件,然后使用 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) 好的,首先将音频文件 (.wav) 添加到项目资源中。

    1. 从菜单工具栏(“视图”)打开“解决方案资源管理器”或直接按 Ctrl+Alt+L。
    2. 单击“属性”下拉列表。
    3. 然后选择“Resource.resx”并按回车键。

    1. 现在从组合框列表中选择“音频”。

    1. 然后点击“添加资源”,选择音频文件(.wav)并点击“打开”。

    1. 选择音频文件并将“Persistence”属性更改为“Embedded in .resx”。

    b) 现在,只需编写这段代码来播放音频。

    在这段代码中,我在表单加载事件中播放音频。

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

    那个。
    全部完成,现在运行项目(按 f5)并享受您的声音。
    万事如意,再见。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2014-07-12
      • 2010-12-02
      • 2021-06-11
      • 2013-06-08
      相关资源
      最近更新 更多