0

プロジェクト リソース フォルダに保存されているオーディオ ファイルのファイル ディレクトリを抽出する際に問題が発生しています。私のプロジェクトには、ファイル (abc.mp3) を追加した mysounds.resx ファイルがあります。

            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#があまり得意ではありません。私は本当にこの回避策が必要です。前もって感謝します。

4

2 に答える 2

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);
于 2014-02-04T19:27:37.430 に答える
0

a) OK、最初に音声ファイル (.wav) をプロジェクト リソースに追加します。

  1. メニュー ツールバー ("VIEW") から "Solution Explorer" を開くか、単に Ctrl+Alt+L を押します。
  2. 「プロパティ」のドロップダウンリストをクリックします。
  3. 次に、「Resource.resx」を選択し、Enter キーを押します。

プロジェクト リソースを開く

  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 キーを押す)、サウンドを楽しんでください。
さようなら。:)

于 2015-02-17T11:19:11.900 に答える