C# 如何在C Windows窗体的后台运行进程时播放mp3 我尝试了一个应用程序的寡头表单,它运行一个过程,实时地将C++ exe的输出重定向到C应用程序。重定向的C++输出然后被用来触发多个任务。例如:如果重定向输出等于playSong,则应播放mp3文件。C++输出重定向工作很好,因为我已经用它来触发其他任务,比如打开和关闭窗体,但只用于播放MP3,它不起作用,之后的代码似乎也起作用。此外,我使用OpenCV在C++程序中,如果这是问题,但是我试着在不同的项目中播放MP3,而不使用任何进程,MP3播放得很好。如果有人能在这方面帮助我,那就太好了。下面是我如何尝试的 using System.Diagnostics; using System.Runtime.InteropServices; public partial class frmPlayList : Form { [DllImport("winmm.dll")] private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); public bool flag = true; static string varr2; public frmPlayList() { InitializeComponent(); } private void frmPlayList_Load(object sender, EventArgs e) { Program.getProcess().OutputDataReceived += processOutputDataReceived2; } public void processOutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); varr2 = e.Data; if (varr2 == "playSong" && flag) { playSong(); flag = false; } } public delegate void UpdateControlsDelegate(); public void playSong() //Playing song { InvokeUpdateControlsForPlaySong(); } public void InvokeUpdateControlsForPlaySong() { if (this.InvokeRequired) { this.BeginInvoke(new UpdateControlsDelegate(UpdateControlsForPlaySong)); } else { UpdateControlsForPlaySong(); } } private void UpdateControlsForPlaySong() { string f = @"path\a.mp3"; mciSendString("open \"" + f + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero); mciSendString("play MediaFile", null, 0, IntPtr.Zero); Console.WriteLine("Song ended!"); } }

C# 如何在C Windows窗体的后台运行进程时播放mp3 我尝试了一个应用程序的寡头表单,它运行一个过程,实时地将C++ exe的输出重定向到C应用程序。重定向的C++输出然后被用来触发多个任务。例如:如果重定向输出等于playSong,则应播放mp3文件。C++输出重定向工作很好,因为我已经用它来触发其他任务,比如打开和关闭窗体,但只用于播放MP3,它不起作用,之后的代码似乎也起作用。此外,我使用OpenCV在C++程序中,如果这是问题,但是我试着在不同的项目中播放MP3,而不使用任何进程,MP3播放得很好。如果有人能在这方面帮助我,那就太好了。下面是我如何尝试的 using System.Diagnostics; using System.Runtime.InteropServices; public partial class frmPlayList : Form { [DllImport("winmm.dll")] private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); public bool flag = true; static string varr2; public frmPlayList() { InitializeComponent(); } private void frmPlayList_Load(object sender, EventArgs e) { Program.getProcess().OutputDataReceived += processOutputDataReceived2; } public void processOutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); varr2 = e.Data; if (varr2 == "playSong" && flag) { playSong(); flag = false; } } public delegate void UpdateControlsDelegate(); public void playSong() //Playing song { InvokeUpdateControlsForPlaySong(); } public void InvokeUpdateControlsForPlaySong() { if (this.InvokeRequired) { this.BeginInvoke(new UpdateControlsDelegate(UpdateControlsForPlaySong)); } else { UpdateControlsForPlaySong(); } } private void UpdateControlsForPlaySong() { string f = @"path\a.mp3"; mciSendString("open \"" + f + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero); mciSendString("play MediaFile", null, 0, IntPtr.Zero); Console.WriteLine("Song ended!"); } },c#,winforms,C#,Winforms,为什么不使用媒体播放器??把它藏起来 我不能回答你的问题,但在你的位置上,我会尝试使用不同的方法来播放mp3文件。也许WiMM和McISEnTrand干扰了C++到C连接。另外,你是否考虑过在C程序中使用C++代码而不是并行运行两个进程?尝试使用NoAdio库,这是为了使用音频文件,并且易于使用USPIAH,我使用了WMPLIB的Windows媒体播放器。谢谢你的帮助

为什么不使用媒体播放器??把它藏起来

我不能回答你的问题,但在你的位置上,我会尝试使用不同的方法来播放mp3文件。也许WiMM和McISEnTrand干扰了C++到C连接。另外,你是否考虑过在C程序中使用C++代码而不是并行运行两个进程?尝试使用NoAdio库,这是为了使用音频文件,并且易于使用USPIAH,我使用了WMPLIB的Windows媒体播放器。谢谢你的帮助