2016-04-07 76 views
1

我目前正試圖設計一個簡單的應用程序,用於播放音樂播放器。我有這首歌曲的網址,並正在設置MediaPlayer,如下所示。一切都在第一首歌曲OK,但是當我玩的其他歌曲,仿真時,程序不會崩潰,但沒有什麼是玩,我得到以下錯誤:MediaPlayer錯誤代碼(-38,0)getDuration

E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer 
E/MediaPlayer: error (-38, 0) 
E/MediaPlayer: Error (-38,0) 

PlayerService.java

public class MusicPlayerService extends Service { 
    private SongItem mSong; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     mSong = ListSong.getListSong().get(ListSong.getSelectedItem()); 
     registerLocalFilter(); 
     playMedia(); 
     return START_STICKY; 
    } 

    private void playMedia() { 
     if (handler == null) { 
      thread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        Looper.prepare(); 
        handler = new Handler(); 
        handler.post(playAudioFromURL); 
        Looper.loop(); 
       } 
      }); 
      thread.start(); 
     } else { 
      handler.post(playAudioFromURL); 
     } 
    } 

    private Runnable playAudioFromURL = new Runnable() { 
     @Override 
     public void run() { 
      if (mediaPlayer != null) { 
       mediaPlayer.release(); 
      } 
      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      try { 
       mediaPlayer.setDataSource(mSong.getSongUrl()); 
       mediaPlayer.setOnPreparedListener(onPreparedListener); 
       mediaPlayer.setOnCompletionListener(onCompletionListener); 
       mediaPlayer.setOnSeekCompleteListener(onSeekCompleteListener); 
       mediaPlayer.setOnErrorListener(onErrorListener); 
       mediaPlayer.prepareAsync(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.w(TAG, "playAudioFromURL: " + e.toString()); 
      } catch (IllegalArgumentException ex) { 
       Log.w(TAG, "playAudioFromURL: " + ex.toString()); 
      } catch (IllegalStateException ex) { 
       Log.w(TAG, "playAudioFromURL: " + ex.toString()); 
      } 
     } 
    }; 


    private MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      ListSong.setPlaying(true); 
      sendLoadComplete(); 
      updateTimer(); 
      mp.start(); 
     } 
    }; 
} 

我在onPrepared()mp.start()Media Player called in state 0, error (-38,0)使用mediaPlayer.prepareAsync();樣的解決方案,但它不工作。

請幫幫我!

回答

0

之前mediaPlayer.setDataSource(mSong.getSongUrl());在MediaPlayer mediaPlayer.reset();它可能會解決您的問題。

+0

**可能**?那麼它應該在評論老兄! – Gattsu