0

我有一組mp4視頻流入VideoView。視圖尋呼機用於在視頻之間滑動。在視頻瀏覽器內的視頻視圖中實時流式傳輸導致空白屏幕

問題:

當滑動視圖尋呼機,它示出了第一和第二頁的視頻完美。但是,當滑動到達第三頁時,設備屏幕變爲空白,我似乎無法再滑動頁面。

工作代碼:

public class VideoActivity extends Activity implements OnClickListener { 
private ViewPager videoViewPager; 
private CirclePageIndicator videoViewPagerIndicator; 
private VideoViewPagerAdapter videoViewPagerAdapter; 
private VideoView videoView; 
private Context context; 
private ArrayList<VideoItem> videoList = new ArrayList<VideoItem>(8); 
private int pageIndicatorRadius = 5; 
private ProgressBar progressBar = null; 
private MediaController mediaController = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /* Flag to set hide the status bar */ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
    setContentView(R.layout.activity_main); 
    context = this; 
    /* Get the video item object list */ 
    videoList = getVideoList(); 
    /* Adapter for the view pager */ 
    videoViewPagerAdapter = new videoViewPagerAdapter(); 
    videoViewPager = (ViewPager) findViewById(R.id.video_viewpager); 
    videoViewPager.setCurrentItem(0); 
    videoViewPager.setAdapter(videoViewPagerAdapter); 

    // videoViewPager.setOffscreenPageLimit(3); 
    /* 
    * Page view indicator . Using the external page view indicator library 
    */ 
    videoViewPagerIndicator = (CirclePageIndicator) findViewById(R.id.video_pageindicator); 
    videoViewPagerIndicator.setViewPager(videoViewPager); 
    videoViewPagerIndicator.setFillColor(getResources().getColor(
      R.color.translucent_blue)); 
    videoViewPagerIndicator.setRadius(pageIndicatorRadius); 

} 

private class videoViewPagerAdapter extends PagerAdapter { 
    /* Method which returns the number of page viewers */ 
    @Override 
    public int getCount() { 
     return videoList.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 

     return (view == object); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position) { 

     View view = null; 
     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     // Inflater the xml layout which should be added to the page viewer 
     view = inflater.inflate(R.layout.video, null); 

     // Create an object of Video Item from the video list 
     VideoItem videoItemObject = videoList.get(position); 

     /* Initialize video player */ 
     videoView = (VideoView) view 
       .findViewById(R.id.video_view); 
     progressBar = (ProgressBar) view 
       .findViewById(R.id.video_progressbar); 
     Uri video = Uri.parse(videoItemObject.getUrl()); 
     mediaController = new MediaController(context); 
     mediaController.setAnchorView(videoView); 
     videoView.setMediaController(mediaController); 
     videoView.setVideoURI(video); 
     videoView.setKeepScreenOn(true); 
     videoView.requestFocus(); 

     videoView.setOnErrorListener(new OnErrorListener() { 

      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       Toast.makeText(context, "Error occured", Toast.LENGTH_SHORT) 
         .show(); 
       return false; 
      } 
     }); 

     videoView.setOnPreparedListener(new OnPreparedListener() { 

      public void onPrepared(MediaPlayer arg0) { 
       progressBar.setVisibility(View.GONE); 
       videoView.start(); 
      } 
     }); 

     videoView 
       .setOnCompletionListener(new OnCompletionListener() { 

        @Override 
        public void onCompletion(MediaPlayer mp) { 
         videoView.stopPlayback(); 

        } 
       }); 

     videoView.setSoundEffectsEnabled(true); 

     ((ViewPager) collection).addView(view, 0); 

     return view; 
    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 
     /** Stop playing the video on activity destroyed */ 
     try { 
      videoView.stopPlayback(); 
     } catch (Exception e) { 
      Log.i("Error :", "Closing video"); 
     } 

     ((ViewPager) collection).removeView((View) view); 
    } 


} 

@Override 
protected void onDestroy() { 
    try { 
     videoView.stopPlayback(); 
    } catch (Exception e) { 
     Log.i("Error :", "Closing application"); 
    } 
    super.onDestroy(); 
} 

@Override 
protected void onPause() { 
    try { 
     videoView.pause(); 
    } catch (Exception e) { 
     Log.i("Error :", "Closing application"); 
    } 
    super.onPause(); 
} 

/* Function to create a video item object */ 
public VideoItem createVideoItem(String title, String url) { 
    VideoItem videoItem = new VideoItem(); 
    videoItem.setTitle(title); 
    videoItem.setUrl(url); 
    return videoItem; 
} 

/* Function that returns a list of video item objects */ 
public ArrayList<VideoItem> getvideoList() { 
    ArrayList<VideoItem> video = new ArrayList<VideoItem>(8); 
    video.add(createVideoItem("A", 
      "http://10.0.1.15:8081/media/big_buck.mp4")); 
    video.add(createVideoItem("B", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    video.add(createVideoItem("C", 
      "http://10.0.1.15:8081/media/big_buck.mp4")); 
    video.add(createVideoItem("D", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    video.add(createVideoItem("E", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    video.add(createVideoItem("F", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    video.add(createVideoItem("G", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    video.add(createVideoItem("H", 
      "http://10.0.1.15:8081/media/big_buck_bunny.mp4")); 
    return video; 
} 
    } 

的logcat:

04-05 12:45:27.139: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:27.826: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:30.873: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:30.873: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:32.787: I/MediaPlayer(10963): path is null 
04-05 12:45:32.787: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:32.826: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:32.826: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:33.444: D/MediaPlayer(10963): getMetadata 
04-05 12:45:33.444: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:33.490: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:33.584: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:34.514: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:36.662: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:36.662: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:39.248: I/MediaPlayer(10963): path is null 
04-05 12:45:39.248: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:39.295: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:39.303: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:39.944: D/MediaPlayer(10963): getMetadata 
04-05 12:45:39.951: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:39.959: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:40.139: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:42.701: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:42.709: E/libEGL(10963): call to OpenGL ES API with no current context (logged once per thread) 
04-05 12:45:42.709: W/HardwareRenderer(10963): EGL error: EGL_BAD_SURFACE 
04-05 12:45:42.733: W/HardwareRenderer(10963): Mountain View, we've had a problem here. Switching back to software rendering. 
04-05 12:45:44.701: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:44.701: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:45.444: D/dalvikvm(10963): GC_CONCURRENT freed 318K, 12% free 7922K/8967K, paused 3ms+6ms, total 53ms 
04-05 12:45:45.740: I/MediaPlayer(10963): path is null 
04-05 12:45:45.740: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:45.834: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:45.834: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:46.420: D/MediaPlayer(10963): getMetadata 
04-05 12:45:46.428: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:46.436: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:46.592: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:49.006: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:49.061: I/MediaPlayer(10963): path is null 
04-05 12:45:49.061: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:49.162: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:49.162: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:49.772: D/MediaPlayer(10963): getMetadata 
04-05 12:45:49.772: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:49.795: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:49.990: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:50.514: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:50.592: I/MediaPlayer(10963): path is null 
04-05 12:45:50.592: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:51.076: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:51.076: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:51.311: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:51.311: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:51.381: D/MediaPlayer(10963): getMetadata 
04-05 12:45:51.381: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:51.436: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:51.709: I/MediaPlayer(10963): sendBroadcast android.media.IMediaPlayer.videoexist 
04-05 12:45:51.834: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:45:51.912: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:51.912: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:45:59.506: I/MediaPlayer(10963): path is null 
04-05 12:45:59.506: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:59.561: I/MediaPlayer(10963): path is null 
04-05 12:45:59.561: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:45:59.662: D/dalvikvm(10963): GC_CONCURRENT freed 338K, 12% free 8045K/9095K, paused 13ms+9ms, total 77ms 
04-05 12:45:59.678: I/MediaPlayer(10963): path is null 
04-05 12:45:59.678: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:46:00.311: E/MediaPlayer(10963): stop called in state 4 
04-05 12:46:00.311: E/MediaPlayer(10963): error (-38, 0) 
04-05 12:46:00.444: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:46:00.451: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:46:00.475: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:46:00.475: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:46:00.483: D/MediaPlayer(10963): getMetadata 
04-05 12:46:00.483: D/MediaPlayer(10963): getMetadata 
04-05 12:46:00.483: W/MediaPlayer(10963): mediaplayer went away with unhandled events 
04-05 12:46:01.467: I/MediaPlayer(10963): path is null 
04-05 12:46:01.467: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:46:01.498: I/MediaPlayer(10963): path is null 
04-05 12:46:01.498: D/MediaPlayer(10963): Couldn't open file on client side, trying server side 
04-05 12:46:01.756: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:46:01.756: E/SpannableStringBuilder(10963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
04-05 12:46:02.061: E/MediaPlayer(10963): stop called in state 4 
04-05 12:46:02.061: E/MediaPlayer(10963): error (-38, 0) 
04-05 12:46:02.123: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:46:02.186: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:46:02.201: W/SurfaceView(10963): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false 
04-05 12:46:02.389: W/MediaPlayer(10963): mediaplayer went away with unhandled events 
04-05 12:46:02.389: D/MediaPlayer(10963): getMetadata 
+0

請發佈您的logcat吧! – Numair 2013-04-04 10:48:08

回答

0

ViewPager在默認情況下,它的內存兩頁。

它嘗試刪除第一個視圖以加載第三個頁面時崩潰。

您可以通過使用

.setOffscreenPageLimit(X)設置你的ViewPager保留在內存中的頁面數。

這會影響您的應用程序內存消耗,請注意。

相關問題