C#でサウンドを再生する実装方法はたくさんありますが、WMPLibのWindowsMediaPlayerの利用方法が一般的です。
複数再生も、URLを指定することで、簡単に実現することができます。
このURLに設定するパスの指定に誤りがあると、下記のような例外が発生します。
例外
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
System.Runtime.InteropServices.COMException HResult=0xC00D132B Message=HRESULT からの例外:0xC00D132B Source=Interop.WMPLib スタック トレース: at WMPLib.WindowsMediaPlayerClass.set_URL(String pbstrURL) at Barcode.Scan(FormMain formMain, FormLog formLog, Info info, TextBox textBoxBaseJan, TextBox& textDummy, Label& lblScanBuf, SerialPort& serialPort1) in C:\Sample.cs:line 93 at System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e) at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m) at System.Windows.Forms.Control.ProcessKeyMessage(Message& m) at System.Windows.Forms.Control.WmKeyChar(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.TextBoxBase.WndProc(Message& m) at System.Windows.Forms.TextBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Sample.Program.Main() in C:\tmp\source\repos\Program.cs:line 19 |
解決方法
この例外の原因は、mp3などの音声ファイルのパスの指定方法が相対パスになっていることが原因です。
そのため、絶対パスの指定方法に変更することで、例外が発生しなくなります。
相対パスから、絶対パスの変換は、GetFullPathを利用することで、簡単に変換することが出来ます。
1 |
_mplayer1.URL = Path.GetFullPath("..\\..\\sound\\sample.mp3"); |