我的Android Media Player随机停止播放。我不知道为什么。我使用两个媒体播放器进行无间隙循环。过了一会儿,声音停止了。问题在几秒钟或几分钟后出现。我尝试了几个修复:
他们都没有解决我的问题。有什么建议? THX!
代码:
private Context context;
private Handler handler;
private Runnable myRunnable;
/*Ausgabe*/
private ImageView cover;
private static MediaPlayer mp;
private static MediaPlayer mp2;
private Uri myUri;
private String curTitle;
private String curPow;
private boolean curMP; //false=mp1, true= mp2
/*Control*/
private ToggleButton onoff;
private Spinner spinner;
private Switch throttle;
/*Volume*/
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jukebox);
context = this;
/*Control-Setup*/
initControls();
initButtons();
/*Sound Setup*/
mp = new MediaPlayer();
mp2 = new MediaPlayer(); //workarround f gapless looping
setDisc("classic");
/*Image Setup*/
cover = (ImageView) findViewById(R.id.imageGallerie);
cover.setImageResource(R.drawable.classic);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mp!=null){
mp.stop();
mp.release();
mp = null;
}
if(mp2!=null){
mp2.stop();
mp2.release();
mp2=null;
}
}
/*sound-file selection*/
public void setDisc(String s){
String tmp = "";
if(throttle.isEnabled()==true && throttle.isChecked())
tmp="2";
myUri = getUri(s+tmp);
curTitle=s;
curPow=tmp;
try {
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(context, myUri);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mp.prepare();
mp2.reset();
mp2.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp2.setDataSource(context, myUri);
mp2.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mp2.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
setMyLoop();
play();
}
});
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
// Arrr, devensive programming... change request
Toast.makeText(context, getResources().getString(R.string.error3), Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
e.printStackTrace();
Toast.makeText(context, getResources().getString(R.string.error3), Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
e.printStackTrace();
Toast.makeText(context, getResources().getString(R.string.error3), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(context, getResources().getString(R.string.error3), Toast.LENGTH_LONG).show();
}
}
/*Gapless Looping - api 16 oder higher */
public void setMyLoop(){
final AssetFileDescriptor afd = getResources().openRawResourceFd(getRawId(this, curTitle.concat(curPow)));
mp.setNextMediaPlayer(mp2);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.prepare();
} catch (Exception e) {
Toast.makeText(context, getResources().getString(R.string.error1), Toast.LENGTH_LONG).show();
}
mp2.setNextMediaPlayer(mp);
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.prepare();
} catch (Exception e) {
Toast.makeText(context, getResources().getString(R.string.error2), Toast.LENGTH_LONG).show();
}
mp.setNextMediaPlayer(mp2);
}
});
}
/*play sound*/
public void play(){
if(onoff.isChecked()){
mp.start();
}
}
/*gui control*/
@Override
public void onClick(View v) {
if(v == onoff){
if(onoff.isChecked()==false){
mpAnhalten();
}
else{
mpStarten();
}
}
...
}
@Override
public void onPause(){
super.onPause();
if(mp!=null)
mpAnhalten();
}
/*pause player*/
public void mpAnhalten(){
if(mp2.isPlaying()){
mp2.pause();
curMP=true;
}
else {
if(mp.isPlaying()){
mp.pause();
}
curMP=false;
}
}
/*start player again*/
public void mpStarten(){
if(curMP==true)
mp2.start();
else
mp.start();
}
@Override
public void onResume(){
super.onResume();
View v = findViewById(R.id.LinearLayout1);
if(mp!=null && onoff.isChecked())
mpStarten();
}
/*volume*/
private void initControls(){
try{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
getApplicationContext();
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error1), Toast.LENGTH_LONG).show();
}
}
/*gui-control sound-file selection*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = parent.getItemAtPosition(position).toString();
if(selection.equals("Classic")){
cover.setImageResource(getImageId(this, "classic"));
cover.invalidate();
throttle.setEnabled(true);
setDisc("classic");
}
...
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
...
/*oncreate submethode*/
private void initButtons(){
/*On Off*/
onoff = (ToggleButton) findViewById(R.id.onoff);
onoff.setChecked(false);
onoff.setOnClickListener(this);
/*track selection*/
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
List<String> list = new ArrayList<String>();
list.add("Modern");
...
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
/*power*/
throttle = (Switch) findViewById(R.id.throttle);
throttle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setDisc(curTitle);
}
});
throttle.setChecked(false);
}
编辑:
这是日志输出:
07-26 16:07:58.707:W / SoftVorbis(191):vorbis_dsp_synthesis返回-135
07-26 16:07:58.779:E / NuPlayerDecoder(191):OMX.google.vorbis.decoder的流错误(错误= -1007),EOS已成功排队
07-26 16:07:58.780:E / NuPlayer(191):从音频解码器收到错误(0xfffffc11),刷新(0),现在正在关闭
07-26 16:07:58.780:E / MediaPlayer(30858):错误(1,-1007)
07-26 16:07:58.781:E / MediaPlayer(30858):错误(1,-1007)
我不知道出了什么问题。