Android中使用TextureView播放视频
package com.example.textureviewvideo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.TextureView.SurfaceTextureListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements SurfaceTextureListener{
// private TextureView textureView;
private MediaPlayer mMediaPlayer;
private Surface surface;
private ImageView videoImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextureView textureView=(TextureView) findViewById(R.id.textureview);
textureView.setSurfaceTextureListener(this);//设置监听函数 重写4个方法
// textureView=new TextureViewTest(this);
// setContentView(textureView);
videoImage=(ImageView) findViewById(R.id.video_image);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,int height) {
System.out.println("onSurfaceTextureAvailable onSurfaceTextureAvailable");
surface=new Surface(surfaceTexture);
new PlayerVideo().start();//开启一个线程去播放视频
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,int height) {
System.out.println("onSurfaceTextureSizeChanged onSurfaceTextureSizeChanged");
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
System.out.println("onSurfaceTextureDestroyed onSurfaceTextureDestroyed");
surfaceTexture=null;
surface=null;
mMediaPlayer.stop();
mMediaPlayer.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
// System.out.println("onSurfaceTextureUpdated onSurfaceTextureUpdated");
}
private class PlayerVideo extends Thread{
@Override
public void run(){
try {
File file=new File(Environment.getExternalStorageDirectory()+"/ansen.mp4");
if(!file.exists()){//文件不存在
copyFile();
}
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource(file.getAbsolutePath());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp){
videoImage.setVisibility(View.GONE);
mMediaPlayer.start();
}
});
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public interface PlayerController{
public void play();
}
/**
* 如果sdcard没有文件就复制过去
*/
private void copyFile() {
AssetManager assetManager = this.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("ansen.mp4");
String newFileName = Environment.getExternalStorageDirectory()+"/ansen.mp4";
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播
相关文章
- Android TextWatcher内容监听死循环案例详解
- Android系统制作自定义签名的例子
- Android组件之BroadcastReceiver广播接收者
- Android实现仿淘宝购物车增加和减少商品数量功能demo示例
- Android开发中MotionEvent坐标获取方法分析
- Android中PreferenceFragment的使用详解
- Android常用三方库混淆规则整理(小结)
- Android自定义ProgressDialog进度等待框
- bud 游戏社交平台(元宇宙社交) for Android v3.70.0 安卓手机版
- B站小视频 for Android v3.2.0.00 安卓版