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/" & DownloadThisMyWebClient2.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
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...
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 Form1Dim WithEvents MyWebClient2 As New Net.WebClientDim x ' trash counter variableDim PlayerStatus As StringDim StopBatch As StringPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadVideoClipDirectoryCheck()
VideoDownloadDirectoryCheck()
AddHandler MyWebClient2.DownloadFileCompleted, AddressOf DownloadFileCallback2AddHandler MyWebClient2.DownloadProgressChanged, AddressOf DownloadProgressCallbackOpenFileDialog1.InitialDirectory = Application.StartupPath &
"\VideoClips"WindowsMediaPlayer1.uiMode =
"none"End SubPrivate Sub VideoClipDirectoryCheck()TryIO.Directory.CreateDirectory(Application.StartupPath &
"\VideoClips")Catch ex As Exception'lblStatus.Text = ex.MessageEnd TryEnd SubPrivate Sub VideoDownloadDirectoryCheck()TryIO.Directory.CreateDirectory(Application.StartupPath &
"\VideoDownloads")Catch ex As Exception'lblStatus.Text = ex.MessageEnd TryEnd Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickIf MyWebClient2.IsBusy ThenMessageBox.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 processTimer1.Enabled =
FalseEnd IfIf 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" ThenTimer1.Enabled =
FalseElseTimer1.Enabled =
TrueEnd If
If IO.File.Exists(Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf") ThenMy.Computer.FileSystem.DeleteFile(Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf")End IfIf IO.File.Exists(Application.StartupPath & "\VideoDownloads\" & "MyFloridaDemo_350-" & x & ".asf") ThenSystem.IO.File.Move(Application.StartupPath &
"\VideoDownloads\" & "MyFloridaDemo_350-" & x & ".asf", Application.StartupPath & "\VideoClips\" & "MyFloridaDemo_350-" & x & ".asf")'System.Threading.Thread.Sleep(3000)End Iflbl_DownloadStatus.Text =
"Download Completed." : Beep()MessageBox.Show(
"MyFloridaDemo_350-" & x & ".asf" & " has finished downloading. It will be moved to your movie folder now.")
End SubPrivate 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.ProgressPercentageEnd SubProtected 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 SubPublic ReadOnly Property IsBusy() As BooleanGetReturn MyWebClient2.IsBusyEnd GetEnd PropertyPrivate Sub btnStartDownloads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownloads.ClickStopBatch =
""Timer1.Enabled =
TrueEnd Sub
Private Sub btn_StopDownloads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_StopDownloads.ClickStopBatch =
"stop"End SubPrivate Sub btnLoadClip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadClip.ClickOpenFileDialog()
If OpenFileDialog1.FileName <> "" ThenWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
WindowsMediaPlayer1.uiMode =
"none"End IfEnd SubPrivate Sub OpenFileDialog()Me.OpenFileDialog1.ShowDialog()End SubEnd
Class====================================================================================================
Looking forward to any replies. Thank you!