Using Android's TextureView to Display Video Mirrored

来源:互联网 发布:尤克里里推荐知乎 编辑:程序博客网 时间:2024/06/16 02:52

http://adevelopers-blog.blogspot.sg/2014/02/using-androids-textureview-to-display.html

Android's VideoView can only play videos straight and not mirrored.
If you want your app to play a video mirrored (for instance if the video was recorded using a front camera), you will need to use a TextureView, which can be easily mirrored by specifyingandroid:scaleX=-1 in the XML file, or textureView.setScaleX(-1) in the code.

Here is a code sample for playing video using TextureView, with the option of displaying the video mirrored:

1234567891011121314151617181920212223242526272829303132
public class SampleActivity implements SurfaceTextureListener {
boolean isMirrored = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
TextureView textureView = (TextureView)findViewById(R.id.myTextureView);
textureView.setSurfaceTextureListener(this);
textureView.setScaleX(isMirrored ? -1 : 1);
}
 
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(new Surface(surface));
try {
mediaPlayer.setDataSource("my_video_file.mp4");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {return false;}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {}
}

0 0
原创粉丝点击