PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/WifiRemote/PluginConnection/MpMusicHelper.cs

http://wifiremote.googlecode.com/
C# | 270 lines | 183 code | 29 blank | 58 comment | 11 complexity | f4e64e0332138279076b3f23c7bde24f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MediaPortal.Music.Database;
  6. using WifiRemote.MPPlayList;
  7. using System.IO;
  8. using MediaPortal.Playlists;
  9. namespace WifiRemote.PluginConnection
  10. {
  11. /// <summary>
  12. /// Helper class for MpMusic actions
  13. /// </summary>
  14. public class MpMusicHelper
  15. {
  16. /// <summary>
  17. /// Plays a music track, starting from a given position
  18. /// </summary>
  19. /// <param name="trackId">Music track that is played</param>
  20. /// <param name="startPos">Start position within the song (in ms)</param>
  21. public static void PlayMusicTrack(int trackId, int startPos)
  22. {
  23. List<Song> songs = new List<Song>();
  24. string sql = "select * from tracks where idTrack=" + trackId;
  25. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  26. if (songs.Count > 0)
  27. {
  28. new Communication().PlayAudioFile(songs[0].FileName, startPos);
  29. }
  30. }
  31. /// <summary>
  32. /// Plays all songs from the given album (defined by artist+albumname), starting with item defined by startPos
  33. /// </summary>
  34. /// <param name="albumArtist">Artist that is played</param>
  35. /// <param name="album">Album that is played</param>
  36. /// <param name="startPos">Position from where playback is started (playlist index)</param>
  37. public static void PlayAlbum(String albumArtist, String album, int startPos)
  38. {
  39. List<Song> songs = new List<Song>();
  40. string sql = "select * from tracks where strAlbumArtist like '%" + albumArtist + "%' AND strAlbum LIKE '%" + album + "%' order by iTrack ASC";
  41. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  42. if (songs.Count > 0)
  43. {
  44. PlaylistHelper.ClearPlaylist("music", false);
  45. List<PlayListItem> items = new List<PlayListItem>();
  46. foreach (Song s in songs)
  47. {
  48. items.Add(PlaylistHelper.ToPlayListItem(s));
  49. }
  50. PlaylistHelper.AddPlaylistItems(PlayListType.PLAYLIST_MUSIC, items, 0);
  51. PlaylistHelper.StartPlayingPlaylist("music", startPos, true);
  52. }
  53. }
  54. /// <summary>
  55. /// Plays all songs from the given artist, starting with item defined by startPos
  56. /// </summary>
  57. /// <param name="albumArtist">Artist that is played</param>
  58. /// <param name="startPos">Position from where playback is started (playlist index)</param>
  59. internal static void PlayArtist(string albumArtist, int startPos)
  60. {
  61. List<Song> songs = new List<Song>();
  62. string sql = "select * from tracks where strAlbumArtist like '%" + albumArtist + "%'";
  63. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  64. if (songs.Count > 0)
  65. {
  66. PlaylistHelper.ClearPlaylist("music", false);
  67. List<PlayListItem> items = new List<PlayListItem>();
  68. foreach (Song s in songs)
  69. {
  70. items.Add(PlaylistHelper.ToPlayListItem(s));
  71. }
  72. PlaylistHelper.AddPlaylistItems(PlayListType.PLAYLIST_MUSIC, items, 0);
  73. PlaylistHelper.StartPlayingPlaylist("music", startPos, true);
  74. }
  75. }
  76. /// <summary>
  77. /// Plays all songs from the given folder, starting with item defined by startPos
  78. /// </summary>
  79. /// <param name="folder">Folder that is played</param>
  80. /// <param name="extensions">Valid extensions</param>
  81. /// <param name="startPos">Position from where playback is started (playlist index)</param>
  82. internal static void PlayAllFilesInFolder(string folder, string[] extensions, int startPos)
  83. {
  84. WifiRemote.LogMessage("Adding all files in " + folder + " to current playlist", WifiRemote.LogType.Debug);
  85. if (Directory.Exists(folder))
  86. {
  87. PlaylistHelper.ClearPlaylist("music", false);
  88. List<PlayListItem> items = new List<PlayListItem>();
  89. foreach (String f in Directory.GetFiles(folder))
  90. {
  91. if (isValidExtension(f, extensions))
  92. {
  93. FileInfo fileInfo = new FileInfo(f);
  94. items.Add(new PlayListItem(fileInfo.Name, fileInfo.FullName, 0));
  95. }
  96. }
  97. PlaylistHelper.AddPlaylistItems(PlayListType.PLAYLIST_MUSIC, items, 0);
  98. PlaylistHelper.StartPlayingPlaylist("music", startPos, true);
  99. }
  100. else
  101. {
  102. WifiRemote.LogMessage("Folder " + folder + " doesn't exist", WifiRemote.LogType.Warn);
  103. }
  104. }
  105. /// <summary>
  106. /// Checks if filename has a valid extension
  107. /// </summary>
  108. /// <param name="filename">Filename to check</param>
  109. /// <param name="extensions">Valid extensions</param>
  110. /// <returns></returns>
  111. private static bool isValidExtension(string filename, string[] extensions)
  112. {
  113. foreach (string e in extensions)
  114. {
  115. if (filename.EndsWith(e, StringComparison.InvariantCultureIgnoreCase))
  116. {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /// <summary>
  123. /// Plays a music file, starting from a given position
  124. /// </summary>
  125. /// <param name="file">File that is played</param>
  126. /// <param name="startPos">Start position within the song (in ms)</param>
  127. internal static void PlayFile(string file, int startPos)
  128. {
  129. MusicDatabase mpMusicDb = MusicDatabase.Instance;
  130. Song song = new Song();
  131. bool inDb = mpMusicDb.GetSongByFileName(file, ref song);
  132. if (inDb)
  133. {
  134. //TODO: fill OSD information
  135. new Communication().PlayAudioFile(file, startPos);
  136. }
  137. else
  138. {
  139. new Communication().PlayAudioFile(file, startPos);
  140. }
  141. }
  142. internal static void ShowMusicTrackDetails(int _trackId)
  143. {
  144. WifiRemote.LogMessage("Not implemented yet for mp music", WifiRemote.LogType.Info);
  145. }
  146. internal static void ShowAlbumDetails(string p, string p_2)
  147. {
  148. WifiRemote.LogMessage("Not implemented yet for mp music", WifiRemote.LogType.Info);
  149. }
  150. internal static void ShowArtistDetails(string p)
  151. {
  152. WifiRemote.LogMessage("Not implemented yet for mp music", WifiRemote.LogType.Info);
  153. }
  154. internal static void ShowFileDetails(string p)
  155. {
  156. WifiRemote.LogMessage("Not implemented yet for mp music", WifiRemote.LogType.Info);
  157. }
  158. internal static void ShowFolderDetails(string p)
  159. {
  160. WifiRemote.LogMessage("Not implemented yet for mp music", WifiRemote.LogType.Info);
  161. }
  162. /// <summary>
  163. /// Add MpExtended information to playlist item
  164. /// </summary>
  165. /// <param name="item">MediaPortal playlist item</param>
  166. /// <param name="message">Playlist message item that is sent to client</param>
  167. internal static void AddMpExtendedInfo(MediaPortal.Playlists.PlayListItem item, PlaylistEntry message)
  168. {
  169. MusicDatabase mpMusicDb = MusicDatabase.Instance;
  170. Song song = new Song();
  171. bool inDb = mpMusicDb.GetSongByFileName(item.FileName, ref song);
  172. if (inDb)
  173. {
  174. message.Name2 = song.Album;
  175. message.AlbumArtist = song.AlbumArtist;
  176. message.Title = song.Title;
  177. message.MpExtId = song.Id.ToString();
  178. message.MpExtMediaType = (int)MpExtended.MpExtendedMediaTypes.MusicTrack;
  179. message.MpExtProviderId = (int)MpExtended.MpExtendedProviders.MPMusic;
  180. }
  181. }
  182. /// <summary>
  183. /// Create a playlist item given a track id
  184. /// </summary>
  185. /// <param name="trackId">track id</param>
  186. /// <returns>Playlist item</returns>
  187. internal static MediaPortal.Playlists.PlayListItem CreatePlaylistItemFromMusicTrack(int trackId)
  188. {
  189. PlayListItem item = null;
  190. List<Song> songs = new List<Song>();
  191. string sql = "select * from tracks where idTrack=" + trackId;
  192. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  193. if (songs.Count > 0)
  194. {
  195. item = PlaylistHelper.ToPlayListItem(songs[0]);
  196. }
  197. return item;
  198. }
  199. /// <summary>
  200. /// Create a playlist item given an album and artist
  201. /// </summary>
  202. /// <param param name="album">Album</param>
  203. /// <param name="albumArtist">Album Artist</param>
  204. /// <returns>Playlist items</returns>
  205. internal static List<PlayListItem> CreatePlaylistItemsFromMusicAlbum(string albumArtist, string album)
  206. {
  207. List<PlayListItem> returnList = new List<PlayListItem>();
  208. List<Song> songs = new List<Song>();
  209. string sql = "select * from tracks where strAlbumArtist like '%" + albumArtist + "%' AND strAlbum LIKE '%" + album + "%'";
  210. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  211. if (songs.Count > 0)
  212. {
  213. foreach (Song s in songs)
  214. {
  215. returnList.Add(PlaylistHelper.ToPlayListItem(s));
  216. }
  217. }
  218. return returnList;
  219. }
  220. /// <summary>
  221. /// Create a playlist item given a artist id
  222. /// </summary>
  223. /// <param name="albumArtist">Album Artist</param>
  224. /// <returns>Playlist items</returns>
  225. internal static List<PlayListItem> CreatePlaylistItemsFromMusicArtist(string albumArtist)
  226. {
  227. List<PlayListItem> returnList = new List<PlayListItem>();
  228. List<Song> songs = new List<Song>();
  229. string sql = "select * from tracks where strAlbumArtist like '%" + albumArtist + "%'";
  230. MusicDatabase.Instance.GetSongsByFilter(sql, out songs, "tracks");
  231. if (songs.Count > 0)
  232. {
  233. foreach (Song s in songs)
  234. {
  235. returnList.Add(PlaylistHelper.ToPlayListItem(s));
  236. }
  237. }
  238. return returnList;
  239. }
  240. }
  241. }