如何获得Android的一个MP3文件的持续时间 [英] How to get the duration of an MP3 file in Android

查看:245
本文介绍了如何获得Android的一个MP3文件的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的媒体播放器的项目,我想根据我正在播放MP3文件的长度,旋转图像,即图像应停止当歌曲结束旋转。
我想选择的MP3文件的时间,所以我可以一次轮换。

I am working on a media player project and I want to rotate an image according to the length of the MP3 file that I am playing, i.e. the image should stop rotating when the song is ended. I want to get the duration of the selected MP3 file so I can time the rotation.

我看了这个问题,从Android的MP3文件获取PCM数据但我看不出如何读取歌曲时间。我怎样才能做到这一点是有 - 被任何函数或方法来获取文件的持续时间

I read this question Get PCM data from an MP3 file in Android but I couldn't see how to read the song duration. How can I do this is there - is any function or methods to get the duration of the file?

推荐答案

希望这会帮助你。

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();

这篇关于如何获得Android的一个MP3文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆