简体   繁体   中英

Android How to get Audio File duration

i made application which use class AudioRecorder to recording files in format pcm. I made list with all files in my app and i want get duration from all files but i have problem. I tried:

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
   metaRetriever.setDataSource(filePath);

and

MediaPlayer mplayer =  new MediaPlayer();
mplayer.reset();
mplayer.setDataSource(URL);
mplayer.prepare();
Integer totalFileDuration = mplayer.getDuration();

I have only errors with this solutions.

From the Android dev docs:

There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after reset() is called. It is a programming error to invoke methods such as getCurrentPosition(), getDuration(), getVideoHeight(), getVideoWidth(), setAudioStreamType(int), setLooping(boolean), setVolume(float, float), pause(), start(), stop(), seekTo(int), prepare() or prepareAsync() in the Idle state for both cases. If any of these methods is called right after a MediaPlayer object is constructed, the user supplied callback method OnErrorListener.onError() won't be called by the internal player engine and the object state remains unchanged; but if these methods are called right after reset(), the user supplied callback method OnErrorListener.onError() will be invoked by the internal player engine and the object will be transfered to the Error state.

Since you're using a URL for the data source, I would try prepareAsync() and then call getDuration() in onPrepared() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM