Exemplo n.º 1
0
        /// <summary>
        /// Initiate a new Spotify Session using the provided configuration, username and password. Please note that using multiple sessions per application is unsupported.
        /// </summary>
        /// <param name="Username">The username to login with.</param>
        /// <param name="Password">The password to login with. You may not store this as plaintext according to the libspotify Terms of Use.</param>
        /// <param name="Configuration">The session configuration to use.</param>
        public SpotifySession(string Username, string Password, SpotifySessionConfig Configuration)
        {
            libspotify.sp_session_config sessionconfig = new libspotify.sp_session_config();
            sessionconfig.api_version = Configuration.APIVersion;
            IntPtr apikey = Marshal.AllocHGlobal(Configuration.APIKey.Length);
            Marshal.Copy(Configuration.APIKey, 0, apikey, Configuration.APIKey.Length);
            sessionconfig.application_key = apikey;
            sessionconfig.application_key_size = Configuration.APIKey.Length;
            sessionconfig.cache_location = Configuration.CacheLocation;
            sessionconfig.device_id = Configuration.DeviceID;
            sessionconfig.compress_playlists = Configuration.CompressPlaylists;
            sessionconfig.dont_save_metadata_for_playlists = Configuration.DontSaveMetadataForPlaylists;
            sessionconfig.initially_unload_playlists = Configuration.InitiallyUnloadPlaylists;
            sessionconfig.settings_location = Configuration.SettingsLocation;
            sessionconfig.user_agent = Configuration.UserAgent;
            //TODO: Implement callbacks to get music streams etc.
            //TODO: Fix the proxy below, needs to get type.
            /*sessionconfig.proxy = Configuration.Proxy.GetProxy(new Uri("http://spotify.com/")).AbsoluteUri; //Should be correct. I think.
            sessionconfig.proxy_username = Configuration.Proxy.Credentials.GetCredential(new Uri("http://spotify.com/"),*/
            sessionconfig.ca_certs_filename = Configuration.CACertsName;
            libspotify.sp_session_callbacks callbacks = new libspotify.sp_session_callbacks();
            callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(mdd);
            IntPtr callbacksNative = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
            Marshal.StructureToPtr(callbacks, callbacksNative, true);
            sessionconfig.callbacks = callbacksNative;

            temp = libspotify.sp_session_create(ref sessionconfig, out spotsession);
            if (temp != libspotify.sp_error.OK)
            {
                SpotifyExceptions.SessionCreateFailedException exc = new SpotifyExceptions.SessionCreateFailedException(libspotify.sp_error_message(temp));
                throw exc;
            }
            temp = libspotify.sp_session_login(spotsession, Username, Password, false, null);
            if (temp != libspotify.sp_error.OK)
            {
                SpotifyExceptions.LoginFailedException exc = new SpotifyExceptions.LoginFailedException(libspotify.sp_error_message(temp));
                throw exc;
            }
        }
        public string GetNowPlaying( )
        {
            libspotify.sp_session_callbacks calls = new libspotify.sp_session_callbacks ( );

            string np = "";

            IntPtr npd = libspotify.sp_track_name ( calls.metadata_updated );

            return np;
        }
Exemplo n.º 3
0
        private IntPtr AddCallbacks()
        {
            connectionErrorDelegate = ConnectionError;
            endOfTrackDelegate = EndOfTrack;
            getAudioBufferStatsDelegate = GetAudioBufferStats;
            logMessageDelegate = LogMessage;
            loggedInDelegate = LoggedIn;
            loggedOutDelegate = LoggedOut;
            messageToUserDelegate = MessageToUser;
            metadataUpdatedDelegate = MetadataUpdated;
            musicDeliveryDelegate = MusicDelivery;
            notifyMainThreadDelegate = NotifyMainThread;
            offlineStatusUpdatedDelegate = OfflineStatusUpdated;
            playTokenLostDelegate = PlayTokenLost;
            startPlaybackDelegate = StartPlayback;
            stopPlaybackDelegate = StopPlayback;
            streamingErrorDelegate = StreamingError;
            userinfoUpdatedDelegate = UserinfoUpdated;

            var callbacks = new libspotify.sp_session_callbacks
                            {
                                connection_error = connectionErrorDelegate.GetFunctionPtr(),
                                end_of_track = endOfTrackDelegate.GetFunctionPtr(),
                                get_audio_buffer_stats = getAudioBufferStatsDelegate.GetFunctionPtr(),
                                log_message = logMessageDelegate.GetFunctionPtr(),
                                logged_in = loggedInDelegate.GetFunctionPtr(),
                                logged_out = loggedOutDelegate.GetFunctionPtr(),
                                message_to_user = messageToUserDelegate.GetFunctionPtr(),
                                metadata_updated = metadataUpdatedDelegate.GetFunctionPtr(),
                                music_delivery = musicDeliveryDelegate.GetFunctionPtr(),
                                notify_main_thread = notifyMainThreadDelegate.GetFunctionPtr(),
                                offline_status_updated = offlineStatusUpdatedDelegate.GetFunctionPtr(),
                                play_token_lost = playTokenLostDelegate.GetFunctionPtr(),
                                start_playback = startPlaybackDelegate.GetFunctionPtr(),
                                stop_playback = stopPlaybackDelegate.GetFunctionPtr(),
                                streaming_error = streamingErrorDelegate.GetFunctionPtr(),
                                userinfo_updated = userinfoUpdatedDelegate.GetFunctionPtr()
                            };

            var callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf((object) callbacks));
            Marshal.StructureToPtr(callbacks, callbacksPtr, true);

            return callbacksPtr;
        }
Exemplo n.º 4
0
        private static libspotify.sp_error initSession()
        {
            lock (_lock)
            {
                waveOut = new WaveOut();

                libspotify.sp_session_callbacks callbacks = new libspotify.sp_session_callbacks();
                callbacks.connection_error = Marshal.GetFunctionPointerForDelegate(fn_connection_error_delegate);
                callbacks.end_of_track = Marshal.GetFunctionPointerForDelegate(fn_end_of_track_delegate);
                callbacks.get_audio_buffer_stats =
                    Marshal.GetFunctionPointerForDelegate(fn_get_audio_buffer_stats_delegate);
                callbacks.log_message = Marshal.GetFunctionPointerForDelegate(fn_log_message);
                callbacks.logged_in = Marshal.GetFunctionPointerForDelegate(fn_logged_in_delegate);
                callbacks.logged_out = Marshal.GetFunctionPointerForDelegate(fn_logged_out_delegate);
                callbacks.message_to_user = Marshal.GetFunctionPointerForDelegate(fn_message_to_user_delegate);
                callbacks.metadata_updated = Marshal.GetFunctionPointerForDelegate(fn_metadata_updated_delegate);
                callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(fn_music_delivery_delegate);
                callbacks.notify_main_thread = Marshal.GetFunctionPointerForDelegate(fn_notify_main_thread_delegate);
                callbacks.offline_status_updated =
                    Marshal.GetFunctionPointerForDelegate(fn_offline_status_updated_delegate);
                callbacks.play_token_lost = Marshal.GetFunctionPointerForDelegate(fn_play_token_lost_delegate);
                callbacks.start_playback = Marshal.GetFunctionPointerForDelegate(fn_start_playback);
                callbacks.stop_playback = Marshal.GetFunctionPointerForDelegate(fn_stop_playback);
                callbacks.streaming_error = Marshal.GetFunctionPointerForDelegate(fn_streaming_error_delegate);
                callbacks.userinfo_updated = Marshal.GetFunctionPointerForDelegate(fn_userinfo_updated_delegate);

                IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
                Marshal.StructureToPtr(callbacks, callbacksPtr, true);

                libspotify.sp_session_config config = new libspotify.sp_session_config();
                config.api_version = libspotify.SPOTIFY_API_VERSION;
                config.user_agent = "SoundBounce";
                config.application_key_size = appkey.Length;
                config.application_key = Marshal.AllocHGlobal(appkey.Length);
                config.cache_location = Path.Combine(Path.GetTempPath(), "SoundBounce_temp");
                config.settings_location = Path.Combine(Path.GetTempPath(), "SoundBounce_temp");
                config.callbacks = callbacksPtr;
                config.compress_playlists = true;
                config.dont_save_metadata_for_playlists = false;
                config.initially_unload_playlists = false;

                Spotify.Log.DebugFormat("api_version={0}", config.api_version);
                Spotify.Log.DebugFormat("api_version={0}", config.api_version);
                Spotify.Log.DebugFormat("application_key_size={0}", config.application_key_size);
                Spotify.Log.DebugFormat("cache_location={0}", config.cache_location);
                Spotify.Log.DebugFormat("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)
                {
                    _sessionPtr = sessionPtr;
                    libspotify.sp_session_set_connection_type(sessionPtr,
                                                              libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED);
                }

                // set high bitrate

                if (ConfigurationManager.AppSettings["HighBitrate"] == "true")
                {
                    libspotify.sp_error test = libspotify.sp_session_preferred_bitrate(sessionPtr,
                                                                                       libspotify.sp_bitrate
                                                                                                 .BITRATE_320k);
                    if (test != libspotify.sp_error.OK)
                        Spotify.Log.WarnFormat("sp_session_preferred_bitrate() to 320k failed: {0}", test);
                    else
                        Spotify.Log.Debug("sp_session_preferred_bitrate() to 320k succeeded!");
                }

                // normalize volume
                if (ConfigurationManager.AppSettings["NormalizeVolume"] == "true")
                {
                    libspotify.sp_session_set_volume_normalization(sessionPtr, true);
                }

                return err;
            }
        }
Exemplo n.º 5
0
        private static libspotify.sp_error initSession()
        {
            libspotify.sp_session_callbacks callbacks = new libspotify.sp_session_callbacks();
            callbacks.connection_error = Marshal.GetFunctionPointerForDelegate(fn_connection_error_delegate);
            callbacks.end_of_track = Marshal.GetFunctionPointerForDelegate(fn_end_of_track_delegate);
            callbacks.get_audio_buffer_stats = Marshal.GetFunctionPointerForDelegate(fn_get_audio_buffer_stats_delegate);
            callbacks.log_message = Marshal.GetFunctionPointerForDelegate(fn_log_message);
            callbacks.logged_in = Marshal.GetFunctionPointerForDelegate(fn_logged_in_delegate);
            callbacks.logged_out = Marshal.GetFunctionPointerForDelegate(fn_logged_out_delegate);
            callbacks.message_to_user = Marshal.GetFunctionPointerForDelegate(fn_message_to_user_delegate);
            callbacks.metadata_updated = Marshal.GetFunctionPointerForDelegate(fn_metadata_updated_delegate);
            callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(fn_music_delivery_delegate);
            callbacks.notify_main_thread = Marshal.GetFunctionPointerForDelegate(fn_notify_main_thread_delegate);
            callbacks.offline_status_updated = Marshal.GetFunctionPointerForDelegate(fn_offline_status_updated_delegate);
            callbacks.play_token_lost = Marshal.GetFunctionPointerForDelegate(fn_play_token_lost_delegate);
            callbacks.start_playback = Marshal.GetFunctionPointerForDelegate(fn_start_playback);
            callbacks.stop_playback = Marshal.GetFunctionPointerForDelegate(fn_stop_playback);
            callbacks.streaming_error = Marshal.GetFunctionPointerForDelegate(fn_streaming_error_delegate);
            callbacks.userinfo_updated = Marshal.GetFunctionPointerForDelegate(fn_userinfo_updated_delegate);

            IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
            Marshal.StructureToPtr(callbacks, callbacksPtr, true);

            libspotify.sp_session_config config = new libspotify.sp_session_config();
            config.api_version = libspotify.SPOTIFY_API_VERSION;
            config.user_agent = "spewtify";
            config.application_key_size = appkey.Length;
            config.application_key = Marshal.AllocHGlobal(appkey.Length);

            try
            {
                new DirectoryInfo(thedir).Create();
            }
            catch { }
            config.cache_location = thedir;
            config.settings_location = thedir;
            config.callbacks = callbacksPtr;
            config.compress_playlists = true;
            config.dont_save_metadata_for_playlists = false;
            config.initially_unload_playlists = false;

            Log.Debug( "api_version={0}", config.api_version);
            Log.Debug( "application_key_size={0}", config.application_key_size);
            Log.Debug( "cache_location={0}", config.cache_location);
            Log.Debug( "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) {

                _sessionPtr = sessionPtr;
                libspotify.sp_session_set_connection_type(sessionPtr, libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED);

            }

            return err;
        }
Exemplo n.º 6
0
        private libspotify.sp_error initSession()
        {
            fn_connection_error_delegate = new connection_error_delegate(connection_error);
            fn_end_of_track_delegate = new end_of_track_delegate(end_of_track);
            fn_get_audio_buffer_stats_delegate = new get_audio_buffer_stats_delegate(get_audio_buffer_stats);
            fn_log_message = new log_message_delegate(log_message);
            fn_logged_in_delegate = new logged_in_delegate(logged_in);
            fn_logged_out_delegate = new logged_out_delegate(logged_out);
            fn_message_to_user_delegate = new message_to_user_delegate(message_to_user);
            fn_metadata_updated_delegate = new metadata_updated_delegate(metadata_updated);
            fn_music_delivery_delegate = new music_delivery_delegate(music_delivery);
            fn_notify_main_thread_delegate = new notify_main_thread_delegate(notify_main_thread);
            fn_offline_status_updated_delegate = new offline_status_updated_delegate(offline_status_updated);
            fn_play_token_lost_delegate = new play_token_lost_delegate(play_token_lost);
            fn_start_playback = new start_playback_delegate(start_playback);
            fn_stop_playback = new stop_playback_delegate(stop_playback);
            fn_streaming_error_delegate = new streaming_error_delegate(streaming_error);
            fn_userinfo_updated_delegate = new userinfo_updated_delegate(userinfo_updated);

            libspotify.sp_session_callbacks callbacks = new libspotify.sp_session_callbacks();
            callbacks.connection_error = Marshal.GetFunctionPointerForDelegate(fn_connection_error_delegate);
            callbacks.end_of_track = Marshal.GetFunctionPointerForDelegate(fn_end_of_track_delegate);
            callbacks.get_audio_buffer_stats = Marshal.GetFunctionPointerForDelegate(fn_get_audio_buffer_stats_delegate);
            callbacks.log_message = Marshal.GetFunctionPointerForDelegate(fn_log_message);
            callbacks.logged_in = Marshal.GetFunctionPointerForDelegate(fn_logged_in_delegate);
            callbacks.logged_out = Marshal.GetFunctionPointerForDelegate(fn_logged_out_delegate);
            callbacks.message_to_user = Marshal.GetFunctionPointerForDelegate(fn_message_to_user_delegate);
            callbacks.metadata_updated = Marshal.GetFunctionPointerForDelegate(fn_metadata_updated_delegate);
            callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(fn_music_delivery_delegate);
            callbacks.notify_main_thread = Marshal.GetFunctionPointerForDelegate(fn_notify_main_thread_delegate);
            callbacks.offline_status_updated = Marshal.GetFunctionPointerForDelegate(fn_offline_status_updated_delegate);
            callbacks.play_token_lost = Marshal.GetFunctionPointerForDelegate(fn_play_token_lost_delegate);
            callbacks.start_playback = Marshal.GetFunctionPointerForDelegate(fn_start_playback);
            callbacks.stop_playback = Marshal.GetFunctionPointerForDelegate(fn_stop_playback);
            callbacks.streaming_error = Marshal.GetFunctionPointerForDelegate(fn_streaming_error_delegate);
            callbacks.userinfo_updated = Marshal.GetFunctionPointerForDelegate(fn_userinfo_updated_delegate);

            IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
            Marshal.StructureToPtr(callbacks, callbacksPtr, true);

            libspotify.sp_session_config config = new libspotify.sp_session_config();
            config.api_version = libspotify.SPOTIFY_API_VERSION;
            config.user_agent = "Jamcast";
            config.application_key_size = appkey.Length;
            config.application_key = Marshal.AllocHGlobal(appkey.Length);
            config.cache_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp");
            config.settings_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp");
            config.callbacks = callbacksPtr;
            config.compress_playlists = true;
            config.dont_save_metadata_for_playlists = false;
            config.initially_unload_playlists = false;

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

                _sessionPtr = sessionPtr;
                libspotify.sp_session_set_connection_type(sessionPtr, libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED);

            }

            return err;
        }
Exemplo n.º 7
0
        private static libspotify.sp_error initSession(string applicationID)
        {
            libspotify.sp_session_callbacks callbacks = new libspotify.sp_session_callbacks();
            callbacks.connection_error = Marshal.GetFunctionPointerForDelegate(fn_connection_error_delegate);
            callbacks.end_of_track = Marshal.GetFunctionPointerForDelegate(fn_end_of_track_delegate);
            callbacks.get_audio_buffer_stats = Marshal.GetFunctionPointerForDelegate(fn_get_audio_buffer_stats_delegate);
            callbacks.log_message = Marshal.GetFunctionPointerForDelegate(fn_log_message);
            callbacks.logged_in = Marshal.GetFunctionPointerForDelegate(fn_logged_in_delegate);
            callbacks.logged_out = Marshal.GetFunctionPointerForDelegate(fn_logged_out_delegate);
            callbacks.message_to_user = Marshal.GetFunctionPointerForDelegate(fn_message_to_user_delegate);
            callbacks.metadata_updated = Marshal.GetFunctionPointerForDelegate(fn_metadata_updated_delegate);
            callbacks.music_delivery = Marshal.GetFunctionPointerForDelegate(fn_music_delivery_delegate);
            callbacks.notify_main_thread = Marshal.GetFunctionPointerForDelegate(fn_notify_main_thread_delegate);
            callbacks.offline_status_updated = Marshal.GetFunctionPointerForDelegate(fn_offline_status_updated_delegate);
            callbacks.play_token_lost = Marshal.GetFunctionPointerForDelegate(fn_play_token_lost_delegate);
            callbacks.start_playback = Marshal.GetFunctionPointerForDelegate(fn_start_playback);
            callbacks.stop_playback = Marshal.GetFunctionPointerForDelegate(fn_stop_playback);
            callbacks.streaming_error = Marshal.GetFunctionPointerForDelegate(fn_streaming_error_delegate);
            callbacks.userinfo_updated = Marshal.GetFunctionPointerForDelegate(fn_userinfo_updated_delegate);

            IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
            Marshal.StructureToPtr(callbacks, callbacksPtr, true);

            libspotify.sp_session_config config = new libspotify.sp_session_config();
            config.api_version = libspotify.SPOTIFY_API_VERSION;
            config.user_agent = "Jamcast";
            config.application_key_size = appkey.Length;
            config.application_key = Marshal.AllocHGlobal(appkey.Length);
            config.cache_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp\\" + applicationID);
            config.settings_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp\\" + applicationID);
            config.callbacks = callbacksPtr;
            config.compress_playlists = true;
            config.dont_save_metadata_for_playlists = false;
            config.initially_unload_playlists = false;

            Log.Debug(Plugin.LOG_MODULE, "api_version={0}", config.api_version);
            Log.Debug(Plugin.LOG_MODULE, "application_key_size={0}", config.application_key_size);
            Log.Debug(Plugin.LOG_MODULE, "cache_location={0}", config.cache_location);
            Log.Debug(Plugin.LOG_MODULE, "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)
            {
                _sessionPtr = sessionPtr;
                libspotify.sp_session_set_connection_type(sessionPtr, libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED);
            }

            libspotify.sp_error test = libspotify.sp_session_preferred_bitrate(sessionPtr, libspotify.sp_bitrate.BITRATE_320k);
            if (test != libspotify.sp_error.OK)
                Log.Warning(Plugin.LOG_MODULE, "sp_session_preferred_bitrate() failed: {0}", test);
            else
                Log.Debug(Plugin.LOG_MODULE, "sp_session_preferred_bitrate() succeeded!");

            return err;
        }