AudioClip : Audio « Media « Android






AudioClip

   

import android.content.Context;
import android.media.MediaPlayer;

public class AudioClip {
  private MediaPlayer mPlayer;
  private String name;
  
  private boolean mPlaying   = false;
  private boolean mLoop    = false;
  public AudioClip(Context ctx, int resID) {
    name = ctx.getResources().getResourceName(resID);
    mPlayer = MediaPlayer.create(ctx, resID);
    mPlayer.setVolume(1000, 1000);
    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
      public void onCompletion(MediaPlayer mp) {
        mPlaying = false;
        if(mLoop)
          mp.start();
        
      }
    });
  }
  public synchronized void play(){
    if(mPlaying)
      return;
    
    if(mPlayer != null){
      mPlaying = true;
      mPlayer.start();
    }
  }
  
  public synchronized void stop(){
    try{
      mLoop = false;
      if(mPlaying){
        mPlaying = false;
        mPlayer.pause();
      }
    }catch(Exception e){
      System.err.println("AudioClip::stop "+name + " " + e.toString());
    }
  }
  
  public synchronized void loop () {
    mLoop = true;
    mPlaying = true;
    mPlayer.start();    
    
  }
  
  public void release () {
    if(mPlayer != null){
      mPlayer.release();
      mPlayer = null;
    }
  }

}

   
    
    
  








Related examples in the same category

1.Record audio to 3gpp file
2.Audio Recording
3.Audio recoding and call back
4.Using BackgroundAudioServiceBinder
5.Custom Audio Recorder
6.Set Audio source and format
7.Audio processing library
8.Audio Synthesis
9.Audio Synthesis 440 Hz
10.Audio Files Filter
11.Recording Audio
12.Random Media Player