资源简介
音乐播放工具类。
代码片段和文件信息
package com.oxueba.student.module.subject.utils;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import com.oxueba.student.util.Utils2Log;
import com.oxueba.student.util.Utils2Toast;
/**
* @author 赵蒙召
* 音频播放工具类
*/
public class AudioUtils {
public static MediaPlayer mMediaPlayer;
public static OnCompleteListener mOnCompleteListener;
private static String mAudioUrl;
public interface OnCompleteListener {
void onComplete(MediaPlayer mp);
}
/**
* 打开音频文件播放
*/
public static void openAudioFile(Context context String path OnCompleteListener onCompleteListener) {
mOnCompleteListener = onCompleteListener;
mAudioUrl = path;
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path); // 设置数据源
mMediaPlayer.prepare(); // prepare自动播放
} catch (Exception e) {
Utils2Toast.showToast(“播放失败!“);
Utils2Log.log_e(“网络视频播放失败----“ e.getMessage());
}
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mOnCompleteListener.onComplete(mp);
}
});
startAudio();
}
/**
* 获取当前播放状态
*/
public static boolean getIsPlayer() {
try {
if (null != mMediaPlayer) {
return mMediaPlayer.isPlaying();
}
} catch (Exception e) {
Utils2Log.log_e(“播放器异常-------“
评论
共有 条评论