2017-02-15 166 views
0

我試圖通過媒體播放器播放實況流mp3音頻。 問題是,當我使用URL時,它阻塞了UI線程一段時間,然後它工作正常。但我需要它也會播放帶有緩衝的歌曲。媒體播放器使用緩衝區播放歌曲

在此先感謝。請幫助。

try { 
      audioPlayer = new MediaPlayer(); 
      audioPlayer.setOnBufferingUpdateListener(this); 
      audioPlayer.setOnCompletionListener(this); 
      audioPlayer.setAudioStreamType(useFrontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC); 

     try { 
      audioPlayer.reset(); 
      audioPlayer.setDataSource(mSongDetail.getPath()); 
      audioPlayer.prepare(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     audioDuration = audioPlayer.getDuration(); 
     audioPlayer.start(); 

     startProgressTimer(); 
    } catch (Exception e) { 
     if (audioPlayer != null) { 
      audioPlayer.reset(); 
      audioPlayer.release(); 
      audioPlayer = null; 
      isPaused = false; 
      MusicPreferance.playingSongDetail = null; 
     } 
     return false; 
    } 
+0

發佈程序相關的問題在這裏..不是一般的問題...發佈你的代碼...你嘗試過的東西?? – Akshay

+0

我已添加代碼 – priyanka

回答

0

嘗試爲它使用audioPlayer.prepareAsync()+ asyncHandler,或使用單獨的線程。