7

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.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

4 Answers4

15

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();
Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
Bharat Jyoti
  • 394
  • 2
  • 6
9
private void startSound(String filename){
    AssetFileDescriptor afd = getAssets().openFd(filename);
    MediaPlayer player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    player.prepare();
    player.start();
}
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
6

You can use the following code to play a specific audio file (i.e your_file.mp3) in your assets folder with:

MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getAssets().openFd("your_file.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();

And to play a raw resource file (i.e your_file) with:

MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.your_file));                    
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    You initialized `MediaPlayer mediaPlayer = new MediaPlayer();` but you rather called `player.prepare();`. etc. – Gordons Jun 02 '17 at 09:54
0

enter image description here

start sound

startSound("mp3/ba.mp3");

method

private void startSound(String filename) {
    AssetFileDescriptor afd = null;
    try {
        afd = getResources().getAssets().openFd(filename);
    } catch (IOException e) {
        e.printStackTrace();
    }
    MediaPlayer player = new MediaPlayer();
    try {
        assert afd != null;
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        player.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    player.start();
}
MODERN
  • 121
  • 1
  • 3