import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.widget.SeekBar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by Rahul-PC on 28-02-2017.
*/
public class ApplozicAudioManager implements MediaPlayer.OnPreparedListener {
private static ApplozicAudioManager myObj;
public ApplozicDocumentView currentView;
Map<String, MediaPlayer> pool = new HashMap<>();
Context context;
int maxsize = 5;
String TAG = "ApplozicAudioManager";
String audio_duration;
int hours, minute, second, duration;
private ApplozicAudioManager() {
}
public static ApplozicAudioManager getInstance() {
try {
if (myObj == null) {
myObj = new ApplozicAudioManager();
}
return myObj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
void play(Uri uri, final ApplozicDocumentView view) {
final String key = view.message.getKeyString();
MediaPlayer mp = pool.get(key);
if (mp != null) {
if (mp.isPlaying()) {
mp.pause();
return;
} else {
mp.seekTo(mp.getCurrentPosition());
mp.start();
}
} else {
mp = new MediaPlayer();
if (pool.size() >= maxsize) {
Map.Entry<String, MediaPlayer> entry = pool.entrySet().iterator().next();
String first = entry.getKey();
pool.remove(first);
}
pool.put(key, mp);
}
pauseOthersifPlaying();
try {
if (context != null && context.getContentResolver() !=null) {
mp.setDataSource(context, uri);
mp.prepare();
}
} catch (Exception e) {
e.printStackTrace();
}
mp.start();
if (currentView != null) {
currentView.setAudioIcons();
}
this.currentView = view;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
pool.remove(key);
currentView.setAudioIcons();
}
});
currentView.audioseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
getMediaPlayer(key).seekTo(progress);
}
}
});
}
public void pauseOthersifPlaying() {
MediaPlayer m;
Iterator it = pool.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
{
m = (MediaPlayer) pair.getValue();
if (m.isPlaying()) {
m.pause();
}
}
}
}
int getAudioState(String key) {
MediaPlayer mp = pool.get(key);
if (mp != null) {
if (mp.isPlaying()) {
return 1;
}
return 0;
}
return -1;
}
public MediaPlayer getMediaPlayer(String key) {
return pool.get(key);
}
public void audiostop() {
MediaPlayer temp;
Iterator it = pool.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
{
temp = (MediaPlayer) pair.getValue();
temp.stop();
temp.release();
}
}
pool.clear();
}
public String refreshAudioDuration(String filePath) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
duration = mediaPlayer.getDuration();
duration = duration / 1000;
hours = duration / 3600;
minute = duration / 60;
second = duration % 60;
mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
audio_duration = hours + "" + minute + ":" + second;
return audio_duration;
}
}
我在android 7中得到以下错误我的代码在android 6或更早版本上工作正常 提出任何解决方案 如果我将Activity中的Media Player对象初始化为静态并进一步在服务类中使用,那么它会显示此错误(-38,0),尝试在错误状态下执行seekTo:mPlayer = 0x0,mCurrentState = 0,在状态0中调用start
E/MediaPlayer: Attempt to call getDuration in wrong state: mPlayer=0x0, mCurren`enter code here`tState=1
03-15 18:27:19.695 2020-2020/com.mobicomkit.sample E/MediaPlayer: error (-38, 0)
03-15 18:27:19.711 2020-2020/com.mobicomkit.sample E/MediaPlayer: Error (-38,0)
03-15 18:27:19.713 2020-2020/com.mobicomkit.sample I/statee:: -1