Sunday, 25 August 2013

How to avoid exception when a dependency is missing? (WMP control)

How to avoid exception when a dependency is missing? (WMP control)

I have an application which can play audio from URL's in MP3 format and up
until now I've been using the WMP control for this task.
(Interop.WMPLib.dll)
But recently I've discovered that my application will not start, throwing
"System.InvalidOperationException" for clients that do not have WMP
installed.
The only alternative methods I've found by searching is either using a
method of which only supports WAV files or methods of which still require
WMP.
Are there any alternatives which don't require much from the client? Or is
there a way to have that exception ignored if the client does not have WMP
installed and just continue running the application with the mp3 playing
feature disabled?
Might my implementation be causing the close on exception at startup?
Here's code that might be relevant: (I'll include how I'm handling
exceptions just incase there's anything I can change in that)
.Net Framework 4.0, Target CPU: x86
Oh and 'Embed Interop Types' is 'True' for '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
Of course, there's more to the code than that but I only included anything
relevant.
Help appreciated~!
Edit: I found this method
http://content.gpwiki.org/index.php/VBNET:AudioVideoPlayback
Would that be more or less compatible? >-<

No comments:

Post a Comment