sithmal
sithmal

Reputation: 58

How to Play All Audio file One by One when Click PLAY button 1 time

I have two mp3 files. i want to play all mp3's one by one files when Click "Play" button.. (Thats means after palyed 1st Audio , then need to play 2nd Audio file)

I tried this code. but isn't working

 package com.example.playaudio;

  import android.app.Activity;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;


 public class MainActivity extends Activity implements OnClickListener{
private MediaPlayer mp;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    Button button1=(Button)findViewById(R.id.button_1);
    button1.setOnClickListener(this);
    
}

public void onClick(View v) {
    int x,y ;
    x=R.raw.a;           //Mp3 file 1
    y=R.raw.b;           //Mp3 file 2          

    if (mp != null) {
        mp.release();
    }
    
    int z=0;
    
     for(int i=0; i<10; i++){            
            mp = MediaPlayer.create(this, x);
            mp.start();
         
        }
    }   

@Override
protected void onDestroy() {
    if(null!=mp){
        mp.release();
    }
    super.onDestroy();
}
}

Please tell me solution for this problem

Upvotes: 1

Views: 880

Answers (2)

KillaKem
KillaKem

Reputation: 1025

Use the Mediaplayer's default constructor to get a handle of a Mediaplayer object then add all audio tracks to the playback queue.

MediaPlayer _myPlayer = new MediaPlayer();
myPlayer.setDataSource( myContext , myURI);
myPlayer.start();


MediaPlayer _nextPlayer = new MediaPlayer();
nextPlayer.setDataSource( myContext , myNextURI);
myPlayer.setNextMediaPlayer(nextPlayer);

Upvotes: 1

Machan Max
Machan Max

Reputation: 89

Try to use While loop

    while(x<10){
        //code
     }

Upvotes: 2

Related Questions