1 /*****************************************************************************
2 * MediaList.java: VLC Java Bindings, MediaList
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
6 * Authors: Filippo Carone <filippo@carone.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 package org.videolan.jvlc;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
32 import org.videolan.jvlc.internal.LibVlc.LibVlcMedia;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
34 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
37 public class MediaList
40 private final JVLC jvlc;
42 private final LibVlcMediaList instance;
44 private List<String> items = new ArrayList<String>();
46 private LibVlcEventManager eventManager;
48 private volatile boolean released;
50 public MediaList(JVLC jvlc)
53 libvlc_exception_t exception = new libvlc_exception_t();
54 instance = jvlc.getLibvlc().libvlc_media_list_new(jvlc.getInstance(), exception);
55 eventManager = jvlc.getLibvlc().libvlc_media_list_event_manager(instance, exception);
59 * @param mrl The media resource locator to add to the media list.
61 public void addMedia(String mrl)
63 MediaDescriptor descriptor = new MediaDescriptor(jvlc, mrl);
68 * @param descriptor The media descriptor to add to the media list.
70 public void addMedia(MediaDescriptor descriptor)
72 if (items.contains(descriptor.getMrl()))
76 items.add(descriptor.getMrl());
77 libvlc_exception_t exception = new libvlc_exception_t();
78 jvlc.getLibvlc().libvlc_media_list_add_media(instance, descriptor.getInstance(), exception);
82 * @return The current number of items in the media list.
86 libvlc_exception_t exception = new libvlc_exception_t();
87 return jvlc.getLibvlc().libvlc_media_list_count(instance, exception);
91 * @param descriptor The media descriptor to get the index of.
92 * @return The index of the media descriptor, or -1 if not found.
94 public int indexOf(MediaDescriptor descriptor)
96 libvlc_exception_t exception = new libvlc_exception_t();
97 return jvlc.getLibvlc().libvlc_media_list_index_of_item(instance, descriptor.getInstance(), exception);
101 * @param index The index of the media descriptor to get.
102 * @return The media descriptor at the given index.
103 * @throws IndexOutOfBoundsException if index is bigger than size() or < 0, or there are no items in the media_list.
105 public MediaDescriptor getMediaDescriptorAtIndex(int index)
107 libvlc_exception_t exception = new libvlc_exception_t();
110 throw new IndexOutOfBoundsException();
112 if (index < 0 || index > size())
114 throw new IndexOutOfBoundsException();
116 LibVlcMedia descriptor = jvlc.getLibvlc().libvlc_media_list_item_at_index(instance, index, exception);
117 return new MediaDescriptor(jvlc, descriptor);
121 * @param index The index of the media to remove.
122 * @return True if the media was successfully removed, false otherwise.
124 public boolean removeMedia(int index)
126 libvlc_exception_t exception = new libvlc_exception_t();
127 jvlc.getLibvlc().libvlc_media_list_remove_index(instance, index, exception);
128 if (exception.b_raised == 0)
137 * @param mrl The media descriptor mrl.
139 public boolean removeMedia(String mrl)
141 int index = items.indexOf(mrl);
146 return removeMedia(index);
150 * @param mediaDescriptor The media descriptor to remove.
152 public boolean removeMedia(MediaDescriptor mediaDescriptor)
154 String mrl = mediaDescriptor.getMrl();
155 int index = items.indexOf(mrl);
160 return removeMedia(index);
164 * Removes all items from the media list.
168 for (int i = 0; i < size(); i++)
175 * @param descriptor The media descriptor to insert.
176 * @param index The index of the inserted media descriptor.
178 public void insertMediaDescriptor(MediaDescriptor descriptor, int index)
180 libvlc_exception_t exception = new libvlc_exception_t();
183 .libvlc_media_list_insert_media(instance, descriptor.getInstance(), index, exception);
190 protected void finalize() throws Throwable
197 * Returns the instance.
198 * @return the instance
200 LibVlcMediaList getInstance()
208 public void release()
216 jvlc.getLibvlc().libvlc_media_list_release(instance);