]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/MediaList.java
5477318340bbf1e65a18029fa06fc8961403de1f
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / MediaList.java
1 /*****************************************************************************
2  * MediaList.java: VLC Java Bindings, MediaList
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  *
9  * $Id $
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 package org.videolan.jvlc;
27
28 import java.util.ArrayList;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31
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;
36
37
38 public class MediaList
39 {
40
41     private final JVLC jvlc;
42
43     private final LibVlcMediaList instance;
44
45     private final LibVlcEventManager eventManager;
46
47     private List<String> items = new ArrayList<String>();
48
49     public MediaList(JVLC jvlc)
50     {
51         this.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);
55     }
56
57     public void addMedia(String media)
58     {
59         MediaDescriptor descriptor = new MediaDescriptor(jvlc, media);
60         addMedia(descriptor);
61     }
62
63     public void addMedia(MediaDescriptor descriptor)
64     {
65         if (items.contains(descriptor.getMrl()))
66         {
67             return;
68         }
69         items.add(descriptor.getMrl());
70         libvlc_exception_t exception = new libvlc_exception_t();
71         jvlc.getLibvlc().libvlc_media_list_add_media_descriptor(instance, descriptor.getInstance(), exception);
72     }
73
74     public int itemsCount()
75     {
76         libvlc_exception_t exception = new libvlc_exception_t();
77         return jvlc.getLibvlc().libvlc_media_list_count(instance, exception);
78     }
79
80     public int indexOf(MediaDescriptor descriptor)
81     {
82         libvlc_exception_t exception = new libvlc_exception_t();
83         return jvlc.getLibvlc().libvlc_media_list_index_of_item(instance, descriptor.getInstance(), exception);
84     }
85
86     public MediaDescriptor getMediaDescriptorAtIndex(int index)
87     {
88         libvlc_exception_t exception = new libvlc_exception_t();
89         LibVlcMediaDescriptor descriptor = jvlc.getLibvlc().libvlc_media_list_item_at_index(instance, index, exception);
90         return new MediaDescriptor(jvlc, descriptor);
91     }
92
93     /**
94      * @param index The index of the media to remove
95      * @return True if the media was successfully removed, false otherwise.
96      */
97     public boolean removeMedia(int index)
98     {
99         libvlc_exception_t exception = new libvlc_exception_t();
100         jvlc.getLibvlc().libvlc_media_list_remove_index(instance, index, exception);
101         if (exception.raised == 0)
102         {
103             items.remove(index);
104             return true;
105         }
106         return false;
107     }
108
109     /**
110      * @param media The media descriptor mrl
111      */
112     public boolean removeMedia(String mrl)
113     {
114         int index = items.indexOf(mrl);
115         if (index == -1)
116         {
117             return false;
118         }
119         return removeMedia(index);
120     }
121
122     public void insertMediaDescriptor(MediaDescriptor descriptor, int index)
123     {
124         libvlc_exception_t exception = new libvlc_exception_t();
125         jvlc
126             .getLibvlc()
127             .libvlc_media_list_insert_media_descriptor(instance, descriptor.getInstance(), index, exception);
128     }
129
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     protected void finalize() throws Throwable
135     {
136         jvlc.getLibvlc().libvlc_media_list_release(instance);
137         super.finalize();
138     }
139
140     /**
141      * Returns the instance.
142      * @return the instance
143      */
144     LibVlcMediaList getInstance()
145     {
146         return instance;
147     }
148
149     /**
150      * @param mediaDescriptor
151      */
152     public boolean removeMedia(MediaDescriptor mediaDescriptor)
153     {
154         String mrl = mediaDescriptor.getMrl();
155         int index = items.indexOf(mrl);
156         if (index == -1)
157         {
158             return false;
159         }
160         return removeMedia(index);
161     }
162
163 }