Search code examples
androidaudioandroid-mediaplayerandroid-resources

Audio not playing in android. android.content.res.Resources$NotFoundException: Resource ID #0x0


I am trying to play an audio file in Android but it seems to crash and give a the ResourcesNotFoundException. The audio file is saved in the raw folder inside res. The name of the audio file is determined by a randomly generated number which is then concatenated to "audio".

int res = getResources().getIdentifier ( "audio"+arrayIndex, "raw","com.din.askak" );
MediaPlayer mPlayer = MediaPlayer.create ( MainActivity.this, res );

In the logcat it seems to throw the exception on the line creating the mediaPlayer. I don't know what I seem to be doing wrong???


Solution

  • Use of getIdentifier() function is discouraged. It is much more efficient to retrieve resources by identifier rather than by name.