Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 31
Registered: ‎07-25-2013
My Device: Q10
My Carrier: Vinaphone

How to get length of an audio file

Hi all,

I'm sorry if it is a dump question, but I still can not figure out how to get length (in seconds) of a media file (e.g. m4a file).

I have bunch of a media files & I want to iterate through them & get length of each one.

 

Best regards,

 

Regular Contributor
Posts: 56
Registered: ‎06-25-2012
My Device: non-blackberry user
My Carrier: Airtel

Re: How to get length of an audio file

if you are using MediaPlayer QML component than i think they have a duration property !!
Retired
Posts: 252
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10
My Carrier: Bell

Re: How to get length of an audio file

Hi yaiba24,

 

Here is a link to the documentation of the duration property of the MediaPlayer API: http://developer.blackberry.com/native/reference/cascades/bb__multimedia__mediaplayer.html#property-...

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Developer
Posts: 31
Registered: ‎07-25-2013
My Device: Q10
My Carrier: Vinaphone

Re: How to get length of an audio file

Hi, I know that MediaPlayer API has duration property, however my question was about looping through a hundreds of audio files and get their duration to display in a list, so my concern in using duration property are:

- duration only available once durationChanged() signal fired (correct me if I'm wrong here), so it's not as easy as "open file, get duration, go next"

- looping through a hundreds of audio files and use MediaPlayer to parse each file just for duration information is a most efficient way (in term of processing time, memory usage, etc.)?

 

Best regards,

 

Retired
Posts: 252
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10
My Carrier: Bell

Re: How to get length of an audio file

[ Edited ]

Hi yaiba24,

 

- Based on the documentation, the duration is available once prepare() has been called and the file is ready to be played. It gets updated when the durationChanged() signal is fired.

 

"Provides the duration of a currently prepared track.

The duration of the current track is available once prepare() has been called, and the player has successfully acquired the resources to play the current track. The duration of the current track is no longer available when stop() has been called. In any other case, a duration of zero is returned."

 

-  Here is one way to do it (i haven't tried it myself though): Define a single MediaPlayer object attached to your top control and in a loop (in C++ or in QML) loop on all media files in your list and change the sourceURL property. In the same loop, call the duration() method to retrieve the duration of the file.

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Retired
Posts: 252
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10
My Carrier: Bell

Re: How to get length of an audio file

Also, i suggest you try using the MetaData class, which seems to be an API that allows you to retrieve the metadata of media files including the duration property:

 

http://developer.blackberry.com/native/reference/cascades/bb__multimedia__metadata.html

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Highlighted
Developer
Posts: 31
Registered: ‎07-25-2013
My Device: Q10
My Carrier: Vinaphone

Re: How to get length of an audio file

Hi sabdelsayed,

The key in your quoted doc is "The duration of the current track is available once prepare() has been called, and the player has successfully acquired the resources to play the current track."

So after prepare() is called, MediaPlayer will notified you about duration in durationChanged event once it's available. I've confirmed that by a simple sample to use MediaPlay in QML. Also, MetaData itself comes from MediaPlayer so it has the same rule.

That's what I've found so far, please correct me if anything's wrong.

 

Best regards,

 

Developer
Posts: 65
Registered: ‎03-20-2013
My Device: Z10(Red Limited Edition)
My Carrier: MY COMPANY

Re: How to get length of an audio file

I'm also facing the same problem.
Can anyone suggest me .

Developer
Posts: 47
Registered: ‎11-23-2010
My Device: Q10
My Carrier: Rogers

Re: How to get length of an audio file

I tried to catch MediaStateChanged signal and check if state is Prepared and then I read duration from MediaPlayer object. Couldn't make it work. Duration was always 0.

Tried your approach with calling "prepare()" after I set sourceUrl and then catch DurationChanged. It works. Thanks.