title: android libvlc multiple surface problems
body: <p>I would like to know if is possible to use two Surface views in the same layout and view the content of both of them at the same time. In future, I would make use of a Grid-view of video-view, where each video-view uses VLC.</p>
  <p>I modified this sample (<a href="https://bitbucket.org/edwardcw/libvlc-android-sample/src/ec2a9176d39968429bee8d197d26d60d75676960/src/com/compdigitec/libvlcandroidsample/VideoActivity.java?at=master" rel="nofollow noreferrer">android-vlc-sample</a>) using a Fragment.</p>
  <p>The result is that I see only one video. How can I solve this?</p>
  <p>From log-cat I do not see any major error, but I think there are rendering problems for android UIThread</p>
  <p><strong>Java Coding</strong></p>
  <pre><code>public class MultipleVideoPlayFragmentActivity extends FragmentActivity {
  
  public final static String LOCATION = &quot;com.compdigitec.libvlcandroidsample.MultipleVideoPlayFragmentActivity.location&quot;;
  private static final String TAG = &quot;MediaPlayer&quot;;
  public String mFilePatha;
  
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.activity_multiple_video_play_fragment);
      Intent intent = getIntent();
      mFilePatha = intent.getExtras().getString(LOCATION);
  }
  
  public static class VideoFragment extends Fragment implements
          SurfaceHolder.Callback, IVideoPlayer {
      public final static String TAG = &quot;LibVLCAndroidSample/VideoActivity&quot;;
  
      public final static String LOCATION = &quot;com.compdigitec.libvlcandroidsample.VideoFragment.location&quot;;
  
      private String mFilePath;
  
      // display surface
      private SurfaceView mSurface;
      private SurfaceHolder holder;
  
      // media player
      private LibVLC libvlc;
      private int mVideoWidth;
      private int mVideoHeight;
      private final static int VideoSizeChanged = -1;
  
      /*************
       * Activity
       *************/
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          return inflater.inflate(R.layout.sample, container, false);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          // Receive path to play from intent
  
          Log.d(TAG, &quot;Playing back &quot; + mFilePath);
          mFilePath = ((MultipleVideoPlayFragmentActivity) getActivity()).mFilePatha;
          // mFilePath=&quot;rtsp://192.168.4.125:554/0&quot;;
          // mFilePath=&quot;android.resource://it.nexera.visiamobile/raw/sample_mpeg4&quot;;
          mSurface = (SurfaceView) getView().findViewById(R.id.surface);
          holder = mSurface.getHolder();
          holder.addCallback(this);
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          setSize(mVideoWidth, mVideoHeight);
      }
  
      @Override
      public void onResume() {
          super.onResume();
          createPlayer(mFilePath);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          releasePlayer();
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          releasePlayer();
      }
  
      /*************
       * Surface
       *************/
  
      public void surfaceCreated(SurfaceHolder holder) {
      }
  
      public void surfaceChanged(SurfaceHolder surfaceholder, int format,
              int width, int height) {
          if (libvlc != null)
              libvlc.attachSurface(holder.getSurface(), this);
      }
  
      public void surfaceDestroyed(SurfaceHolder surfaceholder) {
      }
  
      private void setSize(int width, int height) {
          mVideoWidth = width;
          mVideoHeight = height;
          if (mVideoWidth * mVideoHeight &lt;= 1)
              return;
  
          // get screen size
          int w = getActivity().getWindow().getDecorView().getWidth();
          int h = getActivity().getWindow().getDecorView().getHeight();
  
          // getWindow().getDecorView() doesn't always take orientation into
          // account, we have to correct the values
          boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
          if (w &gt; h &amp;&amp; isPortrait || w &lt; h &amp;&amp; !isPortrait) {
              int i = w;
              w = h;
              h = i;
          }
  
          float videoAR = (float) mVideoWidth / (float) mVideoHeight;
          float screenAR = (float) w / (float) h;
  
          if (screenAR &lt; videoAR)
              h = (int) (w / videoAR);
          else
              w = (int) (h * videoAR);
  
          // force surface buffer size
          holder.setFixedSize(mVideoWidth, mVideoHeight);
  
          // set display size
          LayoutParams lp = mSurface.getLayoutParams();
          lp.width = w;
          lp.height = h;
          mSurface.setLayoutParams(lp);
          mSurface.invalidate();
      }
  
      @Override
      public void setSurfaceSize(int width, int height, int visible_width,
              int visible_height, int sar_num, int sar_den) {
          Message msg = Message.obtain(mHandler, VideoSizeChanged, width,
                  height);
          msg.sendToTarget();
      }
  
      /*************
       * Player
       *************/
  
      private void createPlayer(String media) {
          releasePlayer();
          try {
              if (media.length() &gt; 0) {
                  Toast toast = Toast.makeText(this.getActivity(), media,
                          Toast.LENGTH_LONG);
                  toast.setGravity(
                          Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                  toast.show();
              }
  
              // Create a new media player
              libvlc = LibVLC.getInstance();
              libvlc.setHardwareAcceleration(LibVLC.HW_ACCELERATION_DISABLED);
              libvlc.setSubtitlesEncoding(&quot;&quot;);
              libvlc.setAout(LibVLC.AOUT_OPENSLES);
              libvlc.setTimeStretching(true);
              libvlc.setChroma(&quot;RV32&quot;);
              libvlc.setVerboseMode(true);
              // LibVLC.restart(this.getActivity());
              EventHandler.getInstance().addHandler(mHandler);
              holder.setFormat(PixelFormat.RGBX_8888);
              holder.setKeepScreenOn(true);
              MediaList list = libvlc.getMediaList();
              list.clear();
              list.add(new Media(libvlc, LibVLC.PathToURI(media)), false);
              libvlc.playIndex(0);
          } catch (Exception e) {
              Toast.makeText(this.getActivity(), &quot;Error creating player!&quot;,
                      Toast.LENGTH_LONG).show();
          }
      }
  
      private void releasePlayer() {
          if (libvlc == null)
              return;
          EventHandler.getInstance().removeHandler(mHandler);
          libvlc.stop();
          libvlc.detachSurface();
          holder = null;
          libvlc.closeAout();
          libvlc.destroy();
          libvlc = null;
  
          mVideoWidth = 0;
          mVideoHeight = 0;
      }
  
      /*************
       * Events
       *************/
  
      private Handler mHandler = new MyHandler(this);
  
      private static class MyHandler extends Handler {
          private WeakReference&lt;VideoFragment&gt; mOwner;
  
          public MyHandler(VideoFragment owner) {
              mOwner = new WeakReference&lt;VideoFragment&gt;(owner);
          }
  
          @Override
          public void handleMessage(Message msg) {
              VideoFragment player = mOwner.get();
  
              // SamplePlayer events
              if (msg.what == VideoSizeChanged) {
                  player.setSize(msg.arg1, msg.arg2);
                  return;
              }
  
              // Libvlc events
              Bundle b = msg.getData();
              switch (b.getInt(&quot;event&quot;)) {
              case EventHandler.MediaPlayerEndReached:
                  player.releasePlayer();
                  break;
              case EventHandler.MediaPlayerPlaying:
              case EventHandler.MediaPlayerPaused:
              case EventHandler.MediaPlayerStopped:
              default:
                  break;
              }
          }
      }
  }
  
  }
  </code></pre>
  <p><strong>XML Coding</strong></p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot; &gt;
  
      &lt;fragment
          android:id=&quot;@+id/video_1_fragment&quot;
          android:layout_width=&quot;100dp&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_weight=&quot;1&quot;
          class=&quot;com.compdigitec.libvlcandroidsample.MultipleVideoPlayFragmentActivity$VideoFragment&quot; /&gt;
  
      &lt;fragment
          android:id=&quot;@+id/video_2_fragment&quot;
          android:layout_width=&quot;100dp&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_weight=&quot;1&quot;
          class=&quot;com.compdigitec.libvlcandroidsample.MultipleVideoPlayFragmentActivity$VideoFragment&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  
  **Single Fragment Layout**
  
      &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:baselineAligned=&quot;false&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.SampleActivity&quot; &gt;
  
      &lt;FrameLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; &gt;
  
          &lt;SurfaceView
              android:id=&quot;@+id/surface&quot;
              android:layout_width=&quot;fill_parent&quot;
              android:layout_height=&quot;fill_parent&quot;
              android:layout_gravity=&quot;center&quot; /&gt;
      &lt;/FrameLayout&gt;
  </code></pre>