我有一个音乐播放器应用程序,它削减轨道的哪一部分应该播放。我的问题是,我如何播放下一首歌曲,即使当前的媒体播放器轨道还没有达到歌曲的结尾。我还尝试调用onCompletion:
mplayer.stop();
Log.v("PLAYER", "Audio track Stopped");
checkTrackStatus();
songHandler.removeCallbacks(uxUpdater);
if(fis!=null)fis=null;
if((currentTrack+1) < songs.size())
{
Log.v("Move to next track", songs.get(currentTrack+1).get("songTitle"));
//mplayer.release();
mplayer=null;
currentTrack++;
playSong(currentTrack);
}else
Toast.makeText(getApplication(), "All tracks played", Toast.LENGTH_LONG).show();
播放方法:
public void playSong(final int songIndex)
{
File toPlay = new File(songs.get(songIndex).get("songPath"));
lblTrackText.setText("Playing ["+(songIndex+1)+"/"+songs.size()+"]");
lblTrackTitle.setText(songs.get(songIndex).get("songTitle"));
//lblTrackTitle.setSelected(true);
Log.v("PLAY SONG : ", toPlay.getPath());
if(toPlay.exists())
{
//playAudio(toPlay);
try{
mplayer = new MediaPlayer(this);
//mplayer.setOnCompletionListener(this);
fis=new FileInputStream(new File(songs.get(songIndex).get("songPath"))); //ORIGINAL 'p'
mplayer.setDataSource(fis.getFD());//need to add permission in manifest to play .m4a files "ACCESS_NETWORK_STATE"
mplayer.prepare();
//final String s =p;
mplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//getTotalTrackDuration
@Override
public void onPrepared(MediaPlayer mp) {
mplayer.seekTo(Integer.parseInt(songs.get(songIndex).get("songStart")));
mplayer.start();
//lblTotal.setText(timeConverter(mplayer.getDuration()));
setTrackNo();
//Log.v("NOW PLAYING ", s);
updateProgress();
}
});
} catch (Exception e) {
Log.e("AUDIO PLAYER", "error: " + e.getMessage(), e);
}
}
else
Toast.makeText(getApplication(), songs.get(songIndex).get("songTitle")+" does not exist or is already deleted", Toast.LENGTH_LONG).show();
}
我总是得到一个错误说
错误(-541478725)致命信号11 (SIGSEGV)在0x00002ad8 (code=1)
我用的是vitamio media player。
您得到的错误表明本机代码中存在错误,该错误一定存在于vitamino媒体播放器中。你收到的错误似乎是一个内存错误,这让我认为资源没有被释放,设备内存不足。
尝试添加一个呼叫:
fis.close()
在你的代码前:
if(fis!=null)fis=null;
以确保您正确地关闭上一首歌曲的流。
从这个答案你的问题也可能是你是loading a lot of images and sounds and causing the app to run out of memory
。似乎不同的设备和不同版本的android处理方式也不同,这意味着该应用程序可以在一部手机上运行,但不能在另一部手机上运行。