Example #1
0
        internal protected Track(Session session, IntPtr trackPtr)
        {
            if (trackPtr == IntPtr.Zero)
            {
                throw new ArgumentException("trackPtr can't be zero.");
            }

            if (session == null)
            {
                throw new ArgumentNullException("Session can't be null.");
            }
            this.session  = session;
            this.trackPtr = trackPtr;
            lock (libspotify.Mutex)
                libspotify.sp_track_add_ref(trackPtr);

            this.artists = new DelegateArray <IArtist>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_track_num_artists(trackPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Artist.Get(session, libspotify.sp_track_artist(trackPtr, index)));
            });

            session.DisposeAll += new SessionEventHandler(session_DisposeAll);
        }
Example #2
0
        private Search(Session session, IntPtr searchPtr)
        {
            if (searchPtr == IntPtr.Zero)
            {
                throw new ArgumentException("searchPtr can't be zero");
            }

            if (session == null)
            {
                throw new ArgumentNullException("Session can't be null");
            }
            this.session = session;

            this.searchPtr = searchPtr;

            this.tracks = new DelegateArray <ITrack>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_tracks(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Track.Get(session, libspotify.sp_search_track(searchPtr, index)));
            });

            this.albums = new DelegateArray <IAlbum>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_albums(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Album.Get(session, libspotify.sp_search_album(searchPtr, index)));
            });

            this.artists = new DelegateArray <IArtist>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_artists(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Artist.Get(session, libspotify.sp_search_artist(searchPtr, index)));
            });

            _Complete          += new search_complete_cb(Search__Complete);
            session.DisposeAll += new SessionEventHandler(session_DisposeAll);
        }
        private Search(Session session, IntPtr searchPtr)
        {
            if (searchPtr == IntPtr.Zero)
            {
                throw new ArgumentException("searchPtr can't be zero");
            }

            if (session == null)
            {
                throw new ArgumentNullException("Session can't be null");
            }
            this.session = session;

            this.searchPtr = searchPtr;

            this.tracks = new DelegateArray <ITrack>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_tracks(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Track.Get(session, libspotify.sp_search_track(searchPtr, index)));
            });

            this.albums = new DelegateArray <IAlbum>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_albums(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Album.Get(session, libspotify.sp_search_album(searchPtr, index)));
            });

            this.artists = new DelegateArray <IArtist>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_artists(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(Artist.Get(session, libspotify.sp_search_artist(searchPtr, index)));
            });

            this.playlists = new DelegateArray <IPlaylist>(() =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_search_num_playlists(searchPtr));
            }, (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                {
                    string playlistUri = libspotify.sp_search_playlist_uri(searchPtr, index);
                    var playlistLink   = session.ParseLink(playlistUri);
                    if (playlistLink.Type == sp_linktype.SP_LINKTYPE_PLAYLIST)
                    {
                        var playlist = playlistLink.As <IPlaylist>();
                        playlistLink.Dispose();
                        return(playlist);
                    }
                    else
                    {
                        throw new Exception("Wrong link type: " + playlistLink.Type.ToString());
                    }
                }
            });

            _Complete          += new search_complete_cb(Search__Complete);
            session.DisposeAll += new SessionEventHandler(session_DisposeAll);
        }
        public IEnumerable <ISpotifyObject> GetTopList(ToplistType listType, int maxNumber)
        {
            int currentRegion = -1;

            lock (libspotify.Mutex)
            {
                currentRegion = libspotify.sp_session_user_country(this.sessionPtr);
            }
            IntPtr           topListBrowserPtr = IntPtr.Zero;
            ManualResetEvent reset             = new ManualResetEvent(false);
            Exception        createException   = null;

            ThreadPool.QueueUserWorkItem(delegate(object obj)
            {
                try
                {
                    lock (libspotify.Mutex)
                    {
                        topListBrowserPtr = libspotify.sp_toplistbrowse_create(this.sessionPtr, listType, currentRegion, null, GetPopularCallback, IntPtr.Zero);
                    }
                }
                catch (Exception ex)
                {
                    createException = ex;
                }
                finally
                {
                    reset.Set();
                }
            });
            reset.WaitOne();

            if (createException != null)
            {
                throw createException;
            }

            if (topListBrowserPtr == IntPtr.Zero)
            {
                throw new Exception("Top list browser pointer is zero");
            }

            lock (popularLock)
            {
                if (!libspotify.sp_toplistbrowse_is_loaded(topListBrowserPtr))
                {
                    Monitor.Wait(popularLock);
                }
            }

            if (libspotify.sp_toplistbrowse_is_loaded(topListBrowserPtr))
            {
                lock (libspotify.Mutex)
                {
                    List <ISpotifyObject> objects = new List <ISpotifyObject>();
                    switch (listType)
                    {
                    case ToplistType.Tracks:
                    {
                        int maxTracks = libspotify.sp_toplistbrowse_num_tracks(topListBrowserPtr);
                        if (maxTracks > maxNumber)
                        {
                            maxTracks = maxNumber;
                        }
                        for (int i = 0; i < maxTracks; i++)
                        {
                            var trackPtr = libspotify.sp_toplistbrowse_track(topListBrowserPtr, i);
                            var track    = Track.Get(this, trackPtr);
                            objects.Add(track);
                        }
                    }
                    break;

                    case ToplistType.Artists:
                    {
                        int maxArtists = libspotify.sp_toplistbrowse_num_artists(topListBrowserPtr);
                        if (maxArtists > maxNumber)
                        {
                            maxArtists = maxNumber;
                        }
                        for (int i = 0; i < maxArtists; i++)
                        {
                            var artistPtr = libspotify.sp_toplistbrowse_artist(topListBrowserPtr, i);
                            var artist    = Artist.Get(this, artistPtr);
                            objects.Add(artist);
                        }
                    }
                    break;

                    case ToplistType.Albums:
                    {
                        int maxAlbums = libspotify.sp_toplistbrowse_num_albums(topListBrowserPtr);
                        if (maxAlbums > maxNumber)
                        {
                            maxAlbums = maxNumber;
                        }
                        for (int i = 0; i < maxAlbums; i++)
                        {
                            var albumPtr = libspotify.sp_toplistbrowse_album(topListBrowserPtr, i);
                            var album    = Album.Get(this, albumPtr);
                            objects.Add(album);
                        }
                    }
                    break;

                    default:
                        throw new Exception("Unrecognized list type");
                    }
                    libspotify.sp_toplistbrowse_release(topListBrowserPtr);
                    return(objects);
                }
            }
            else
            {
                var error = libspotify.sp_toplistbrowse_error(topListBrowserPtr);
                libspotify.sp_toplistbrowse_release(topListBrowserPtr);
                throw new Exception("Error retrieving top list: " + error.ToString());
            }
        }