2014-02-14 43 views
0

我有以下方法是爲了返回給定原始資源的持續時間長度(以毫秒爲單位)。使用MediaPlayer獲取原始資源的持續時間長度

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 

任何幫助理解。

+0

'MediaPlayer.create (context,R.raw.aboveingredients)'雖然它可能與你的問題無關,你是否應該使用傳遞給你的getD的'id' urationLength'方法? – Michael

+0

當你傳遞'id'作爲參數時,爲什麼你明確地在'getDurationLength'中獲得資源?編輯:邁克爾說什麼...... –

+0

現在忽略變量'id'。這只是爲了測試目的。 – Rhys

回答

1

setText希望你能在一個字符串不是integer

通過獲得string值,以避免錯誤

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

或級聯添加到字符串

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

工作,謝謝! – Rhys