我有一个应用程序可以播放来自 URL 的 MP3 格式的音频,到目前为止,我一直在使用 WMP 控件来完成这项任务。(Interop.WMPLib.dll)
但最近我发现我的应用程序无法启动,为未安装 WMP 的客户端抛出“System.InvalidOperationException”。
我通过搜索找到的唯一替代方法是使用仅支持 WAV 文件的方法或仍需要 WMP 的方法。
有没有其他对客户要求不高的替代方案?或者,如果客户端没有安装 WMP 并在禁用 mp3 播放功能的情况下继续运行应用程序,是否有办法忽略该异常?
我的实现可能会导致启动时关闭异常吗?这是可能相关的代码:(我将包括我如何处理异常,以防万一有什么我可以改变的)
.Net Framework 4.0,目标 CPU:x86
哦,对于“Interop.WMPLib.dll”,“嵌入互操作类型”是“真”
Imports WMPLib
Public Class frmMain
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
AddHandler Application.ThreadException, AddressOf OnThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionEventRaised
InitializeComponent()
System.Windows.Forms.Application.EnableVisualStyles()
End Sub
Sub UnhandledExceptionEventRaised(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
If e.IsTerminating Then
Dim o As Object = e.ExceptionObject
MessageBox.Show(o.ToString)
End If
End Sub
Private Sub OnThreadException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
MessageBox.Show(e.Exception.Message & Environment.NewLine & Environment.NewLine & e.Exception.InnerException.ToString)
End Sub
Public WithEvents mp3Player As New WindowsMediaPlayer
'frmMain_Load etc..
Private Sub btnAudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAudio.Click
Dim mp3URL As String = "http://host.com/file.mp3"
mp3Player.URL = mp3URL
mp3Player.controls.play()
End Sub
End Class
当然,代码还有更多内容,但我只包含了任何相关的内容。
帮助赞赏~!
编辑:我找到了这个方法http://content.gpwiki.org/index.php/VBNET:AudioVideoPlayback
这会或多或少兼容吗?>-<