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.LinkedHashSet;
30 import java.util.List;
32 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaDescriptor;
34 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
35 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
38 public class MediaList
41 private final JVLC jvlc;
43 private final LibVlcMediaList instance;
45 private final LibVlcEventManager eventManager;
47 private List<String> items = new ArrayList<String>();
49 public MediaList(JVLC jvlc)
52 libvlc_exception_t exception = new libvlc_exception_t();
53 instance = jvlc.getLibvlc().libvlc_media_list_new(jvlc.getInstance(), exception);
54 eventManager = jvlc.getLibvlc().libvlc_media_list_event_manager(instance, exception);
58 * @param mrl The media resource locator to add to the media list.
60 public void addMedia(String mrl)
62 MediaDescriptor descriptor = new MediaDescriptor(jvlc, mrl);
67 * @param descriptor The media descriptor to add to the media list.
69 public void addMedia(MediaDescriptor descriptor)
71 if (items.contains(descriptor.getMrl()))
75 items.add(descriptor.getMrl());
76 libvlc_exception_t exception = new libvlc_exception_t();
77 jvlc.getLibvlc().libvlc_media_list_add_media(instance, descriptor.getInstance(), exception);
81 * @return The current number of items in the media list.
85 libvlc_exception_t exception = new libvlc_exception_t();
86 return jvlc.getLibvlc().libvlc_media_list_count(instance, exception);
90 * @param descriptor The media descriptor to get the index of.
91 * @return The index of the media descriptor, or -1 if not found.
93 public int indexOf(MediaDescriptor descriptor)
95 libvlc_exception_t exception = new libvlc_exception_t();
96 return jvlc.getLibvlc().libvlc_media_list_index_of_item(instance, descriptor.getInstance(), exception);
100 * @param index The index of the media descriptor to get.
101 * @return The media descriptor at the given index.
102 * @throws IndexOutOfBoundsException if index is bigger than size() or < 0, or there are no items in the media_list.
104 public MediaDescriptor getMediaDescriptorAtIndex(int index)
106 libvlc_exception_t exception = new libvlc_exception_t();
109 throw new IndexOutOfBoundsException();
111 if (index < 0 || index > size())
113 throw new IndexOutOfBoundsException();
115 LibVlcMediaDescriptor descriptor = jvlc.getLibvlc().libvlc_media_list_item_at_index(instance, index, exception);
116 return new MediaDescriptor(jvlc, descriptor);
120 * @param index The index of the media to remove.
121 * @return True if the media was successfully removed, false otherwise.
123 public boolean removeMedia(int index)
125 libvlc_exception_t exception = new libvlc_exception_t();
126 jvlc.getLibvlc().libvlc_media_list_remove_index(instance, index, exception);
127 if (exception.raised == 0)
136 * @param media The media descriptor mrl.
138 public boolean removeMedia(String mrl)
140 int index = items.indexOf(mrl);
145 return removeMedia(index);
149 * @param mediaDescriptor The media descriptor to remove.
151 public boolean removeMedia(MediaDescriptor mediaDescriptor)
153 String mrl = mediaDescriptor.getMrl();
154 int index = items.indexOf(mrl);
159 return removeMedia(index);
163 * Removes all items from the media list.
167 for (int i = 0; i < size(); i++)
174 * @param descriptor The media descriptor to insert.
175 * @param index The index of the inserted media descriptor.
177 public void insertMediaDescriptor(MediaDescriptor descriptor, int index)
179 libvlc_exception_t exception = new libvlc_exception_t();
182 .libvlc_media_list_insert_media(instance, descriptor.getInstance(), index, exception);
189 protected void finalize() throws Throwable
191 jvlc.getLibvlc().libvlc_media_list_release(instance);
196 * Returns the instance.
197 * @return the instance
199 LibVlcMediaList getInstance()