2011-08-29 71 views
7

我在制定適當的流媒體視頻播放器的工作了幾個小時的Android以及創建一個播放器,可以播放含量少的如歌曲,拖車等很細的相當成功。但是,由於播放器需要大量流媒體,因此播放器會顯示一些不尋常的行爲,例如電影和電視節目等,因此播放器開始滯後於此類數據。任何人都可以幫助我解決這個問題。如何爲大型內容創建流式視頻播放器?

在此先感謝...

這裏是源:

public class player extends Activity implements OnErrorListener, 
    OnPreparedListener { 



/** Called when the activity is first created. */ 
private static final int UPDATE_FREQUENCY = 500; 

private static final int STEP_VALUE = 4000; 

private static final int DIALOG_KEY = 0; 

private TextView currentTime, duration; 

private VideoView videoView; 

private SeekBar seekbar = null; 

private View mediacontroller; 

private ProgressDialog progressDialog = null; 

private ImageButton playButton = null; 

private ImageButton prevButton = null; 

private ImageButton nextButton = null; 

private boolean isMoveingSeekBar = false; 

private boolean isMediaCtrlShown = false; 

private final Handler handler = new Handler(); 

private boolean isStarted = true; 

private String currentFile = "singham_320b"; 

private boolean isCustomSeekButtonClick = false; 

private boolean isPauseButtonClick = false; 

private static boolean isMyDialogShowing = false; 

private static int percentageBuffer = 0; 

private int mpCurrentPosition; 

int hh = 00, mm = 00, ss = 00, ms = 00; 

int i = 0; 

int previouPosition = 0; 

private Runnable onEverySecond=new Runnable() { 
     public void run() { 
       if (videoView!=null) { 
         seekbar.setProgress(videoView.getCurrentPosition()); 
       } 

       if (!isPauseButtonClick) { 
         mediacontroller.postDelayed(onEverySecond, 1000); 
       } 
     } 
}; 

private final Runnable updatePositionRunnable = new Runnable() 
{ 
    public void run() 
    { 
     updatePosition(); 
    } 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    setUpMyDialog(); 
    showMyDialog(); 

    videoView = (VideoView) findViewById(R.id.videoview); 

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    seekbar = (SeekBar) findViewById(R.id.seekbar); 

    currentTime = (TextView) findViewById(R.id.currentTime); 

    playButton = (ImageButton) findViewById(R.id.play); 

    prevButton = (ImageButton) findViewById(R.id.prev); 

    nextButton = (ImageButton) findViewById(R.id.next); 

    duration = (TextView) findViewById(R.id.duration); 

    mediacontroller = findViewById(R.id.mediacontroller); 


    videoView.setOnErrorListener(this); 

    videoView.setOnPreparedListener(this); 

    videoView.setOnTouchListener(new View.OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 

      if (!isMediaCtrlShown) 
      { 
       mediacontroller.setVisibility(View.GONE); 

       isMediaCtrlShown = true; 

      } 
      else 
      { 
       mediacontroller.setVisibility(View.VISIBLE); 

       isMediaCtrlShown = false; 
      } 
      return false; 
     } 
    }); 

    Uri video = Uri.parse("http://wpc.1B42.edgecastcdn.net/001B42/mobile/songs/pyaar_ka_punchnama/life_sahi_hai_320b.mp4"); 

    videoView.setVideoURI(video); 

    seekbar.setOnSeekBarChangeListener(seekBarChanged); 

    playButton.setOnClickListener(onButtonClick); 

    nextButton.setOnClickListener(onButtonClick); 

    prevButton.setOnClickListener(onButtonClick); 

} 

@Override 
public boolean onError(MediaPlayer mp, int what, int extra) { 
    // TODO Auto-generated method stub 
    return false; 
} 

public void calculateTime(int ms) { 

    ss = ms/1000; 

    mm = ss/60; 

    ss %= 60; 

    hh = mm/60; 

    mm %= 60; 

    hh %= 24; 

} 

@Override 
public void onPrepared(MediaPlayer mp) 
{ 
    dismissMyDialog(); 

    videoView.start(); 

    mediacontroller.setVisibility(View.VISIBLE); 

    isMediaCtrlShown = false; 

    seekbar.setProgress(0); 

    seekbar.setMax(videoView.getDuration()); 


    ms = videoView.getDuration(); 

    calculateTime(ms); 

    duration.setText("" + hh + ":" + mm + ":" + ss); 

    ms = videoView.getCurrentPosition(); 

    calculateTime(ms); 

    currentTime.setText("" + hh + ":" + mm + ":" + ss); 

    playButton.setImageResource(android.R.drawable.ic_media_pause); 

    updatePosition(); 

    isStarted = true; 

    mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() 
    { 
     // show updated information about the buffering progress 
     @Override 
     public void onBufferingUpdate(MediaPlayer mp, int percent) 
     { 
      Log.d(this.getClass().getName(), "percent: " + percent); 
      percentageBuffer = percent; 
      secondarySeekBarProgressUpdater(percent); 
      // progress.setSecondaryProgress(percent); 
      if (i == 0) 
      { 
       i = i + 1; 

       previouPosition = mp.getCurrentPosition(); 
      } 
      else if (i == 1) 
      { 
       if (mp.getCurrentPosition() == previouPosition) 
       { 
        if (!isPauseButtonClick) 
        { 

         showMyDialog(); 
         if (percent == 100) 
         { 
          dismissMyDialog(); 
         } 
        } 
       } 
       else 
       { 
        i = 0; 

        previouPosition = 0; 

        dismissMyDialog(); 
       } 
      } 
      else if (isCustomSeekButtonClick) 
      { 
       isCustomSeekButtonClick = false; 

       if (mpCurrentPosition == mp.getCurrentPosition()) 
       { 

        showMyDialog(); 
        if (percent == 100) 
        { 
         dismissMyDialog(); 
        } 
       } 
       else 
       { 
        dismissMyDialog(); 
       } 
      } 
     } 
    }); 

    mp.setOnSeekCompleteListener(new OnSeekCompleteListener() 
    { 
     public void onSeekComplete(MediaPlayer mp) 
     { 
      if (mp.isPlaying()) 
      { 

      } 
      else 
      { 
       onStart(); 

       onPause(); 

       onStart(); 

      } 

     } 
    }); 
} 


private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() 
{ 
    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) 
    { 
     isMoveingSeekBar = false; 
    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) 
    { 
     isMoveingSeekBar = true; 
    } 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) 
    { 
     Log.e("",""+progress+""+percentageBuffer); 

     if (fromUser) 
     { 
      isCustomSeekButtonClick = fromUser; 

      videoView.seekTo(progress); 

      mpCurrentPosition = progress; 

      Log.e("OnSeekBarChangeListener", "onProgressChanged"); 
     } 
     if (isMoveingSeekBar) 
     { 
      videoView.seekTo(progress); 

      Log.i("OnSeekBarChangeListener", "onProgressChanged"); 
     } 
    } 
}; 
private View.OnClickListener onButtonClick = new View.OnClickListener() { 

    @Override 
    public void onClick(View v) 
    { 
     switch (v.getId()) 
     { 
     case R.id.play: 
     { 
      if (videoView.isPlaying()) 
      { 
       handler.removeCallbacks(updatePositionRunnable); 

       isPauseButtonClick = true; 

       videoView.pause(); 

       playButton.setImageResource(android.R.drawable.ic_media_play); 

      } 
      else 
      { 
       if (isStarted) 
       { 
        videoView.start(); 
        isPauseButtonClick = false; 
        playButton.setImageResource(android.R.drawable.ic_media_pause); 

        updatePosition(); 
       } 
       else 
       { 
        startPlay(currentFile); 
        isPauseButtonClick = false; 
        videoView.start(); 
       } 
      } 

      break; 
     } 
     case R.id.next: 
     { 
      int seekto = videoView.getCurrentPosition() + STEP_VALUE; 

      if (seekto > videoView.getDuration()) 

       seekto = videoView.getDuration(); 

      videoView.pause(); 

      videoView.seekTo(seekto); 
      /* 
      * try { Thread.sleep(15000); } catch (InterruptedException e) { 
      * // TODO Auto-generated catch block e.printStackTrace(); } 
      */ 
      // player.pause(); 
      videoView.start(); 

      break; 
     } 
     case R.id.prev: { 
      int seekto = videoView.getCurrentPosition() - STEP_VALUE; 

      if (seekto < 0) 
       seekto = 0; 

      videoView.pause(); 

      videoView.seekTo(seekto); 

      // player.pause(); 
      videoView.start(); 

      break; 
     } 
     } 
    } 
}; 

private void updatePosition() 
{ 
    handler.removeCallbacks(updatePositionRunnable); 

    seekbar.setProgress(videoView.getCurrentPosition()); 

    ms = videoView.getCurrentPosition(); 

    calculateTime(ms); 

    currentTime.setText("" + hh + ":" + mm + ":" + ss); 

    handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY); 
} 

private void startPlay(String file) 
{ 
    Log.i("Selected: ", file); 

    // selelctedFile.setText(file); 
    seekbar.setProgress(0); 

    videoView.stopPlayback(); 

    videoView.start(); 

    seekbar.setMax(videoView.getDuration()); 

    playButton.setImageResource(android.R.drawable.ic_media_pause); 

    updatePosition(); 

    isStarted = true; 
} 
void setUpMyDialog() 
{ 
    if (progressDialog == null) 
    { 
     progressDialog = (ProgressDialog) onCreateDialog(DIALOG_KEY); 

     progressDialog = new ProgressDialog(player.this); 
     progressDialog.setMessage("Loading..."); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    } 
} 

void showMyDialog() 
{ 
    Log.e("showMyDialog***", "****" + isMyDialogShowing); 

    if (!isMyDialogShowing) 
    { 
     isMyDialogShowing = true; 

     Log.e("showMyDialog: true***", "****" + isMyDialogShowing); 

     if (progressDialog != null && !progressDialog.isShowing()) 
     { 
      Log.e("showMyDialog: true***", "****progressDialog" + progressDialog.isShowing()); 

      progressDialog.show(); 
     } 
     else if(progressDialog == null) 
     { 
      setUpMyDialog(); 
      progressDialog.show(); 
     } 
    } 
} 

void dismissMyDialog() 
{ 
    Log.e("dismissMyDialog***", "****"); 

    if (progressDialog != null && progressDialog.isShowing()) 
    { 
     progressDialog.dismiss(); 
     progressDialog = null; 

     isMyDialogShowing = false; 
    } 
} 

void killMyDialog() 
{ 
    isMyDialogShowing = false; 
} 
private void secondarySeekBarProgressUpdater(int percent){ 
    seekbar.setSecondaryProgress(percent); 
} 
+0

沒有告訴我們你到目前爲止所做的工作,併發布了你目前使用的代碼,這裏的任何人都不可能解決這個問題。 – FoamyGuy

+0

SO是一個要求特定編程問題的網站。 –

+1

我知道SO是專門用於編程的問題,我不是要求特定的代碼,而是創建這樣一個流媒體播放器的核心邏輯,然後我也添加了我的代碼用於創建我的播放器 – Jitu

回答

2

我對這個問題的直覺說,你可能會被集中在玩家忽視的東西,特別是因爲它工作正常含量少。你有沒有想過檢查流服務器?如果服務器不能完成大文件流式傳輸任務,那麼玩家就無法做到這一點。此外,您可以調整服務器的數據包大小,以幫助播放器一次使用較小的「叮咬」媒體來管理播放。嘗試使用免費的Apple 達爾文流媒體服務器。有一個針對Apache,Windows和其他版本的版本,並且它是非常可配置的。給自己一套不同尺寸的文件,並嘗試確定播放開始失敗的大小。這個數字會給你一個關於問題出在哪裏的大問題,無論是傳輸的服務器數據包大小,Android環境中的可用內存還是其他地方。無論如何,嘗試從服務器設置較小的數據包大小。這應該讓您的播放器做更少的工作,並希望改善播放。

你可以在網上獲得很多地方的達爾文服務器。 Here is one such link。維基百科也有一些有用的信息和一些這個服務器的鏈接,find them here。祝你在這方面的研究。

弗蘭克。

+0

我們通過對視頻進行適當的編碼,可以很好地播放視頻。感謝您的回覆.. :-) – Jitu