The app haves following issues:
- It freezes each 2 seconds
- Progress bar updates each 2 seconds instead one :)
- If the internet drops for a while, the app hangs and even crashes.
- Sometimes if don't crash but just stops and must be restarted.
- UpdateTrack continues operating in loop after form is closed.
So what I did:
- Registered account with Spotify, made my own private app and used my own client ID. No more caring about rate limits! Go onward!
Fixed 2: In Viewer.vb
Made all Delay(2000)
to Delay(1000)
This made the progress update far more better but also, made the app awfully laggy, couldnt even drag it smoothly.
Then started trying to understand why it just stops. And found the culprit. You didn't catch exceptions in a place they CAN appear.
The offending place was just, naked, unprotected:
_playback = _spotify.GetPlayback()
First, GetPlayback can throw and chainthrow everythning, from System.AggregateException
, NullReferenceException
and HTTPRequestException
to things like Socket timeouts. AND, it freezes the otherwise async routine on the GUI thread! Oh and, of course, it silently kills your UpdateTrack()
chaining, effectively dying!
What I did was to add a Try/Catch block and a boolean condition check if succeeded, forcing a retry if it fails. And second, I migrated to Await
a GetPlaybackAsync
. Then, the application survived all cable, wifi disconnection, and clumsy network latency tests! Please, incorporate this into your next update! This fixed 1, 3, and 4. The program is stable, and responsible now (Well, not when it downloads the image though! You'd better add some try/catch stuff there as well. Anything that calls Spotify API can throw, and break your app)
Dim GetPlaybackCompleted As Boolean = False
Try
_playback = Await _spotify.GetPlaybackAsync()
GetPlaybackCompleted = True
Catch e As Exception
Log(e.Message)
End Try
If GetPlaybackCompleted = False Then
Await Task.Delay(1000)
UpdateTrack()
Return
End If
Finally, whatever remained was to kill the UpdateTrack chain when the viewer closes, because you didnt stopped this chain and each new opening of the viewer generated a new chain... effectively slowing down your app and fucking up with your rate limits. I added a boolean flag to kill the chain!
Dim DisableUpdates As Boolean = False
Private Async Sub UpdateTrack()
If DisableUpdates Then Return
'in SpotifyApiSleep(sender As Object, e As EventArgs) Handles Me.FormClosing
DisableUpdates = True
End Sub
To be honest I did a lot of other editions of your program to customize it to suiit my needs, namely, extract the album image and texts to textfiles, and simulating global hotkey press but everything was hardcoded in code with no way to configure it.
Please, consider adding a config file where we can replace your client id with our owns, feature to write the label, artists, song and the album image to files [so we can use image/text sources in OBS] and a hotkey simulation when the song changes (so we can trigger animations inside OBS when song changes)
Example code I used to add custom specific for me features:
' PeterSvP Add: Add some Text files for event handling in OBS ans save the cover to png!
System.IO.File.WriteAllText("Song-Title.txt", _playback.Item.Name)
System.IO.File.WriteAllText("Song-Artist.txt", artists)
System.IO.File.WriteAllText("Song-Album.txt", _playback.Item.Album.Name)
AlbumCover.Image.Save("Song-Image.png")
' Press the Minus hotkey so OBS triggers the flash animation
keybd_event(&HBD, &H9E, 0, 0)
Await Task.Delay(64)
keybd_event(&HBD, &H9E, 2, 0)
'in SpotifyApiSleep(sender As Object, e As EventArgs) Handles Me.FormClosing
'added code to delete the image and empty the text files
Dim i As System.Drawing.Image
i = New System.Drawing.Bitmap(32, 32)
i.Save("Song-Image.png")
System.IO.File.WriteAllText("Song-Title.txt", "")
System.IO.File.WriteAllText("Song-Artist.txt", "")
System.IO.File.WriteAllText("Song-Album.txt", "")
Where keybd_event is
<DllImport("user32.dll")>
Private Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger, dwExtraInfo As Integer)
End Sub
Of course they will be nice welcome if you officially support them.