OpenHome Forum
Example-Code in - Printable Version

+- OpenHome Forum (
+-- Forum: OpenHome (/forumdisplay.php?fid=1)
+--- Forum: LibSpotify (/forumdisplay.php?fid=6)
+--- Thread: Example-Code in (/showthread.php?tid=1134)

Example-Code in - timi257 - 12-08-2013 11:16 PM

I just found LibSpotify today and I'm trying to use it in
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?

RE: Example-Code in - andreww - 14-08-2013 10:06 AM

Try having a look at this thread here:

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 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.)

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)
        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)
        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.

        ' 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
        End While
        timer.Change(limit, Timeout.Infinite)
    End Sub
End Class