MyWebClient2.DownloadFileAsync(address, fileName) and Windows Media Player

My first post... go easy on me guys.... Thanks.

I found what I think might be undocumented issue with these two together.

I am creating a custom media player with Windows Media Player as the core. The application also has a pop out side panel which has a webbrowser on it and a ListView.

From the ListView you can CHECK a few videoclips to download and pass them to the downloader which uses

Dim addressAsNew Uri("http://www.mywebsite.com/myvideos/shortclips/" & DownloadThis)

Dim fileNameAsString ="MyVideoDownload/" & DownloadThis

MyWebClient2.DownloadFileAsync(address, fileName)

This works just great for ever file in the list....

** Now here comes the issue.... Once you load a file into the media player, I expect it ties up the DownloadFileAsync because now when you pass a file to the routine it basically ignores it and raises a COMPLETED exception.

I have tested this thing 12 ways to sunday.... Removed handles for progress and completed and still it will not download once you have loaded a file into the player. even if you PAUSE, STOP orWindowsMediaPlayer1.URL = "". Once the media player is made active you loose MyWebClient.DownloadFileAsync ability. I expect thsi is due to the progressive downloading ability built inside of the windows media player.

Looking forward to anyones ideas on this. I will be looking at moving the downloadfile to the backgroundworker class but should not have to do this....

Thank you,

Dale Graham

[2264 byte] By [DaleGraham] at [2007-12-23]
# 1

You'll probably need to post a better example of the code you are using (not the whole project, but at least snipits that deal with everything you mentioned).

Is the filename different each time? How did you reference media player? What does the web browser have to do wtih all this (is that where you're getting access to media player - by using IE's plugin)?

It's hard to imagine that automating Media Player would have any direct effect on the WebClient class...

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Here is an example I wrote.... Short sweet, no frills...

before you do anything at all with media player, hit 'Start Downloads'

You will download 9 files..... You can of course stop after file 3 or 4 if you wish... You can tell the loop and the process work at that point.

Now 'LOAD A CLIP' - WHILE the DOWNLOADS are in progress.... what ever file you are downloading will be the last one to work... You will get FILE COMPLETE callback everytime now...

Download the zipped project here: http://www.dalegraham.com/FileDownloadAsyncTest.zip

Here is the code:

Public Class Form1

Dim WithEvents MyWebClient2 As New Net.WebClient

Dim x ' trash counter variable

Dim PlayerStatus As String

Dim StopBatch As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

VideoClipDirectoryCheck()

VideoDownloadDirectoryCheck()

AddHandler MyWebClient2.DownloadFileCompleted, AddressOf DownloadFileCallback2

AddHandler MyWebClient2.DownloadProgressChanged, AddressOf DownloadProgressCallback

OpenFileDialog1.InitialDirectory = Application.StartupPath & "\VideoClips"

WindowsMediaPlayer1.uiMode = "none"

End Sub

Private Sub VideoClipDirectoryCheck()

Try

IO.Directory.CreateDirectory(Application.StartupPath & "\VideoClips")

Catch ex As Exception

'lblStatus.Text = ex.Message

End Try

End Sub

Private Sub VideoDownloadDirectoryCheck()

Try

IO.Directory.CreateDirectory(Application.StartupPath & "\VideoDownloads")

Catch ex As Exception

'lblStatus.Text = ex.Message

End Try

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If MyWebClient2.IsBusy Then

MessageBox.Show("Currently downloading another file...Your file has been added to the que.")

Else

'MessageBox.Show("DING 3")

x += 1 : If x > 9 Then Timer1.Enabled = False : MessageBox.Show("Done with all downloads")

Dim address As New Uri("http://www.dalegraham.com/windowsmediaplayerdemotestclips/testclips/MyFloridaDemo_350.asf")

Dim fileName As String = "VideoDownloads/" & "MyFloridaDemo_350-" & x & ".asf"

MyWebClient2.DownloadFileAsync(address, fileName) ' ** This is the actual DOWNLOAD process

Timer1.Enabled = False

End If

If x = 9 Then Timer1.Enabled = False : MessageBox.Show("Done with all downloads")

End Sub

Private Sub DownloadFileCallback2(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

If StopBatch = "stop" Then

Timer1.Enabled = False

Else

Timer1.Enabled = True

End If

If IO.File.Exists(Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf") Then

My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf")

End If

If IO.File.Exists(Application.StartupPath & "\VideoDownloads\" & "MyFloridaDemo_350-" & x & ".asf") Then

System.IO.File.Move(Application.StartupPath & "\VideoDownloads\" & "MyFloridaDemo_350-" & x & ".asf", Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf")

'System.Threading.Thread.Sleep(3000)

End If

lbl_DownloadStatus.Text = "Download Completed." : Beep()

MessageBox.Show("MyFloridaDemo_350-" & x & ".asf" & " has finished downloading. It will be moved to your movie folder now.")

End Sub

Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As Net.DownloadProgressChangedEventArgs)

lbl_DownloadStatus.Text = "MyFloridaDemo_350-" & x & ".asf" & " " & CStr(e.UserState) & e.BytesReceived & " of " & e.TotalBytesToReceive & " Percentage:" & e.ProgressPercentage

End Sub

Protected Overridable Sub OnDownloadFileCompleted(ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

MessageBox.Show("MyFloridaDemo_350-" & x & ".asf" & " has finished downloading. Will MOVE the file into your movie folder now.")

System.IO.File.Move(Application.StartupPath & "\VideoDownloads\" & "MyFloridaDemo_350-" & x & ".asf", Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf")

End Sub

Public ReadOnly Property IsBusy() As Boolean

Get

Return MyWebClient2.IsBusy

End Get

End Property

Private Sub btnStartDownloads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownloads.Click

StopBatch = ""

Timer1.Enabled = True

End Sub

Private Sub btn_StopDownloads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_StopDownloads.Click

StopBatch = "stop"

End Sub

Private Sub btnLoadClip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadClip.Click

OpenFileDialog()

If OpenFileDialog1.FileName <> "" Then

WindowsMediaPlayer1.URL = OpenFileDialog1.FileName

WindowsMediaPlayer1.uiMode = "none"

End If

End Sub

Private Sub OpenFileDialog()

Me.OpenFileDialog1.ShowDialog()

End Sub

End Class

====================================================================================================

Looking forward to any replies. Thank you!

DaleGraham at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...