2012-09-27 101 views
5

對於自定義視頻,我必須重寫MediaController(更改按鈕的設計並連接到按鈕暫停/播放選擇器)。情況是這樣的:將視頻暫停並轉動應用程序,然後再次恢復,視頻開始再次播放,而不是從暫停的位置開始播放,因此問題出現了:如何使視頻開始播放從哪裏停下來? VideoActivity中需要更改哪些內容?從VideoView中的暫停狀態播放視頻

public class VideoActivity extends Activity { 

VideoView videoView; 

VkMediaController mc; 

private static final String CURRENT = "duration"; 

private static final String URL = "url"; 

private Uri mURI; 

private int mCurrentPosition = -1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.video_view_player); 
    videoView = (VideoView) findViewById(R.id.videoViewplayer); 
    mc = new VkMediaController(this); 
    mURI = getIntent().getData(); 
    if (savedInstanceState != null) { 
     mURI = Uri.parse(savedInstanceState.getString(URL)); 
     mCurrentPosition = savedInstanceState.getInt(CURRENT); 

    } 
    videoView.setMediaController(mc); 
    videoView.setVideoURI(mURI); 
    videoView.requestFocus(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    videoView.start(); 
    if (mCurrentPosition != -1) { 
     videoView.seekTo(mCurrentPosition); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    outState.putInt(CURRENT, videoView.getCurrentPosition()); 
    outState.putString(URL, mURI.toString()); 
    super.onSaveInstanceState(outState); 
} 

}

+0

什麼是錯誤/問題?這段代碼有什麼錯誤? – njzk2

+0

沒有錯誤,但沒有從當前位置開始播放,並且從 – ricky

+0

開始您是否檢查了mCurrentPosition的值? – njzk2

回答

14

我認爲這個問題是videoView.getCurrentPosition()總是返回0,當你調用內部onSaveInstanceState(Bundle outState),COS當時的視頻已經復位,所以叫videoView.getCurrentPosition()onPause(),這將返回你所期望的值。

+0

後重新啓動視頻謝謝,我嘗試做 – ricky

+0

保護無效onPause(){ videoView.pause(); videoView.getCurrentPosition(); super.onPause(); }它是正確的? – ricky

+0

我必須在onSaveInstanceState(Bundle outState)中保留mCurrentPosition? – ricky

2

我有同樣的問題。我以爲我做了一切正確,但它沒有奏效。然後我只是改變了順序,所以我第一次打電話VideoView.seekTo(),然後VideoView.start()。這工作。 我也有VideoView.requestFocus()在那裏,但我不知道這是否重要。

videoPlayer.requestFocus(); 
videoPlayer.seekTo(position); 
videoPlayer.start(); 

所以,如果你確定你的位置變量有正確的值,這可能是你的答案。 視頻未加載時,您可能無法查找。像這樣實現onPreparedListener:

public class VideoActivity extends Activity implements OnPreparedListener { 
    VideoView videoPlayer; 
    int position; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(savedInstanceState!=null){ 
    position=savedInstanceState.getInt("pos") 
    } 
    // setting up the videoview 
    setContentView(R.layout.activity_video); 
    getIntent().getExtras().getString("url"); 
    Uri videouri = Uri.parse(getIntent().getExtras().getString("url")); 
    videoPlayer = (VideoView) findViewById(R.id.videoView); 
    videoPlayer.setOnPreparedListener(this); 
    videoPlayer.setKeepScreenOn(true); 
    videoPlayer.setVideoURI(videouri); 

    } 

    /** 
    * Start the plaback when the video is loaded 
    * 
    * @param mp 
    * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer) 
    */ 
    public void onPrepared(MediaPlayer mp) { 
    //this is a TextView infront of the VideoView which tells the User the Video is loading- hide that 
    findViewById(R.id.tv_video_load).setVisibility(View.GONE); 

    videoPlayer.requestFocus(); 
    videoPlayer.seekTo(position) 
    videoPlayer.start(); 
} 
} 
+0

你可以在onCreate()或onStart()中調用這些方法嗎? – ricky

+0

哦你是對的。我已經實現了OnPreparedListener,這一切都在onPrepared方法中。有意義的是,如果視頻尚未加載,則無法查找。 –

+0

我在onStar中調用了這些方法,它是正確的? – ricky