Android获取声音长度

代码

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

String out = "";
   // get mp3 info

  // convert duration to minute:seconds
String duration =  
 metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Log.v("time", duration);
long dur = Long.parseLong(duration);
String seconds = String.valueOf((dur % 60000) / 1000);

Log.v("seconds", seconds);
String minutes = String.valueOf(dur / 60000);
out = minutes + ":" + seconds;
if (seconds.length() == 1) {
    txtTime.setText("0" + minutes + ":0" + seconds);
}else {
    txtTime.setText("0" + minutes + ":" + seconds);
}
Log.v("minutes", minutes);
  // close object
   metaRetriever.release();

参考地址

http://stackoverflow.com/questions/9834697/how-to-get-the-duration-of-an-mp3-file-in-android?rq=1

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。