This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 21 - Pages: 2 [ 1 2 | Next ] - Last Post: 4 May 22, 16:28 Last Post By: RobM Threads: [ Previous | Next ]
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 14:52
 
  Click to reply to this thread Reply
In Jupiter, I'm using a messy method to get the duration of an MP3 (ignore the ttype - that's just for two different types of String formatting):
private String mp3Length(AlbumObject ao, int ttype) {
	CountDownLatch workerLatch = new CountDownLatch(1);
	String cl = "";
	Media media = new Media(ao.getFile().toURI().toString());
	MediaPlayer mp = new MediaPlayer(media);
	mp.setOnReady(new Runnable() {
		public void run() {
			if(media.getDuration() != null) {
				tc = TimeCode.fromMillis((long)media.getDuration().toMillis()).toString();
				String[] parts = tc.split(":");
				String[] labels1 = {"h ", "m ", "s"};
				String[] labels2 = {":", ":", ""};
				for (int i = 0; i < parts.length; i++) {
					int value = (int)Double.parseDouble(parts[i]);
					if(ttype == 1 && (cl.length() > 0 || value > 0)) cl += Integer.toString(value) + labels1[i];
					else if(ttype == 2 && (value > 0 || i > 0)) cl += String.format("%02d", value) + labels2[i];
				}
			}
			workerLatch.countDown();
		}
	});
	mp.setOnError(new Runnable() {
		public void run() {
			workerLatch.countDown();
		}
	});
	try {
		workerLatch.await();
	} catch (InterruptedException ex) {
		throw new RuntimeException(ex);
	} finally {
		mp.dispose(); 
	}
	if(cl.equals("")) cl = null;
	return cl;
}
It's very fast, and returns the correct value.

In Minimal, I've planted this in *slide.htt*
<%
if(fileCategory == Category.audio)  {
	tc=MediaUtil.getDuration(new File(fileName));
	System.out.println(fileName);
	System.out.println(tc);
}
%>
The file name is correct. It takes an ungodly long time to execute, and returns null every time.

A clue?
RobM

Posts: 4,380
Registered: 4-Aug-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 15:35   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
Have you tried other formats than mp3?
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 15:38   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
RobM wrote:
Have you tried other formats than mp3?

Exactly the same result with an M4A. Long, long delay, followed by null.
RobM

Posts: 4,380
Registered: 4-Aug-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 15:41   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
JeffTucker wrote:
RobM wrote:
Have you tried other formats than mp3?

Exactly the same result with an M4A. Long, long delay, followed by null.

Maybe it only works with video
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 15:50   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Exactly the same result with a video. And this was introduced to go along with audio clips, so....
RobM

Posts: 4,380
Registered: 4-Aug-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 16:34   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
Last idea, does it need a ‘new instance’ being a ‘class’ thingymebob.
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 16:41   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
I'm just going to wait for divine guidance.

What I've got has been working like a champ for several years, so the only reason to replace it is the desire for "neatness."
RobM

Posts: 4,380
Registered: 4-Aug-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 22:09   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
JeffTucker wrote:
I'm just going to wait for divine guidance.

What I've got has been working like a champ for several years, so the only reason to replace it is the desire for "neatness."

I ran this code
AlbumObject folder = JAlbumContext.getInstance().getExplorer().getCurrentFolder();
for(AlbumObject ao:folder.getChildren()) {
fileCategory = ao.getCategory();
fileName = ao.getFile();
if(fileCategory == Category.audio)  {
	tc=MediaUtil.getDuration(ao.getFile());
	System.out.println(fileName);
	System.out.println(tc);
}
}
In the System console, with a folder with an mp3 and a wav file. The result was
Users/robert/My Albums/Car/Arrow_Hit.wav
00:00:00.779
/Users/robert/My Albums/Car/rainthunSiren.mp3
00:00:05.849
0.197s ==> null
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 22:17   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Oh, you get the prize for the day. That executes quickly (on a par with my existing routine), and yields the correct info.

Once again, David has just left us to thrash about in the water for a while, in the hopes that we'd learn how to swim. :)
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 23:03   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
BTW, if anyone starts to tinker with this, a warning....

Let's say the audio is 34.985 seconds long. You might try to muck about with this so that it rounds off properly, to 35 seconds.

But guess what. When you actually show the audio control bar in Firefox, it will show the length as 35 seconds, as it should. But Chrome will show it as 34 seconds. So if you have, for example, posted the length of the audio under the thumbnail, it will always be wrong by one second in either one browser or the other. Firefox rounds up, Chrome truncates.

All part of life's rich pageant.
RobM

Posts: 4,380
Registered: 4-Aug-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 3 May 22, 23:45   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
JeffTucker wrote:
Oh, you get the prize for the day. That executes quickly (on a par with my existing routine), and yields the correct info.

Once again, David has just left us to thrash about in the water for a while, in the hopes that we'd learn how to swim. :)

Do I now qualify as providing divine guidance ;)
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 4 May 22, 00:26   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
We are not worthy.
davidekholm

Posts: 4,405
Registered: 18-Oct-2002
Re: Having no luck with MediaUtils.getDuration()
Posted: 4 May 22, 13:03   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
You figured it out: Your file references were relative the current working directory and therefore led to missing files. To see better error messages use the loadMedia version and watch the exception. The getDuration version is simply a convenience call, but as it doesn't throw exceptions (just returning null) it makes debugging harder. Here's the implementation of getDuration by the way:
    public static TimeCode getDuration(File mediaFile) {
        try {
            Media media = loadMedia(mediaFile);
            Duration d = media.getDuration();
            if (d != Duration.UNKNOWN) {
                return new TimeCode(d.toMillis());
            }
        } catch (MediaException | IOException ex) {
        }
        return null;
    }
JeffTucker

Posts: 8,460
Registered: 31-Jan-2006
Re: Having no luck with MediaUtils.getDuration()
Posted: 4 May 22, 13:57   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Tough to measure accurately, but it does seem to be executing in about half the time of the method I was using in init.bsh, perhaps because the old method was interpreted, instead of compiled.
davidekholm

Posts: 4,405
Registered: 18-Oct-2002
Re: Having no luck with MediaUtils.getDuration()
Posted: 4 May 22, 14:09   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
JeffTucker wrote:
Tough to measure accurately, but it does seem to be executing in about half the time of the method I was using in init.bsh, perhaps because the old method was interpreted, instead of compiled.

I don't think so. 99% of the time is spent within the MediaPlayer doing IO, but good that it seems to perform faster. The code "under the hood" within MediaUtil looks very similar to your old code:
    private Media doLoadMedia(String mediaURI) throws MediaException, IOException {
        CountDownLatch latch = new CountDownLatch(1);
        media = new Media(mediaURI);
        MediaPlayer player = new MediaPlayer(media);
        player.setOnReady(() -> {
            latch.countDown();
        });
        player.setOnError(() -> {
            latch.countDown();
        });
 
        try {
            latch.await(3000, TimeUnit.MILLISECONDS);
            if (media.getError() != null) {
                throw media.getError();
            }
            player.dispose();
            return media;
        } catch (InterruptedException ex) {
            throw new IOException("Timeout reading media from " + mediaURI);
        }
    }
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums