2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
23 namespace Tizen.Content.MediaContent
26 /// The PlayList API provides functions to manage media playlists.
29 /// A PlayList is a list of songs which can be played in some sequence i.e. sequential or shuffled order.
30 /// The Media PlayList API provides functions to insert, delete or updates a media playlist in the database.
32 public class PlayList : ContentCollection
34 private readonly IDictionary<string, int> _dictionary = new Dictionary<string, int>();
35 private IntPtr _playlistHandle = IntPtr.Zero;
36 internal IntPtr Handle
40 if (_playlistHandle == IntPtr.Zero)
42 throw new ObjectDisposedException(nameof(PlayList));
45 return _playlistHandle;
50 _playlistHandle = value;
54 private void RefreshPlaylistDictionary()
57 Interop.Playlist.PlaylistMemberCallback callback = (int memberId, IntPtr mediaHandle, IntPtr data) =>
59 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
60 IntPtr val = IntPtr.Zero;
63 MediaContentValidator.ThrowIfError(
64 Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
66 Interop.MediaInformation.GetMediaId(newHandle, out val);
67 _dictionary.Add(Marshal.PtrToStringAnsi(val), memberId);
72 Interop.Libc.Free(val);
75 MediaContentValidator.ThrowIfError(
76 Interop.Playlist.ForeachMediaFromDb(Id, IntPtr.Zero, callback, IntPtr.Zero), "Failed to get playlist items");
80 /// The ID of the media playlist
82 /// <since_tizen> 3 </since_tizen>
88 MediaContentValidator.ThrowIfError(
89 Interop.Playlist.GetPlaylistId(Handle, out id), "Failed to get value");
95 internal string _playListName;
99 /// <since_tizen> 3 </since_tizen>
104 return _playListName;
109 MediaContentValidator.ThrowIfError(
110 Interop.Playlist.SetName(Handle, value), "Failed to set value");
111 _playListName = value;
115 /// The path of the thumbnail
117 /// <since_tizen> 3 </since_tizen>
118 public string ThumbnailPath
122 IntPtr val = IntPtr.Zero;
125 MediaContentValidator.ThrowIfError(
126 Interop.Playlist.GetThumbnailPath(Handle, out val), "Failed to get value");
127 return Marshal.PtrToStringAnsi(val);
131 Interop.Libc.Free(val);
137 MediaContentValidator.ThrowIfError(
138 Interop.Playlist.SetThumbnailPath(Handle, value), "Failed to set value");
143 /// The constructor to create a new playlist with the given name in the media database.
145 /// <since_tizen> 3 </since_tizen>
146 /// <param name="name">The name of the inserted playlist</param>
147 public PlayList(string name)
149 _playListName = name;
152 internal PlayList(IntPtr handle)
154 _playlistHandle = handle;
155 IntPtr val = IntPtr.Zero;
158 MediaContentValidator.ThrowIfError(
159 Interop.Playlist.GetName(handle, out val), "Failed to get value");
160 _playListName = Marshal.PtrToStringAnsi(val);
164 Interop.Libc.Free(val);
169 /// Adds a new media info to the playlist.
171 /// <since_tizen> 3 </since_tizen>
172 /// <param name="mediaContent">The AudioContent obect to be added</param>
173 public void AddItem(MediaInformation mediaContent)
175 MediaContentValidator.ThrowIfError(
176 Interop.Playlist.AddMedia(Handle, mediaContent.MediaId), "Failed to add item");
180 /// Removes the playlist members related with the media from the given playlist.
182 /// <since_tizen> 3 </since_tizen>
183 /// <param name="media">The AudioContent object to be removed</param>
184 public void RemoveItem(MediaInformation media)
187 RefreshPlaylistDictionary();
188 _dictionary.TryGetValue(media.MediaId, out memberId);
189 MediaContentValidator.ThrowIfError(
190 Interop.Playlist.RemoveMedia(Handle, memberId), "Failed to remove item");
194 /// Sets the playing order in the playlist.
196 /// <since_tizen> 3 </since_tizen>
197 /// <param name="media">The playlist reference</param>
198 /// <param name="playOrder">The playing order</param>
199 public void SetPlayOrder(MediaInformation media, int playOrder)
202 RefreshPlaylistDictionary();
203 _dictionary.TryGetValue(media.MediaId, out memberId);
204 MediaContentValidator.ThrowIfError(
205 Interop.Playlist.SetPlayOrder(Handle, memberId, playOrder), "Failed to set play order");
209 /// Gets the playing order in the playlist for the passed member id.
211 /// <since_tizen> 3 </since_tizen>
212 /// <param name="media">The MediaInformation instance</param>
213 /// <returns>The number of play order</returns>
214 public int GetPlayOrder(MediaInformation media)
218 RefreshPlaylistDictionary();
219 _dictionary.TryGetValue(media.MediaId, out memberId);
220 MediaContentValidator.ThrowIfError(
221 Interop.Playlist.GetPlayOrder(Handle, memberId, out playOrder), "Failed to get play order");
227 /// Imports the playlist from m3u playlist file.
229 /// <since_tizen> 3 </since_tizen>
230 /// <param name="name">The name of the playlist to save</param>
231 /// <param name="filePath">The path to import the playlist file</param>
232 /// <returns>The imported PlayList object</returns>
233 public static PlayList Import(string name, string filePath)
235 PlayList playList = null;
236 IntPtr playlistHandle;
238 MediaContentValidator.ThrowIfError(
239 Interop.Playlist.ImportFromFile(name, filePath, out playlistHandle), "Failed to import");
241 playList = new PlayList(name);
242 playList.Handle = playlistHandle;
247 /// Exports the playlist to m3u playlist file.
249 /// <since_tizen> 3 </since_tizen>
250 /// <param name="list">The playlist instance to export</param>
251 /// <param name="filePath">The path to save exported playlist</param>
252 /// <returns>path The path to export the playlist</returns>
253 public static void Export(PlayList list, string filePath)
255 MediaContentValidator.ThrowIfError(
256 Interop.Playlist.ExportToFile(list.Handle, filePath), "Failed to export playlist:" + filePath);
260 /// Gets the number of the media info for the given playlist present in the media database.
262 /// <since_tizen> 3 </since_tizen>
263 /// <param name="filter">ContentFilter used to match media content from the media database.</param>
264 /// <returns>The number of media contents matching the filter passed</returns>
265 public override int GetMediaInformationCount(ContentFilter filter)
268 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
269 MediaContentValidator.ThrowIfError(
270 Interop.Playlist.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get media count");
275 public override void Dispose()
277 if (_playlistHandle != IntPtr.Zero)
279 Interop.Playlist.Destroy(_playlistHandle);
280 _playlistHandle = IntPtr.Zero;
285 /// Iterates through the media files with an optional filter in the given audio playlist from the media database.
286 /// This function gets all media files associated with the given media playlist and meeting desired filter option.
287 /// If NULL is passed to the filter, no filtering is applied.
289 /// <since_tizen> 3 </since_tizen>
290 /// <param name="filter">ContentFilter used to match media content from the media database.</param>
291 /// <returns>List of content media items matching the passed filter</returns>
292 public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
294 List<MediaInformation> mediaContents = new List<MediaInformation>();
295 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
296 Interop.Playlist.PlaylistMemberCallback callback = (int memberId, IntPtr mediaHandle, IntPtr data) =>
298 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
299 MediaContentValidator.ThrowIfError(
300 Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
302 MediaContentType type;
303 Interop.MediaInformation.GetMediaType(newHandle, out type);
304 if (type == MediaContentType.Image)
306 Interop.ImageInformation.SafeImageInformationHandle imageInfo;
307 MediaContentValidator.ThrowIfError(
308 Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
310 mediaContents.Add(new ImageInformation(imageInfo, newHandle));
312 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
314 Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
315 MediaContentValidator.ThrowIfError(
316 Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
318 mediaContents.Add(new AudioInformation(audioInfo, newHandle));
320 else if (type == MediaContentType.Video)
322 Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
323 MediaContentValidator.ThrowIfError(
324 Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
326 mediaContents.Add(new VideoInformation(videoInfo, newHandle));
328 else if (type == MediaContentType.Others)
330 mediaContents.Add(new MediaInformation(newHandle));
335 MediaContentValidator.ThrowIfError(
336 Interop.Playlist.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get media information");
338 return mediaContents;