2014-09-10 183 views
2

嗨,我每寫一個代碼播放FM無線電波。在我的代碼中有一個進度條和兩個按鈕,其中一個用於播放,另一個用於停止。當我點擊播放按鈕時,它不會發生。我怎麼解決這個問題?或者有沒有好的建議我播放音頻m3u8文件?
我正在使用的代碼是:Android MediaPlayer播放m3u8只有音頻流不能正常工作

public class Main extends Activity implements OnClickListener { 

    private ProgressBar playSeekBar; 

    private Button buttonPlay; 

    private Button buttonStopPlay; 

    private MediaPlayer player; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     initializeUIElements(); 

     initializeMediaPlayer(); 

    } 

    private void initializeUIElements() { 

     playSeekBar = (ProgressBar) findViewById(R.id.progressBar1); 
     playSeekBar.setMax(100); 
     playSeekBar.setVisibility(View.INVISIBLE); 

     buttonPlay = (Button) findViewById(R.id.buttonPlay); 
     buttonPlay.setOnClickListener(this); 

     buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay); 
     buttonStopPlay.setEnabled(false); 
     buttonStopPlay.setOnClickListener(this); 

    } 

    public void onClick(View v) { 
     if (v == buttonPlay) { 
      startPlaying(); 
     } else if (v == buttonStopPlay) { 
      stopPlaying(); 
     } 
    } 

    private void startPlaying() { 
     buttonStopPlay.setEnabled(true); 
     buttonPlay.setEnabled(false); 

     playSeekBar.setVisibility(View.VISIBLE); 

     player.prepareAsync(); 

     player.setOnPreparedListener(new OnPreparedListener() { 

      public void onPrepared(MediaPlayer mp) { 
       player.start(); 
      } 
     }); 

    } 

    private void stopPlaying() { 
     if (player.isPlaying()) { 
      player.stop(); 
      player.release(); 
      initializeMediaPlayer(); 
     } 

     buttonPlay.setEnabled(true); 
     buttonStopPlay.setEnabled(false); 
     playSeekBar.setVisibility(View.INVISIBLE); 
    } 

    private void initializeMediaPlayer() { 
     player = new MediaPlayer(); 
     try { 

      player.setDataSource("http://url.m3u8"); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() { 

      public void onBufferingUpdate(MediaPlayer mp, int percent) { 
       playSeekBar.setSecondaryProgress(percent); 
       Log.i("Buffering", "" + percent); 
      } 
     }); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (player.isPlaying()) { 
      player.stop(); 
     } 
    } 
} 

回答

0

嘗試調用prepareAsync前setOnPreparedListener()()如果你沒有得到onPrepared()回調。

+0

坦克的要求,實際上當我改變.m3u8 url到.mp3 url它工作正常。但對於.m3u8,它不會播放任何內容 – 2014-09-10 16:21:23

1

我通過vitamio庫解決了這個問題。謝謝

相關問題