Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This effect can be achieved using a <code>TextureView</code>. Essentially what you do is create a <code>MediaPlayer</code> instance, <code>prepare()</code> it and start() it, you can then use <code>MediaPlayer.setSurface()</code> method on any <code>TextureView</code> to change the surface as the video is playing without any changes to <code>MediaPlayer</code> object state, as stated in android's docs for <code>setSurface()</code> method:</p> <p><code>This method can be called in any state and calling it does not change the object state.</code></p> <p>Please note this implementation is for the sake of demonstration, you should probably use <code>mediaplayer.prepareAsync()</code> and wait for a callback with <code>onPreparedListener()</code>, you will also need to set the correct dimensions for the second <code>TextureView</code> according to your video size, handle orientation changes and of course handle exceptions properly wherever needed.</p> <p>activity_main.xml:</p> <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:background="#ff0000" /&gt; &lt;Button android:id="@+id/btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="Switch to second surface" /&gt; &lt;TextureView android:id="@+id/tv_full" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>MainActivity.java</p> <pre><code>public class MainActivity extends Activity { private static final String VIDEO_URI = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; private TextureView tvFull; private MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mp = new MediaPlayer(); try { mp.setDataSource(this, Uri.parse(VIDEO_URI)); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.prepare(); } catch (IOException e) { e.printStackTrace(); } ((ViewPager) findViewById(R.id.pager)).setAdapter(new PagerAdapter() { @Override public int getCount() { return 1; } @Override public boolean isViewFromObject(View view, Object o) { return view == o; } @Override public Object instantiateItem(ViewGroup container, int position) { final TextureView tv = new TextureView(MainActivity.this); tv.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) { tv.setSurfaceTextureListener(null); mp.setSurface(new Surface(surfaceTexture)); mp.start(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {} @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {} }); if (tv.isAvailable()) { tv.getSurfaceTextureListener().onSurfaceTextureAvailable(tv.getSurfaceTexture(), tv.getWidth(), tv.getHeight()); } container.addView(tv, 0); return tv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }); tvFull = (TextureView) findViewById(R.id.tv_full); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { tvFull.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) { tvFull.setSurfaceTextureListener(null); mp.setSurface(new Surface(surfaceTexture)); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } }); if (tvFull.isAvailable()) { tvFull.getSurfaceTextureListener().onSurfaceTextureAvailable(tvFull.getSurfaceTexture(), tvFull.getWidth(), tvFull.getHeight()); } } }); } @Override protected void onDestroy() { mp.reset(); mp.release(); super.onDestroy(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload