package com.android.game;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
public class AudioClip {
private static final String TAG = "AudioClip";
private MediaPlayer mPlayer;
private String name;
private boolean mPlaying = false;
private boolean mLoop = false;
public AudioClip(Context context, int resId) {
name = context.getResources().getResourceName(resId);
mPlayer = MediaPlayer.create(context, resId);
mPlayer.setVolume(1000, 1000);
mPlayer.setOnCompletionListener(
new MediaPlayer .OnCompletionListener() {
@Override
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) {
Log.e(TAG, "stop()--err--" + e.toString());
}
}
public synchronized void loop() {
mLoop = true;
mPlaying = true;
mPlayer.start();
}
public void release() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}