Exemple #1
0
        private void OnOnLoggedIn(IntPtr sessionPtr, libspotify.sp_error error)
        {
            if (error == libspotify.sp_error.OK)
            {
                IsLoggedIn = true;
            }

            Log.Debug(ModuleId, String.Format("Session Logged IN: {0}", error));

            if (_programSemafore != null)
            {
                _programSemafore.Set();
            }
        }
Exemple #2
0
        private void _AlbumbrowseComplete(IntPtr result, IntPtr userDataPtr)
        {
            try
            {
                libspotify.sp_error error = libspotify.sp_albumbrowse_error(result);

                if (error != libspotify.sp_error.OK)
                {
                    _session.Log.Error(ModuleId, String.Format("Album browse failed: {0}", libspotify.sp_error_message(error)));
                    return;
                }

                if (Tracks == null)
                {
                    Tracks = new List <Track>();
                }

                else
                {
                    if (Tracks != null)
                    {
                        foreach (var track in Tracks)
                        {
                            track.Dispose();
                        }
                    }

                    Tracks.Clear();
                }

                int numtracks = libspotify.sp_albumbrowse_num_tracks(_browsePtr);
                for (int i = 0; i < libspotify.sp_albumbrowse_num_tracks(_browsePtr); i++)
                {
                    var trackPtr = libspotify.sp_albumbrowse_track(_browsePtr, i);
                    var track    = new Track(trackPtr, _session);
                    Tracks.Add(track);
                }
                this.IsBrowseComplete = true;
            }
            finally
            {
                EndBrowse();
            }
        }
Exemple #3
0
        private void _ArtistbrowseComplete(IntPtr result, IntPtr userDataPtr)
        {
            try
            {
                libspotify.sp_error error = libspotify.sp_albumbrowse_error(result);

                if (error != libspotify.sp_error.OK)
                {
                    _session.Log.Error(ModuleId, String.Format("Artist browse failed: {0}", libspotify.sp_error_message(error)));
                    return;
                }

                int numalbums = libspotify.sp_artistbrowse_num_albums(_browsePtr);

                if (this.Albums == null)
                {
                    this.Albums = new List <Album>();
                }

                for (int i = 0; i < libspotify.sp_artistbrowse_num_albums(_browsePtr); i++)
                {
                    IntPtr albumPtr = libspotify.sp_artistbrowse_album(_browsePtr, i);

                    // excluding singles, compilations, and unknowns
                    if (libspotify.sp_album_type(albumPtr) == libspotify.sp_albumtype.SP_ALBUMTYPE_ALBUM &&
                        libspotify.sp_album_is_available(albumPtr))
                    {
                        this.Albums.Add(new Album(albumPtr, _session));
                    }
                }

                this.Biography = Functions.PtrToString(libspotify.sp_artistbrowse_biography(_browsePtr));

                this.IsBrowseComplete = true;
            }
            finally
            {
                EndBrowse();
            }
        }
Exemple #4
0
        protected virtual libspotify.sp_error Init()
        {
            // first add our handlers
            OnNotifyMainThread += OnOnNotifyMainThread;
            OnLoggedIn         += OnOnLoggedIn;
            OnLoggedOut        += OnOnLoggedOut;
            OnPlayTokenLost    += OnOnPlayTokenLost;

            // then custom ones
            InitEvents();

            Callbacks = new libspotify.sp_session_callbacks();
            if (OnNotifyMainThread != null)
            {
                Callbacks.notify_main_thread = Marshal.GetFunctionPointerForDelegate(OnNotifyMainThread);
            }
            if (OnConnectionError != null)
            {
                Callbacks.connection_error = Marshal.GetFunctionPointerForDelegate(OnConnectionError);
            }
            if (OnEndOfTrack != null)
            {
                Callbacks.end_of_track = Marshal.GetFunctionPointerForDelegate(OnEndOfTrack);
            }
            if (OnGetAudioBufferStats != null)
            {
                Callbacks.get_audio_buffer_stats = Marshal.GetFunctionPointerForDelegate(OnGetAudioBufferStats);
            }
            if (OnLogMessage != null)
            {
                Callbacks.log_message = Marshal.GetFunctionPointerForDelegate(OnLogMessage);
            }
            if (OnLoggedIn != null)
            {
                Callbacks.logged_in = Marshal.GetFunctionPointerForDelegate(OnLoggedIn);
            }
            if (OnLoggedOut != null)
            {
                Callbacks.logged_out = Marshal.GetFunctionPointerForDelegate(OnLoggedOut);
            }
            if (OnMessageToUser != null)
            {
                Callbacks.message_to_user = Marshal.GetFunctionPointerForDelegate(OnMessageToUser);
            }
            if (OnMetadataUpdated != null)
            {
                Callbacks.metadata_updated = Marshal.GetFunctionPointerForDelegate(OnMetadataUpdated);
            }
            if (OnMusicDelivery != null)
            {
                Callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(OnMusicDelivery);
            }
            if (OnOfflineStatusUpdated != null)
            {
                Callbacks.offline_status_updated = Marshal.GetFunctionPointerForDelegate(OnOfflineStatusUpdated);
            }
            if (OnPlayTokenLost != null)
            {
                Callbacks.play_token_lost = Marshal.GetFunctionPointerForDelegate(OnPlayTokenLost);
            }
            if (OnStartPlayback != null)
            {
                Callbacks.start_playback = Marshal.GetFunctionPointerForDelegate(OnStartPlayback);
            }
            if (OnStopPlayback != null)
            {
                Callbacks.stop_playback = Marshal.GetFunctionPointerForDelegate(OnStopPlayback);
            }
            if (OnStreamingError != null)
            {
                Callbacks.streaming_error = Marshal.GetFunctionPointerForDelegate(OnStreamingError);
            }
            if (OnUserinfoUpdated != null)
            {
                Callbacks.userinfo_updated = Marshal.GetFunctionPointerForDelegate(OnUserinfoUpdated);
            }
            if (OnConnectionStateUpdated != null)
            {
                Callbacks.connectionstate_updated = Marshal.GetFunctionPointerForDelegate(OnConnectionStateUpdated);
            }
            if (OnCredentialsBlobUpdated != null)
            {
                Callbacks.credentials_blob_updated = Marshal.GetFunctionPointerForDelegate(OnCredentialsBlobUpdated);
            }
            if (OnOfflineError != null)
            {
                Callbacks.offline_error = Marshal.GetFunctionPointerForDelegate(OnOfflineError);
            }
            if (OnPrivateSessionModeChanged != null)
            {
                Callbacks.private_session_mode_changed =
                    Marshal.GetFunctionPointerForDelegate(OnPrivateSessionModeChanged);
            }
            if (OnScobbleError != null)
            {
                Callbacks.scrobble_error = Marshal.GetFunctionPointerForDelegate(OnScobbleError);
            }


            IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Callbacks));

            Marshal.StructureToPtr(Callbacks, callbacksPtr, true);

            if (String.IsNullOrEmpty(CacheLocation))
            {
                CacheLocation = Path.Combine(Path.GetTempPath(),
                                             "spotify_api_temp" + Path.DirectorySeparatorChar + AppId);
            }

            var config = new libspotify.sp_session_config
            {
                api_version          = libspotify.SPOTIFY_API_VERSION,
                user_agent           = "Spoti",
                application_key_size = AppKey.Length,
                application_key      = Marshal.AllocHGlobal(AppKey.Length),
                cache_location       = CacheLocation, // Path.Combine(Path.GetTempPath(), "spotify_api_temp\\" + _appId),
                settings_location    = CacheLocation,
                // Path.Combine(Path.GetTempPath(), "spotify_api_temp\\" + _appId) : ,
                callbacks          = callbacksPtr,
                compress_playlists = true,
                dont_save_metadata_for_playlists = false,
                initially_unload_playlists       = false
            };

            Log.Debug(ModuleId, String.Format("api_version={0}", config.api_version));
            Log.Debug(ModuleId, String.Format("application_key_size={0}", config.application_key_size));
            Log.Debug(ModuleId, String.Format("cache_location={0}", config.cache_location));
            Log.Debug(ModuleId, String.Format("settings_location={0}", config.settings_location));

            Marshal.Copy(AppKey, 0, config.application_key, AppKey.Length);

            IntPtr sessionPtr;

            libspotify.sp_error err = libspotify.sp_session_create(ref config, out sessionPtr);

            if (err == libspotify.sp_error.OK)
            {
                Handle = sessionPtr;
                libspotify.sp_session_set_connection_type(sessionPtr,
                                                          libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED);
            }

            var preferredBitrate = libspotify.sp_session_preferred_bitrate(sessionPtr,
                                                                           libspotify.sp_bitrate.BITRATE_320k);

            if (preferredBitrate == libspotify.sp_error.OK)
            {
                Log.Debug(ModuleId, "Session Bitrate set to 320 Kbp/s");
            }
            else
            {
                Log.Warning(ModuleId, "Failed to set session bitrate to 320 Kbp/s");
            }


            // _workerSemafore.Set();

            return(err);
        }