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:
- The sleep could be too long sometimes (when the code is more conservative)
- 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:
- The sleep could be too long sometimes (when the code is more conservative)
- 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 |1 Answer
Reset to default 1Here'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
版权声明:本文标题:vlcj - How can I reliably get a music file's title and duration? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768729a2624204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
media.events().addMediaParsedChangedListener(new MediaParsedChangedListener() {
before youmedia.parsing().parse();
, chatgpt told me. – Jeremy Thompson Commented Mar 12 at 6:01