admin管理员组

文章数量:1400559

I've exhausted my research for getting the title and duration from a music file. I've tried adding event listeners and even though the seemingly appropriate methods in those listeners get called, they appear to get called as soon as the process for getting metadata starts instead of when that process ends.

So far, this brittle MVC example is the only one that seems to work reliably. There are two problems with this code:

  1. The sleep could be too long sometimes (when the code is more conservative)
  2. The sleep could be too short for others (when the code is too aggressive)
import uk.co.caprica.vlcj.media.Meta;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import com.sun.jna.NativeLibrary;
public class Test2 {
   public static void main(String[] args) {
      NativeLibrary.addSearchPath("libvlc", "lib");
      System.setProperty("jna.library.path", "lib");
      System.setProperty("VLC_PLUGIN_PATH", "plugins");
      MediaPlayerFactory factory = new MediaPlayerFactory();
      for (String mediaPath : args) {
         Media media = factory.media().newMedia(mediaPath);
         media.parsing().parse();
         try {Thread.sleep(30);} catch (Throwable ex) {} // brittle
         System.out.printf("   Title: %s\n",     media.meta().get(Meta.TITLE));
         System.out.printf("Duration: %d(ms)\n", media.info().duration());
         media.release();
      }
      factory.release();
   }
}

I've also looked for classes that reveal processing state but came up empty.

I'm using vlcj 4.8.3 with jna 5.13.0.

I've exhausted my research for getting the title and duration from a music file. I've tried adding event listeners and even though the seemingly appropriate methods in those listeners get called, they appear to get called as soon as the process for getting metadata starts instead of when that process ends.

So far, this brittle MVC example is the only one that seems to work reliably. There are two problems with this code:

  1. The sleep could be too long sometimes (when the code is more conservative)
  2. The sleep could be too short for others (when the code is too aggressive)
import uk.co.caprica.vlcj.media.Meta;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import com.sun.jna.NativeLibrary;
public class Test2 {
   public static void main(String[] args) {
      NativeLibrary.addSearchPath("libvlc", "lib");
      System.setProperty("jna.library.path", "lib");
      System.setProperty("VLC_PLUGIN_PATH", "plugins");
      MediaPlayerFactory factory = new MediaPlayerFactory();
      for (String mediaPath : args) {
         Media media = factory.media().newMedia(mediaPath);
         media.parsing().parse();
         try {Thread.sleep(30);} catch (Throwable ex) {} // brittle
         System.out.printf("   Title: %s\n",     media.meta().get(Meta.TITLE));
         System.out.printf("Duration: %d(ms)\n", media.info().duration());
         media.release();
      }
      factory.release();
   }
}

I've also looked for classes that reveal processing state but came up empty.

I'm using vlcj 4.8.3 with jna 5.13.0.

Share Improve this question edited Mar 12 at 7:35 Jeff Holt asked Mar 12 at 5:53 Jeff HoltJeff Holt 3,2123 gold badges25 silver badges31 bronze badges 2
  • You want to tap into the media.events().addMediaParsedChangedListener(new MediaParsedChangedListener() { before you media.parsing().parse();, chatgpt told me. – Jeremy Thompson Commented Mar 12 at 6:01
  • I tried it. It doesn't work. It gets called too soon. Guess what happens when you reply to chatgpt saying "it doesn't work"? I'll give you a clue: after a couple of hours of editing an MCV example, you'll burn your daily free allotment of questions. – Jeff Holt Commented Mar 12 at 7:28
Add a comment  | 

1 Answer 1

Reset to default 1

Here's one way, you can deal with the asynchronicity in various ways:

package demo;

import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.MetaData;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

public class ParseDemo {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("Specify MRL");
            System.exit(1);
        }

        MediaPlayerFactory mpf = new MediaPlayerFactory("--quiet");
        Media media = mpf.media().newMedia(args[0]);

        CountDownLatch latch = new CountDownLatch(1);

        AtomicBoolean parsed = new AtomicBoolean(false);

        media.events().addMediaEventListener(new MediaEventAdapter() {
            @Override
            public void mediaParsedChanged(Media media, MediaParsedStatus newStatus) {
                System.out.printf("Parsed changed: %s%n", newStatus);
                switch (newStatus) {
                    case SKIPPED:
                    case FAILED:
                    case TIMEOUT:
                        latch.countDown();
                        break;
                    case DONE:
                        parsed.set(true);
                        latch.countDown();
                        break;
                }
            }
        });

        media.parsing().parse();

        latch.await();

        if (!parsed.get()) {
            System.out.println("Failed to parse");
            System.exit(0);
        }

        MetaData md = media.meta().asMetaData();
        System.out.printf("Parsed: %s%n", md);
    }
}

Sample output with vlcj-4.8.3:

Parsed changed: DONE
Parsed: MetaData[values={ALBUM=Night at the Grindhouse, ALBUM_ARTIST=STRNGR & Destryur, TRACK_NUMBER=9, TITLE=Corpse Boogie, ARTIST=STRNGR & Destryur, GENRE=Dance/Electronic, DISC_NUMBER=1},extraValues{}]

Event listener/adapter here:

https://github/caprica/vlcj/blob/vlcj-4.8.3/src/main/java/uk/co/caprica/vlcj/media/MediaEventAdapter.java#L54

本文标签: vlcjHow can I reliably get a music file39s title and durationStack Overflow