我嘗試播放流媒體音樂背景和,我用下面的音樂服務類:Android的MediaPlayer的準備失敗:狀態=與流媒體音樂爲0x1
public class MusicService extends Service implements
OnErrorListener, MediaPlayer.OnCompletionListener {
private static final int NOTIFICATION_ID = 1;
private final IBinder mBinder = new ServiceBinder();
MediaPlayer mPlayer;
private int length = 0;
private Notification mNotification;
public MusicService() {
}
@Override
public void onCompletion(MediaPlayer mp) {
mPlayer.stop();
}
public class ServiceBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
if (mPlayer != null) {
mPlayer.setLooping(true);
mPlayer.setVolume(100, 100);
}
mPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
onError(mPlayer, what, extra);
return true;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// mPlayer.start();
return START_STICKY;
}
public void startMusic(String path) {
Toast.makeText(this, "pause music", Toast.LENGTH_SHORT).show();
if (mPlayer.isPlaying()) {
mPlayer.release();
}
try {
SongController s = (SongController) DBControllerFactory.getController("queue", this);
Log.d("MusicService", Config.SERVICE_ROOT_URL + "uploads/" + ((Song) s.select()[s.getTotalRows() - 1]).getFile() + ".mp3");
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(Config.SERVICE_ROOT_URL + "uploads/" + ((Song) s.select()[s.getTotalRows() - 1]).getFile() + ".mp3");
mPlayer.prepare(); /////error at here
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseMusic() {
Toast.makeText(this, "pause music", Toast.LENGTH_SHORT).show();
if (mPlayer.isPlaying()) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
}
public void resumeMusic() {
Toast.makeText(this, "resume music", Toast.LENGTH_SHORT).show();
if (mPlayer.isPlaying() == false) {
mPlayer.seekTo(length);
mPlayer.start();
}
}
public void stopMusic() {
//setUpAsForeground("test");
Toast.makeText(this, "stop music", Toast.LENGTH_SHORT).show();
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
}
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
return false;
}
void setUpAsForeground(String text) {
Intent openIntent = new Intent(getApplicationContext(), MainScreenActivity.class);
openIntent.putExtra("action", "opendrawer");
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
0,
openIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mNotification = new Notification();
mNotification.tickerText = text;
mNotification.icon = R.drawable.ic_launcher;
mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotification.setLatestEventInfo(getApplicationContext(),
getResources().getString(R.string.app_name), text, pi);
startForeground(NOTIFICATION_ID, mNotification);
}
}
當第一次的音樂播放時,它工作正常,但第二次它給了以下錯誤:
java.io.IOException: Prepare failed.: status=0x1
W/System.err﹕ at android.media.MediaPlayer._prepare(Native Method)
W/System.err﹕ at android.media.MediaPlayer.prepare(MediaPlayer.java:1125)
......
,但如果我刪除設備我的應用程序的緩存它再次工作正常。所以我認爲這是與緩存有關的問題,但我無法確定問題誰能幫助我嗎?
Thaks回覆...!我已經嘗試過記錄並獲取正確的源路徑,正如我第一次播放它時所說的那樣,但在此之後它不起作用,即使我關閉了應用程序並重新開始了應用程序,但它在我刪除應用程序緩存時仍然有效。 – vedraiyni 2015-03-19 14:29:53
藉助你在這裏的小解釋,我猜想有一個媒體播放器沒有獲得釋放的問題。或者如果它確實如此,我認爲它應該使用相同的音頻源文件再次創建。 所以現在你需要做一些工作。 1.您必須發佈,重置並銷燬mediaplayer實例 2.使用源重新創建mediaplayer實例 3.再次啓動 – 2015-03-20 05:56:34
問題:正如您所說的那樣,即使關閉應用程序並關注應用程序,它也不起作用再次,但它的作品,如果我刪除應用程序的緩存「,,,, 記住,你在服務工作,所以你必須明確地銷燬它0r停止並再次啓動它。因爲在其他情況下,服務將繼續運行,您的onDestroy方法是不會打電話 – 2015-03-20 06:01:13