使用MediaPlayer获取原始资源的持续时间长度

时间:2014-02-14 19:12:53

标签: java android

我有以下方法,用于返回给定原始资源的持续时间长度(以毫秒为单位)。

private int getDurationLength(int id) {
    MediaPlayer mp = MediaPlayer.create(context, R.raw.aboveingredients);
    return mp.getDuration();
}

在我的原始文件夹中,我有一个名为aboveingredients.m4a的原始资源,如下图所示。

Raw folder with m4a file

执行以下代码时:

audioDuration.setText(getDurationLength(R.raw.aboveingredients));

我在Logcat中收到以下错误:

android.content.res.Resources$NotFoundException: String resource ID

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

setText希望您传入的字符串不是integer

获取string值以避免错误

int soundLength = getDurationLength(R.raw.aboveingredients);
audioDuration.setText(String.valueOf(soundLength));

或将串联添加到字符串

 audioDuration.setText("soundLength = " + soundLength);