Chirag Savsani
Chirag Savsani

Reputation: 6142

MediaPlayer plays only 2-3 seconds not full mp3

There is mp3 file which is 20 seconds long.

But MediaPlayer only plays 2-3 seconds.

I am using below code to play mp3 from raw file.

try{
        AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(R.raw.buzzer6);
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
                @Override
                public void onPrepared(MediaPlayer mp){
                      mp.start();
                }
        });
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        mediaPlayer.prepare();
} catch (IOException ioe) {
        ioe.printStackTrace();
}

So what is the issue?

I want to Play full mp3 file which is 20 seconds.

Upvotes: 1

Views: 236

Answers (1)

trocchietto
trocchietto

Reputation: 2617

I suggest you to have a look at this post

Basically if you try to enlarge the scope of MediaPlayer as instance variable it should work

(In the example is declared static, you probably do not need to declare it static)

just change this line:

MediaPlayer mediaPlayer = new MediaPlayer();

in:

private MediaPlayer mediaPlayer //is an instance variable not a local one

while into the method you maintain:

mediaPlayer = new MediaPlayer();

Upvotes: 3

Related Questions