/// <summary>
    /// Add or enque a song to the current playlist - call OnSongInserted() after this!!!
    /// </summary>
    /// <param name="song">the song to add</param>
    /// <returns>if the action was successful</returns>
    private bool AddSongToPlaylist(ref Song song)
    {
      PlayList playlist;
      if (PlaylistPlayer.CurrentPlaylistType == PlayListType.PLAYLIST_MUSIC_TEMP)
      {
        playlist = PlaylistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC_TEMP);
      }
      else
      {
        playlist = PlaylistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC);
      }
      
      if (playlist == null)
      {
        return false;
      }

      //add to playlist
      PlayListItem playlistItem = new PlayListItem();
      playlistItem.Type = PlayListItem.PlayListItemType.Audio;
      StringBuilder sb = new StringBuilder();

      playlistItem.FileName = song.FileName;
      sb.Append(song.Track);
      sb.Append(". ");
      sb.Append(song.Artist);
      sb.Append(" - ");
      sb.Append(song.Title);
      playlistItem.Description = sb.ToString();
      playlistItem.Duration = song.Duration;

      playlistItem.MusicTag = song.ToMusicTag();
      playlist.Add(playlistItem);

      OnSongInserted();
      return true;
    }
    protected void LoadPlayList(string strPlayList, bool startPlayback, bool isAsynch, bool defaultLoad)
    {
      IPlayListIO loader = PlayListFactory.CreateIO(strPlayList);
      if (loader == null)
      {
        return;
      }

      PlayList playlist = new PlayList();

      if (!Util.Utils.FileExistsInCache(strPlayList))
      {
        Log.Info("Playlist: Skipping non-existing Playlist file: {0}", strPlayList);
        return;
      }

      if (!loader.Load(playlist, strPlayList))
      {
        if (isAsynch && defaultLoad) // we might not be in GUI yet! we have asynch and default load because we might want to use asynch loading from gui button too, later!
          throw new Exception(string.Format("Unable to load Playlist file: {0}", strPlayList)); // exception is handled in backgroundworker
        else
          TellUserSomethingWentWrong();
        return;
      }

      if (_autoShuffleOnLoad)
      {
        playlist.Shuffle();
      }

      playlistPlayer.CurrentPlaylistName = Path.GetFileNameWithoutExtension(strPlayList);
      if (playlist.Count == 1 && startPlayback)
      {
        Log.Info("GUIMusic:Play: play single playlist item - {0}", playlist[0].FileName);
        // Default to type Music, when a playlist has been selected from My Music
        g_Player.Play(playlist[0].FileName, g_Player.MediaType.Music);
        return;
      }

      if (null != bw && isAsynch && bw.CancellationPending)
        return;

      // clear current playlist
      //playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC).Clear();

      Song song = new Song();
      PlayList newPlaylist = new PlayList();

      // add each item of the playlist to the playlistplayer
      for (int i = 0; i < playlist.Count; ++i)
      {
        if (null != bw && isAsynch && bw.CancellationPending)
          return;

        PlayListItem playListItem = playlist[i];
        m_database.GetSongByFileName(playListItem.FileName, ref song);
        MusicTag tag = new MusicTag();
        tag = song.ToMusicTag();
        playListItem.MusicTag = tag;
        if (Util.Utils.FileExistsInCache(playListItem.FileName) ||
            playListItem.Type == PlayListItem.PlayListItemType.AudioStream)
        {
          newPlaylist.Add(playListItem);
        }
        else
        {
          Log.Info("Playlist: File {0} no longer exists. Skipping item.", playListItem.FileName);
        }
      }

      if (null != bw && isAsynch && bw.CancellationPending)
        return;

      ReplacePlaylist(newPlaylist);

      if (startPlayback)
        StartPlayingPlaylist();
    }
    private bool AddRandomSongToPlaylist(ref Song song)
    {
      //check duplication
      PlayList playlist = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC);
      for (int i = 0; i < playlist.Count; i++)
      {
        PlayListItem item = playlist[i];
        if (item.FileName == song.FileName)
        {
          return false;
        }
      }

      //add to playlist
      PlayListItem playlistItem = new PlayListItem();
      playlistItem.Type = PlayListItem.PlayListItemType.Audio;
      StringBuilder sb = new StringBuilder();

      playlistItem.FileName = song.FileName;
      sb.Append(song.Track);
      sb.Append(". ");
      sb.Append(song.Artist);
      sb.Append(" - ");
      sb.Append(song.Title);
      playlistItem.Description = sb.ToString();
      playlistItem.Duration = song.Duration;

      MusicTag tag = new MusicTag();
      tag = song.ToMusicTag();

      playlistItem.MusicTag = tag;

      playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC).Add(playlistItem);
      return true;
    }
        bool AddRandomSongToPlaylist(ref Song song, ref YouTubeFeed vidr)
        {
            //check duplication
            PlayList playlist = playlistPlayer.GetPlaylist(_playlistType);
            for (int i = 0; i < playlist.Count; i++)
            {
                PlayListItem item = playlist[i];
                if (item.FileName == song.FileName)
                    return false;
            }

            //add to playlist
            PlayListItem playlistItem = new PlayListItem();
            playlistItem.Type = PlayListItem.PlayListItemType.Video; //Playlists.PlayListItem.PlayListItemType.Audio;
            StringBuilder sb = new StringBuilder();

            playlistItem.FileName = song.FileName;
            //sb.Append(song.Track);
            //sb.Append(". ");
            sb.Append(song.Artist);
            if (!string.IsNullOrEmpty(song.Title))
            {
                sb.Append(" - ");
                sb.Append(song.Title);
            }
            playlistItem.Description = sb.ToString();
            playlistItem.Duration = song.Duration;

            MusicTag tag = new MusicTag();
            tag = song.ToMusicTag();
            foreach (YouTubeEntry entry in vidr.Entries)
            {
                if (Youtube2MP.PlaybackUrl(entry) == playlistItem.FileName)
                {
                    playlistItem.MusicTag = entry;
                    if (!Youtube2MP._settings.UseYouTubePlayer)
                    {
                        playlistItem.FileName = Youtube2MP.StreamPlaybackUrl(entry, new VideoInfo());
                    }
                }
            }

            playlistPlayer.GetPlaylist(_playlistType).Add(playlistItem);
            return true;
        }
 void OnRetrieveMusicInfo(ref List<GUIListItem> items)
 {
     if (items.Count <= 0)
         return;
     MusicDatabase dbs = MusicDatabase.Instance;
     Song song = new Song();
     foreach (GUIListItem item in items)
     {
         if (item.MusicTag == null)
         {
             if (dbs.GetSongByFileName(item.Path, ref song))
             {
                 MusicTag tag = new MusicTag();
                 tag = song.ToMusicTag();
                 item.MusicTag = tag;
             }
             //else
             //  if (UseID3)
             //  {
             //    item.MusicTag = TagReader.TagReader.ReadTag(item.Path);
             //  }
         }
     }
 }
    /// <summary>
    /// Invoked when a song is Stopped or Changed to next song
    /// If "Resume" is configured and the meta matches the resume settings then the Resume time is stored in the DB
    /// </summary>
    /// <param name="type"></param>
    /// <param name="stoptime"></param>
    /// <param name="filename"></param>
    void OnPlaybackChangedOrStopped(g_Player.MediaType type, int stoptime, string filename)
    {
      if (type != g_Player.MediaType.Music)
      {
        return;
      }

      if (_resumeEnabled)
      {
        Song song = new Song();
        // We might have reached the end of a song, then clear the resumeAt time
        m_database.GetSongByFileName(filename, ref song);
        int endTime = song.Duration - MusicPlayer.BASS.Config.CrossFadeIntervalMs / 1000;
        if (song.Id > -1 && (endTime == stoptime || stoptime < _resumeAfter && song.ResumeAt > 0))
        {
          song.ResumeAt = 0;
          m_database.SetResume(song);
          return;
        }

        if (stoptime > _resumeAfter)
        {
          Log.Info("GUIMusic: Song stopped at {0} seconds with resume support enabled", stoptime);
          if (_resumeSelect.Length > 0)
          {
            MusicTag tag = null;
            // We found a valid song, if the id is > -1
            if (song.Id > -1)
            {
              tag = song.ToMusicTag();
            }
            else
            {
              // read tag from file
              tag = m_database.GetTag(filename);
            }

            string value = "";
            switch (_resumeSelect)
            {
              case "Genre":
                value = tag.Genre;
                break;

              case "Title":
                value = tag.Title;
                break;

              case "Filename":
                value = tag.FileName;
                break;

              case "Album":
                value = tag.Album;
                break;

              case "Artist":
                value = tag.Artist;
                break;

              case "AlbumArtist":
                value = tag.AlbumArtist;
                break;

              case "Composer":
                value = tag.Composer;
                break;

              case "Conductor":
                value = tag.Conductor;
                break;
            }

            if (!value.Contains(_resumeSearch))
            {
              Log.Info("GUIMusic: Tags not matching selection criteria. No resumetime stored.");
              return;
            }
          }

          if (song.Id == -1)
          {
            // No Song found. Let's add it to the database and then retrieve it
            Log.Debug("GUIMusic: Song not found in database. Add to database");
            m_database.AddSong(filename);
            m_database.GetSongByFileName(filename, ref song);
          }

          song.ResumeAt = stoptime;
          m_database.SetResume(song);
        }
      }
    }
Example #7
0
        /// <summary>
        /// Returns a playlistitem from a song
        /// 
        /// Note: this method is available in MediaPortal 1.2 in 
        /// MediaPortal.Music.Database.Song.ToPlayListItem()
        /// 
        /// As it wasn't available in 1.1.3 this was copied to a private method
        /// TODO: Remove this once 1.2 has become stable and 1.1.3 is not needed anymore
        /// </summary>
        /// <param name="song">Song</param>
        /// <returns>Playlistitem from a song</returns>
        internal static PlayListItem ToPlayListItem(Song song)
        {
            PlayListItem pli = new PlayListItem();

            pli.Type = PlayListItem.PlayListItemType.Audio;
            pli.FileName = song.FileName;
            pli.Description = song.Title;
            pli.Duration = song.Duration;
            pli.MusicTag = song.ToMusicTag();

            return pli;
        }
Example #8
0
    private bool AddStreamSongToPlaylist(ref Song song)
    {
      PlayList playlist = PlaylistPlayer.GetPlaylist(PlayListType.PLAYLIST_RADIO_STREAMS);
      if (playlist == null || song == null)
      {
        return false;
      }

      // We only want one item at each time since the links invalidate and therefore 
      // automatic advancement of playlist items does not make any sense.
      playlist.Clear();

      //add to playlist
      PlayListItem playlistItem = new PlayListItem();
      playlistItem.Type = PlayListItem.PlayListItemType.Audio;
      StringBuilder sb = new StringBuilder();

      playlistItem.FileName = song.URL;

      sb.Append(song.Artist);
      sb.Append(" - ");
      sb.Append(song.Title);
      playlistItem.Description = sb.ToString();
      playlistItem.Duration = song.Duration;

      playlistItem.MusicTag = song.ToMusicTag();

      playlist.Add(playlistItem);

      return true;
    }
Example #9
0
    private void AddItemToFacadeControl(Song aSong)
    {
      GUIListItem item = new GUIListItem(aSong.ToShortString());
      item.Label = aSong.Artist + " - " + aSong.Title;

      string iconThumb = InfoScrobbler.GetSongAlbumImage(aSong);
      item.ThumbnailImage = item.IconImage = item.IconImageBig = iconThumb;
      item.MusicTag = aSong.ToMusicTag();

      facadeRadioPlaylist.Add(item);
    }
    private void LoadFacade()
    {
      TimeSpan totalPlayingTime = new TimeSpan();
      PlayList pl = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC);
      Song song = new Song();

      if (facadeLayout != null)
      {
        facadeLayout.Clear();
      }

      for (int i = 0; i < pl.Count; i++)
      {
        PlayListItem pi = pl[i];
        if (PlayListFactory.IsPlayList(pi.FileName))
        {
          pl.Remove(pi.FileName, false);
          LoadPlayList(pi.FileName, false, false, false, false);
        }
        // refresh pi if .m3u are cleaned up
        pi = pl[i];
        GUIListItem pItem = new GUIListItem(pi.Description);
        MusicTag tag = new MusicTag();
        if (m_database.GetSongByFileName(pi.FileName, ref song))
        {
          tag = song.ToMusicTag();
          pi.MusicTag = tag;
        }
        else
        {
          tag = TagReader.TagReader.ReadTag(pi.FileName);
          pi.MusicTag = tag;
        }
        bool dirtyTag = false;
        if (tag != null)
        {
          pItem.MusicTag = tag;
          if (tag.Title == ("unknown") || tag.Title.IndexOf("unknown") > 0 || tag.Title == string.Empty)
          {
            dirtyTag = true;
          }
        }
        else
        {
          dirtyTag = true;
        }

        if (tag != null && !dirtyTag)
        {
          int playCount = tag.TimesPlayed;
          string duration = Util.Utils.SecondsToHMSString(tag.Duration);
          pItem.Label = string.Format("{0} - {1}", tag.Artist, tag.Title);
          pItem.Label2 = duration;
          pItem.MusicTag = pi.MusicTag;
        }

        pItem.Path = pi.FileName;
        pItem.IsFolder = false;

        if (pi.Duration > 0)
        {
          totalPlayingTime = totalPlayingTime.Add(new TimeSpan(0, 0, pi.Duration));
        }

        if (pi.Played)
        {
          pItem.Shaded = true;
        }

        Util.Utils.SetDefaultIcons(pItem);

        pItem.OnRetrieveArt += new GUIListItem.RetrieveCoverArtHandler(OnRetrieveCoverArt);
        pItem.OnItemSelected += new GUIListItem.ItemSelectedHandler(item_OnItemSelected);

        facadeLayout.Add(pItem);
      }

      int iTotalItems = facadeLayout.Count;
      if (iTotalItems > 0)
      {
        GUIListItem rootItem = facadeLayout[0];
        if (rootItem.Label == "..")
        {
          iTotalItems--;
        }
      }

      //set object count label, total duration
      GUIPropertyManager.SetProperty("#itemcount", Util.Utils.GetObjectCountLabel(iTotalItems));

      if (totalPlayingTime.TotalSeconds > 0)
      {
        GUIPropertyManager.SetProperty("#totalduration",
                                       Util.Utils.SecondsToHMSString((int)totalPlayingTime.TotalSeconds));
      }
      else
      {
        GUIPropertyManager.SetProperty("#totalduration", string.Empty);
      }

      SelectCurrentItem();
      UpdateButtonStates();
    }