get duration of audio file

后端 未结 10 1163
生来不讨喜
生来不讨喜 2020-12-07 19:15

I have made a voice recorder app, and I want to show the duration of the recordings in a listview. I save the recordings like this:

MediaRecorder recorder =          


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 19:40

    MediaMetadataRetriever is a lightweight and efficient way to do this. MediaPlayer is too heavy and could arise performance issue in high performance environment like scrolling, paging, listing, etc.

    Furthermore, Error (100,0) could happen on MediaPlayer since it's a heavy and sometimes restart needs to be done again and again.

    Uri uri = Uri.parse(pathStr);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(AppContext.getAppContext(),uri);
    String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    int millSecond = Integer.parseInt(durationStr);
    

提交回复
热议问题