Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example-Code in vb.net
12-08-2013, 11:16 PM
Post: #1
Example-Code in vb.net
Hey!
I just found LibSpotify today and I'm trying to use it in vb.net.
But I don't even know how to import it or anything?

Can you help me with importing it and maybe give me some examples how to do easy things with it?
Find all posts by this user
14-08-2013, 10:06 AM (This post was last modified: 14-08-2013 10:06 AM by andreww.)
Post: #2
RE: Example-Code in vb.net
Try having a look at this thread here: http://forum.openhome.org/showthread.php?tid=1113

It should help you follow the instructions to build ohLibSpotify.dll from the source files. (I'm afraid we don't have a prebuilt version to download.)

Here's the example code from that thread re-written in VB.net. Create a new Windows Forms project, add a text box, set it to dock to the full window and turn on its vertical scrollbar. Add a reference to ohLibSpotify.dll, then use this code. When you run it, you'll need to make sure spotify_appkey.key is in the current directory, and that the native libspotify.dll is in the same directory as the executable. (This is an extremely bare-bones example. In a proper application you'd embed the app-key rather than read it from a file and you'd prompt the user for login credentials. You'd do something more interesting than just print libspotify's log messages. But this should get you up and running.)

Code:
Imports SpotifySharp
Imports System.Threading
Imports System.Collections.Concurrent
Imports System.IO

Public Class Form1

    Class Listener
        Inherits SpotifySessionListener
        Private form As Form1
        Public Sub New(form As Form1)
            Me.form = form
        End Sub
        Public Overrides Sub NotifyMainThread(session As SpotifySession)
            form.InvokeProcessEvents(session)
        End Sub
        Public Overrides Sub LoggedIn(session As SpotifySession, spError As SpotifyError)
            form.TextBox1.AppendText("Logged in!" + vbCrLf)
        End Sub
        Public Overrides Sub ConnectionError(session As SpotifySession, spError As SpotifyError)
            form.TextBox1.AppendText(String.Format("ConnectionError: {0}" + vbCrLf, spError))
        End Sub
        Public Overrides Sub LogMessage(session As SpotifySession, data As String)
            form.logMessages.Enqueue(data)
            NotifyMainThread(session)
        End Sub

    End Class
    Private syncContext As SynchronizationContext
    Private session As SpotifySession
    Private timer As Timer
    Private logMessages As ConcurrentQueue(Of String)
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        syncContext = SynchronizationContext.Current
        logMessages = New ConcurrentQueue(Of String)()
        Dim config = New SpotifySessionConfig()

        config.ApiVersion = 12
        config.CacheLocation = "spotifydata"
        config.SettingsLocation = "spotifydata"
        config.ApplicationKey = File.ReadAllBytes("spotify_appkey.key")
        config.UserAgent = "My Spotify Test App"
        config.Listener = New Listener(Me)
        timer = new System.Threading.Timer(Sub(obj) InvokeProcessEvents(session), Nothing, Timeout.Infinite, Timeout.Infinite)
        session = SpotifySession.Create(config)
        session.Login(<USERNAME_HERE>, <PASSWORD_HERE>, false, Nothing)
    End Sub
    Private Sub InvokeProcessEvents(session As SpotifySession)
        syncContext.Post(Sub(obj) ProcessEvents(session), Nothing)
    End Sub
    Private Sub ProcessEvents(session As SpotifySession)
        Me.session = session
        Dim limit = 0
        Dim message = ""
        While logMessages.TryDequeue(message)
            textBox1.AppendText(message + vbCrLf)
        End While
        While limit = 0
            session.ProcessEvents(limit)
        End While
        timer.Change(limit, Timeout.Infinite)
    End Sub
End Class
Visit this user's website Find all posts by this user


Forum Jump: