Results 1 to 3 of 3
  1. #1
    lambi is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default 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...

    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");
                }
            }
        }
    }
    (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.)

    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 with
    Java Code:
                    ds = Manager.createDataSource(links[1]);
                    player.setSource(ds);
    just does not seem to work. Only workaround so far is simply closing the player and opening a new one with the next link
    Java Code:
                    player.deallocate();
                    player.close();
                    createPlayer(links[1]);
    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?
    Anyone has a solution / hint where i'm wrong?

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    In EventHandler you could try this try this instead:

    Java Code:
    player = javax.media.Manager.createPlayer(links[1]);
    so now second songs will be played.

    I removed:
    Java Code:
     DataSource ds = null;
     ds = Manager.createDataSource(links[1]);
     player.setSource(ds);
    I have no knowledge of this API
    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?

  3. #3
    lambi is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    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

  1. Vlc player interfaces in java
    By sankar1227 in forum Networking
    Replies: 1
    Last Post: 08-06-2011, 09:39 PM
  2. MP3 Player idea
    By vinoth in forum New To Java
    Replies: 3
    Last Post: 08-18-2009, 02:10 AM
  3. Replies: 8
    Last Post: 08-28-2008, 12:48 AM
  4. SWT Flash Player
    By forthe in forum SWT / JFace
    Replies: 0
    Last Post: 07-29-2008, 09:48 AM
  5. mp3 player applet
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 05-20-2008, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •