Ted pottel
Ted pottel

Reputation: 6983

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.

Upvotes: 7

Views: 20624

Answers (4)

MODERN
MODERN

Reputation: 121

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();
}

Upvotes: 0

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

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();

Upvotes: 6

Bharat Jyoti
Bharat Jyoti

Reputation: 394

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();

Upvotes: 15

Charlie-Blake
Charlie-Blake

Reputation: 11050

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();
}

Upvotes: 9

Related Questions