/// <summary> /// Set track to play (immediately). /// </summary> /// <param name="playlistId"> /// ID of playlsit in which the requested track is located (0 for no playlist). /// </param> /// <param name="trackId"> /// ID of track in database. /// </param> /// <returns> /// Returns true if track is available and is plaing now. /// </returns> public static byte PlayTrack(int playlistId, long trackId) { if (trackId < 1) { return(0); } DatabaseTrackInfo track = new DatabaseTrackModelProvider <DatabaseTrackInfo>( ServiceManager.DbConnection).FetchSingle(trackId); if (track == null) { return(0); } Source source = Helper.GetPlaylistSource(playlistId); Source requestedSource = source; TrackListModel model = null; if (source == null) { source = MusicLibrary; } model = ((ITrackModelSource)source).TrackModel; if (model != null) { int i = 0; for (i = 0; i < model.Count; i++) { TrackInfo t = (TrackInfo)model.GetItem(i); if (t is DatabaseTrackInfo && ((DatabaseTrackInfo)t).TrackId == track.TrackId) { break; } } if (i != model.Count) { ServiceManager.PlaybackController.Source = (ITrackModelSource)source; ServiceManager.PlayerEngine.OpenPlay((TrackInfo)model.GetItem(i)); _playTimeout = Timestamp(); return((byte)(source == requestedSource ? 2 : 1)); } } ServiceManager.PlayerEngine.OpenPlay(track); _playTimeout = Timestamp(); return(1); }
/// <summary> /// Add track to playlist. /// </summary> /// <param name="playlistId"> /// ID of playlist (1 and 2 supported only). /// </param> /// <param name="trackId"> /// Track ID of track to add. /// </param> /// <param name="allowTwice"> /// True if track should be added although it's already in available in the playlist. /// </param> /// <returns> /// True if track successfully added to playlist. /// </returns> public static bool AddTrackToPlayList(int playlistId, int trackId, bool allowTwice) { if (!allowTwice) { TrackListModel m = (playlistId == 1 ? RemotePlaylist : PlayQueuePlaylist).TrackModel; for (int i = 0; i < m.Count; i++) { object t = m.GetItem(i); if (t is DatabaseTrackInfo && ((DatabaseTrackInfo)t).TrackId == trackId) { return(false); } } } switch (playlistId) { case 1: { Selection selection = null; MusicLibrarySource source = MusicLibrary; for (int i = 0; i < source.TrackModel.Count; i++) { object t = source.TrackModel.GetItem(i); if (t is DatabaseTrackInfo && ((DatabaseTrackInfo)t).TrackId == trackId) { selection = new Hyena.Collections.Selection(); selection.Select(i); break; } } if (selection != null) { RemotePlaylist.AddSelectedTracks(source, selection); return(true); } break; } case 2: { DatabaseTrackInfo track = new DatabaseTrackModelProvider <DatabaseTrackInfo>( ServiceManager.DbConnection).FetchSingle(trackId); if (track != null) { PlayQueuePlaylist.EnqueueTrack(track, false); return(true); } break; } } return(false); }
public static int AddArtistOrAlbumToPlayList(int playlistId, int id, bool allowTwice, bool isAlbum) { if (playlistId != 1 && playlistId != 2) { return(0); } int count = 0; MusicLibrarySource lib = MusicLibrary; DatabaseSource dest = playlistId == 1 ? RemotePlaylist : PlayQueuePlaylist; TrackListModel model = lib.TrackModel; Selection selection = new Selection(); if (allowTwice) { for (int i = 0; i < model.Count; i++) { object t = model.GetItem(i); if (t is DatabaseTrackInfo && (isAlbum && ((DatabaseTrackInfo)t).AlbumId == id || !isAlbum && ((DatabaseTrackInfo)t).ArtistId == id)) { selection.Select(i); count++; } } } else { List <SortEntry> ids = new List <SortEntry>(); List <int> doubleIds = new List <int>(); for (int i = 0; i < model.Count; i++) { object t = model.GetItem(i); if (t is DatabaseTrackInfo && (isAlbum && ((DatabaseTrackInfo)t).AlbumId == id || !isAlbum && ((DatabaseTrackInfo)t).ArtistId == id)) { SortEntry e = new SortEntry(); e.pos = i; e.selected = true; e.id = (t as DatabaseTrackInfo).TrackId; ids.Add(e); } } ids.Sort(); dest.Reload(); model = dest.TrackModel; for (int i = 0; i < model.Count; i++) { object t = model.GetItem(i); if (t is DatabaseTrackInfo && (isAlbum && ((DatabaseTrackInfo)t).AlbumId == id || !isAlbum && ((DatabaseTrackInfo)t).ArtistId == id)) { doubleIds.Add((t as DatabaseTrackInfo).TrackId); } } doubleIds.Sort(); int si = 0, di = 0; while (si < ids.Count && di < doubleIds.Count) { if (ids[si].id == doubleIds[di]) { ids[si].selected = false; si++; } else if (ids[si].id < doubleIds[di]) { si++; } else { di++; } } for (int i = 0; i < ids.Count; i++) { if (ids[i].selected) { selection.Select(ids[i].pos); count++; } } } if (count != 0) { if (dest != PlayQueuePlaylist) { dest.AddSelectedTracks(lib, selection); } else { foreach (int s in selection) { ((PlayQueueSource)dest).EnqueueTrack(lib.TrackModel.GetItem(s) as TrackInfo, false); } } } return(count); }
public static byte [] Playlist(int readBytes) { byte request = 0; if (readBytes > 0) { request = Helper.Buffer[0]; } switch (request) { case 1: { // requested playlist names Source remotePlaylist = Helper.RemotePlaylist; ((DatabaseSource)remotePlaylist).Reload(); Source musicLibrary = ServiceManager.SourceManager.MusicLibrary; ushort count = 0; bool remotePlaylistAdded = false; int index = 4; Helper.Buffer[0] = Helper.Buffer[1] = 0; // - we're going to fill the buffer with all playlists which are not empty (expect remote playlist) // - we'll use the request buffer because it's big enough and we don't know the final size // - the remote playlist won't be listed if it's fresh created, so we do that manually // we just fit it into position where the source enumaration would contain it List <DatabaseSource> sources = new List <DatabaseSource>(); // we have to catch the sources first because we refresh the while iterating and this crashes on // in older banshee versions foreach (Source s in ServiceManager.SourceManager.Sources) { if (s is DatabaseSource && s != remotePlaylist && s.Parent == musicLibrary || s == musicLibrary || s is PlayQueueSource) { sources.Add(s as DatabaseSource); } } foreach (Source s in sources) { DatabaseSource so = s as DatabaseSource; Helper.ClearSourceFilters(so); if (so.TrackModel.Count > 0 || s == Helper.PlayQueuePlaylist) { if (!remotePlaylistAdded && String.Compare(s.Name, remotePlaylist.Name) >= 0) { count++; index = Helper.SourceAsPlaylistToBuffer(index, remotePlaylist); remotePlaylistAdded = true; } count++; index = Helper.SourceAsPlaylistToBuffer(index, s); } } // remote playlist would be at the end so it's still not added to return - do it if (!remotePlaylistAdded) { count++; index = Helper.SourceAsPlaylistToBuffer(index, remotePlaylist); } // write playlist count to buffer Array.Copy(Helper.ShortToByte(count), 0, Helper.Buffer, 2, 2); // get needed bytes from buffer so we can return it byte [] result = new byte [index]; Array.Copy(Helper.Buffer, 0, result, 0, index); return(result); } case 2: { // requested tracks from playlist ushort playlistId = 0; int maxReturn = 0; uint startPosition = 0; // get parameters if given if (readBytes > 2) { playlistId = Helper.ShortFromBuffer(1); } if (readBytes > 6) { maxReturn = (int)Helper.IntFromBuffer(3); } if (readBytes > 10) { startPosition = Helper.IntFromBuffer(7) & 0xffffffff; } // search for the playlist which was requested Source source = Helper.GetPlaylistSource(playlistId); TrackListModel model = null; if (source != null) { model = ((ITrackModelSource)source).TrackModel; } if (model != null) { // playlist exists, return the requested amount of track IDs int count = model.Count; int toCount = count; // request wants a return from the current track position // let's see if this model contains the currently played track if ((startPosition & 0x80000000) != 0) { int pos = model.IndexOf(ServiceManager.PlayerEngine.CurrentTrack); startPosition = startPosition & 0x7fffffff; if (pos < 0 || startPosition > 100) { startPosition = 0; } else { int newStart = (int)(pos - startPosition); startPosition = newStart < 0 ? 0 : (uint)newStart; } } int returned = count - (int)startPosition; // correct te amount of returned tracks if (returned < 0) { returned = 0; } else if (maxReturn != 0 && returned > maxReturn) { returned = maxReturn; toCount = (int)startPosition + maxReturn; } // get requested tracks and write it in the result buffer byte [] result = new byte [12 + 4 * returned]; Array.Copy(Helper.IntToByte((uint)count), 0, result, 0, 4); Array.Copy(Helper.IntToByte((uint)returned), 0, result, 4, 4); Array.Copy(Helper.IntToByte((uint)startPosition), 0, result, 8, 4); byte [] zeroId = new byte [] { 0, 0, 0, 0 }; for (int i = (int)startPosition; i < toCount; i++) { TrackInfo track = (TrackInfo)model.GetItem(i); Array.Copy(track is DatabaseTrackInfo ? Helper.IntToByte((uint)((DatabaseTrackInfo)track).TrackId) : zeroId, 0, result, (i - startPosition) * 4 + 12, 4); } return(result); } else { // count = returned = startPosition = 0 return(new byte [] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); } } case 3: { // requested to play tracks int playlistId = 0; long trackId = 0; // get parameters if given if (readBytes > 6) { playlistId = Helper.ShortFromBuffer(1); trackId = Helper.IntFromBuffer(3); } return(new byte [] { Helper.PlayTrack(playlistId, trackId) }); } case 4: if (readBytes > 7 && Helper.AddTrackToPlayList( Helper.ShortFromBuffer(2), (int)Helper.IntFromBuffer(4), (Helper.Buffer[1] & 0x1) != 0)) { return(Helper.ShortToByte(1)); } return(Helper.ShortToByte(0)); case 5: case 6: if (readBytes > 7) { return(Helper.ShortToByte((ushort)Helper.AddArtistOrAlbumToPlayList( Helper.ShortFromBuffer(2), (int)Helper.IntFromBuffer(4), (Helper.Buffer[1] & 0x1) != 0, request == 6))); } return(Helper.ShortToByte(0)); case 7: if (readBytes > 6 && Helper.RemoveTrackFromPlaylist( Helper.ShortFromBuffer(1), (int)Helper.IntFromBuffer(3))) { return(Helper.ShortToByte(1)); } return(Helper.ShortToByte(0)); case 8: case 9: if (readBytes > 6) { return(Helper.ShortToByte((ushort)Helper.RemoveArtistOrAlbumFromPlaylist( Helper.ShortFromBuffer(1), (int)Helper.IntFromBuffer(3), request == 9))); } return(Helper.ShortToByte(0)); } return(new byte [] { 0 }); }