Mode one: Use Contentresolver
The contentresolver of the system provides us with the method of querying the Media Library, and the information of some media files, such as name, file path, length of play, etc. are kept in the media library.
Uri Contenturi =Media.external_content_uri; string[] Columns= {mediastore.audio.media._id,//Song IDMediaStore.Audio.Media.DURATION,//Total playback duration of the song}; Contentresolver Mresolver=Context.getcontentresolver (); String selection= MediaStore.Audio.Media.DATA + "=?"; String selectionargs[]={path}; Cursor Cursor= Mresolver.query (Contenturi,NULL, Selection, Selectionargs,NULL);if(Cursor! =NULL&&Cursor.movetofirst ()) { intDuration =Cursor.getint (Cursor.getcolumnindex (media.duration)); Cursor.close ();}
Mode two: Use Mediametadataretriever
MediaMetadataRetrieverclass provides a unified interface for getting frames and metadata (meta data) from a media file.
New// before getting, set file path (should only be local path)String duration = // free if (! textutils.isempty (Duration)) { long dur = long.parselong (duration);}
In addition, the Mediametadataretriever class provides a unified interface for getting frames and metadata (meta data) from a media file. If you get the video at a certain point in time getFrameAtTime(long timeUs) , this method returns an Bitmap object.
Android Get video Voice file length of time