0

我在android.support.v4.app.Fragment中使用MediaPlayer類。我想mediaPlayer.start()儘快片段是鑑於發生,所以我做了這個Android片段onVisible

public View onCreateView(LayoutInflater inflater, 
         ViewGroup container, Bundle savedInstanceState) { 
    mediaPlayer = new MediaPlayer(); 
    mediaPlayer.setDisplay(vidHolder); 
    mediaPlayer.setOnPreparedListener(this); 
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mediaPlayer.setDataSource(videoAddress); 
    mediaPlayer.prepareAsync(); 

} 
public void onPrepared(MediaPlayer mediaPlayer) { 
    mediaPlayer.start(); 
} 

現在既然片段自動預載我有兩個視頻同時播放。 是否有像onCreateView這樣的事件,只有當fragement可見時才被觸發?

回答

0

您應該重寫onStart()。

從Android碎片文檔在onStart:

onStart() makes the fragment visible to the user (based on its containing activity being started). 
+0

似乎在onStart也觸發之後onCreateView甚至在片段上視圖。 – 2014-10-10 04:02:54

0

找到了自己

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 
    boolean m_iAmVisible = isVisibleToUser; 

    if (m_iAmVisible) { 
     Log.e("SHOW", videoAddress); 
    } else { 
     Log.e("HIDE", videoAddress); 
    } 
}