124 lines
2.8 KiB
Java
124 lines
2.8 KiB
Java
package com.coolape.tianrun;
|
|
|
|
import android.content.Context;
|
|
import android.media.MediaPlayer;
|
|
import android.media.MediaPlayer.OnCompletionListener;
|
|
import android.media.MediaPlayer.OnPreparedListener;
|
|
import android.media.MediaPlayer.OnErrorListener;
|
|
import android.media.MediaPlayer.OnSeekCompleteListener;
|
|
import android.net.Uri;
|
|
|
|
public class MyMediaPlayer {
|
|
public static volatile MediaPlayer player = null;
|
|
static String onPrepareOrgs = "";
|
|
|
|
public void prepare(Context context, String audioSource, String orgs) {
|
|
onPrepareOrgs = orgs;
|
|
try {
|
|
Uri uri = Uri.parse(audioSource);
|
|
if (player == null) {
|
|
player = MediaPlayer.create(context, uri);
|
|
player.setLooping(false);
|
|
} else {
|
|
if (player.isPlaying()) {
|
|
stop();
|
|
}
|
|
player.reset();
|
|
if (audioSource.startsWith("http")) {
|
|
player.setDataSource(context, uri);
|
|
} else {
|
|
player.setDataSource(audioSource);
|
|
}
|
|
player.prepareAsync();
|
|
}
|
|
player.setOnPreparedListener(new OnPreparedListener() {
|
|
@Override
|
|
public void onPrepared(MediaPlayer player) {
|
|
player.setLooping(false);
|
|
int len = player.getDuration();
|
|
U3dPlugin.UnitySendMessage("onMediaPrepared", len + "",
|
|
onPrepareOrgs);
|
|
}
|
|
});
|
|
player.setOnCompletionListener(new OnCompletionListener() {
|
|
|
|
@Override
|
|
public void onCompletion(MediaPlayer player) {
|
|
pause();
|
|
U3dPlugin.UnitySendMessage("onMediaComplet", "true",
|
|
onPrepareOrgs);
|
|
}
|
|
});
|
|
player.setOnErrorListener(new OnErrorListener() {
|
|
|
|
@Override
|
|
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
|
|
U3dPlugin.UnitySendMessage("onMediaError", "false",
|
|
onPrepareOrgs);
|
|
return false;
|
|
}
|
|
});
|
|
player.setOnSeekCompleteListener(new OnSeekCompleteListener() {
|
|
|
|
@Override
|
|
public void onSeekComplete(MediaPlayer arg0) {
|
|
// TODO Auto-generated method stub
|
|
U3dPlugin.UnitySendMessage("onMediaSeek", "true",
|
|
onPrepareOrgs);
|
|
}
|
|
});
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public int getProgress() {
|
|
if (player != null) {
|
|
return player.getCurrentPosition();
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public void pause() {
|
|
if (player != null && player.isPlaying()){
|
|
player.pause();
|
|
}
|
|
}
|
|
|
|
public void play() {
|
|
if (player != null && !player.isPlaying()){
|
|
player.start();
|
|
}
|
|
}
|
|
|
|
public void stop() {
|
|
if (player != null)
|
|
return;
|
|
if (player.isPlaying()) {
|
|
player.stop();
|
|
}
|
|
try {
|
|
// mp.prepareAsync();
|
|
// player.prepare();
|
|
player.reset();
|
|
// player.seekTo(0);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void seek(int progress) {
|
|
if (player != null)
|
|
player.seekTo(progress);
|
|
}
|
|
|
|
public void destroy() {
|
|
if (player != null) {
|
|
player.stop();
|
|
player.release();
|
|
}
|
|
player = null;
|
|
}
|
|
}
|