媒体播放时出现WMPLib控制台应用程序异常

本文关键字:控制台 应用程序 异常 WMPLib 媒体播放 | 更新日期: 2023-09-27 18:20:54

我不习惯使用InteropServices,但我正在使用WMPLib从控制台应用程序播放歌曲当我从Visual Studio调试应用程序时,它可以正常工作但它崩溃了,给了我以下异常:

Unhandled Exception: System.Runtime.InteropServices.COMException: The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
   at WMPLib.IWMPPlayer4.get_controls()
   at ConsoleMP3Player.Program.Main(String[] args) in C:'Users'Ibrahim'Desktop'Console.Mp3'Console.Mp3'Program.cs:line 67

当我从命令行运行它时:

C:'Users'Ibrahim'Desktop'Console.Mp3'Console.Mp3'bin'Debug>ConsoleMP3Player play

以下是play命令的简单代码:

var _player = new WindowsMediaPlayer();
_player.URL = "Full path to a mp3 file";
_player.controls.play();

非常感谢您的帮助。

媒体播放时出现WMPLib控制台应用程序异常

不要使用坏的COM控件,而是尝试使用托管和线程安全的MediaPlayer类。添加对PresentationCore和WindowsBase的引用,并尝试以下操作:

using System.Windows.Media;
public void PlaySoundAsync(string filename)
{
    // This plays the file asynchronously and returns immediately.
    MediaPlayer mp = new MediaPlayer();
    mp.MediaEnded += new EventHandler(Mp_MediaEnded);
    mp.Open(new Uri(filename));
    mp.Play();
}
private void Mp_MediaEnded(object sender, EventArgs e)
{
    // Close the player once it finished playing. You could also set a flag here or raise another event.
    ((MediaPlayer)sender).Close();
}