Ted pottel Ted pottel - 5 days ago 5
Android Question

Can you play a mp3 file from the assets folder?

I store all my sounds in the res folder. I'm wondering if there is a way to store them in the assets folder and play them from there?

This is the code I'm using now:

void StartSound(int id) {
if (id==0)
return;

AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// float volume = actualVolume / maxVolume;
float volume=(float) ( .01*(double)cGlobals.iVol);

// Is the sound loaded already?
if (nLastSound!=0)
soundPool.stop(nLastSound);

int t=soundPool.play(id, volume, volume, 1, 0, 1f);
nLastSound=t;
}


I want to change this to avoid a memory error when the APK tries to load. If I remove some of the files from the res folder it works fine. I'm hopping I want have the same issue if they are saved as a file.

Answer

You can do it in another way too. Put the .mp3 files under res/Raw folder and use the following code:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.android);
mediaPlayer.start();
Comments