Rhys
Rhys

Reputation: 2173

Get duration length of a raw resource using MediaPlayer

I have the following method which is meant to return the duration length (in ms) of a given raw resource.

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

In my raw folder, I have a raw resource called aboveingredients.m4a as shown in the image below.

Raw folder with m4a file

When the following code is executed:

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

I receive the following error in Logcat:

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

Any help is appreciated.

Upvotes: 1

Views: 1161

Answers (1)

meda
meda

Reputation: 45490

setText expect you to pass in a String not an integer

get the string value to avoid the error

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

or add concatenation to a string

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

Upvotes: 1

Related Questions