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 specifying android: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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | |
} |
thanks it works for me
ReplyDeletei am using videoview custom , how can i find TextureView ?Please help me :(
ReplyDelete