How to start media or sound file.How to start media or sound file.

How to start media file in android program.

Answer 1

Use this code to start media file.
  
  MediaPlayer mp = MediaPlayer.create(context,R.raw.audio);
  mp.start(); 

Answer 2

To prevent ANR problem try to start media inside worker thread.
  
Thread workerThread = new Thread(new Runnable() {
  public void run() {
  try {
           MediaPlayer mp = MediaPlayer.create(context,R.raw.audio);
           mp.start();
      } 
     catch (Throwable t) {
     Log.i("StartMediaFile", "Thread  exception "+t);
  }   
     }
 });
 
 workerThread.start();
 

SUBMIT YOUR ANSWER

  |  
 
 
 

Preview :