Results 1 to 3 of 3
Thread: JMF player.setSource
- 02-19-2010, 05:10 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
JMF player.setSource
Ok, so maybe i'm an idiot and the solution is right in front of me and i can't figure it out, but for the life of me i can't get this to work the way i want.
I'm coding a simple audio player which is supposed ot read the contents of a folder, and then play the contents (currently in order, to be changed later). But for whatever reason, i can't get my player to accept a new source...
(omitted: a whole bunch of jaudiotagger-stuff which i use to read the id-tags, as well as all the try-catch blocks needed for DataSources, URLs etc.)Java Code:public class Library { static AudioFile f; static Tag tag; static String[] names; static URL[] links; static Player player; public static void main(String[] args) { File lib = new File("E:/MediaLibrary/music"); System.out.println(lib); List<File> list = new ArrayList<File>(); getLinks(lib, list); System.out.println(names.length); for (int i = 0; i < names.length; i++) { System.out.println("Name:" + names[i]); System.out.println("Link: " + links[i].toString()); } createPlayer(links[0]); System.out.println(player.getState()); } private static void createPlayer(URL link) { player = Manager.createPlayer(link); player.start(); player.addControllerListener(new EventHandler()); } private static void getLinks(File folder, List<File> list) { folder.setReadOnly(); File[] files = folder.listFiles(); names = new String[files.length]; links = new URL[files.length]; for (int j = 0; j < files.length; j++) { if (files[j].isDirectory()) { getLinks(files[j], list); } else { list.add(files[j]); } } for (int x = 0; x < list.size(); x++) { f = AudioFileIO.read(list.get(x)); tag = f.getTag(); names[x] = tag.getFirst(FieldKey.ARTIST); links[x] = list.get(x).toURL(); } } private static class EventHandler implements ControllerListener { public void controllerUpdate(ControllerEvent event) { if (event instanceof EndOfMediaEvent) { player.deallocate(); System.out.println("end of media"); DataSource ds = null; ds = Manager.createDataSource(links[1]); player.setSource(ds); player.start(); System.out.println("player started"); } } } }
Now, everything that happens is that the first song in the directory (as saved in links[0]) gets played over and over again. Defining a new DataSource withjust does not seem to work. Only workaround so far is simply closing the player and opening a new one with the next linkJava Code:ds = Manager.createDataSource(links[1]); player.setSource(ds);But i don't like it as it just calls for memory leaks or something like that. And if there's the possibility to set a new datasource, why not use it?Java Code:player.deallocate(); player.close(); createPlayer(links[1]);
Anyone has a solution / hint where i'm wrong?
- 02-21-2010, 03:33 AM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
In EventHandler you could try this try this instead:
so now second songs will be played.Java Code:player = javax.media.Manager.createPlayer(links[1]);
I removed:
I have no knowledge of this APIJava Code:DataSource ds = null; ds = Manager.createDataSource(links[1]); player.setSource(ds);
and is it wise to call twice Manager.createPlayer
or should it be done with DataSource like you did,
but i've got two songs playing not one for two times
was this helpful?
- 02-22-2010, 01:09 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Well, it basically does the same as my workaround. Except in your solution we're reusing the same player object, which probably is better than instancing a new one as i did. And it seems to work just fine, judging from the memory footprint, all the memory used by the player gets properly freed up on media change, so no memory leak there. So, even if it's not as "elegant" is it could be, it works well enough.
Thanks.
Similar Threads
-
Vlc player interfaces in java
By sankar1227 in forum NetworkingReplies: 1Last Post: 08-06-2011, 08:39 PM -
MP3 Player idea
By vinoth in forum New To JavaReplies: 3Last Post: 08-18-2009, 01:10 AM -
[SOLVED] ActionEvent.setSource() is failing in Swing while working in AWT
By playwin2 in forum AWT / SwingReplies: 8Last Post: 08-27-2008, 11:48 PM -
SWT Flash Player
By forthe in forum SWT / JFaceReplies: 0Last Post: 07-29-2008, 08:48 AM -
mp3 player applet
By willemjav in forum Java AppletsReplies: 0Last Post: 05-20-2008, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks