系统播放器
打开视频列表
public void getbut_local(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri url = data.getData();
if (url==null){
return;
}
Log.d(TAG, "onActivityResult: ---" + url);
getSystemplayer(url);
}
调用系统播放器播放视频
private void getSystemplayer(Uri url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(url, "video/*");
startActivity(intent);
}
调用系统播放器播放网络视频
public void getLocal(View view) {
// 练习网址: http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4
String BaseUrl = "http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4";
Uri uri = Uri.parse(BaseUrl);
getSystemplayered(uri);
}
private void getSystemplayered(Uri url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(url, "video/*");
startActivity(intent);
}
VideoView播放器
调用 VideoView播放器播放视频
public void getbut_local(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri url = data.getData();
if (url==null){
return;
}
Log.d(TAG, "onActivityResult: ---" + url);
playVideoView(url);
}
调用 VideoView 播放器播放网络视频
public void getVideo(View view) {
// 练习网址: http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4
String BaseUrl = "http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4";
Uri uri = Uri.parse(BaseUrl);
playVideoView(uri);
}
private void playVideoView(Uri uri){
VideoView videoView=this.findViewById(R.id.videoview);
videoView.setVideoURI(uri);
videoView.start();
}
SurfaceView + Mediaplay 播放器
SurfaceView + Mediaplay 实现播放本地视频
public class MainActivity extends AppCompatActivity {
private SurfaceView mSurfaceview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// 1、 SurfaceView 找到 SurfaceView 布局
mSurfaceview = (SurfaceView) findViewById(R.id.surfaceview);
}
}
public void getVideo(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
// 2、MediaPlayer
final MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(this, uri);
} catch (IOException e) {
e.printStackTrace();
}
// 3、SurfaceHolder
// 用于 连接 2、MediaPlayer 与 1、SurfaceView
mSurfaceview.getHolder().addCallback(new SurfaceHolder.Callback2() {
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.stop();
mediaPlayer.release();
}
});
}
SurfaceView + Mediaplay 实现播放网络视频
public class SurfaceViewActivity extends AppCompatActivity {
private ImageView imageView;
private SurfaceView surfaceView;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_view);
context = getApplicationContext();
imageView = this.findViewById(R.id.imageView);
// SurfaceView
surfaceView = this.findViewById(R.id.surfaceView);
getVideoCover();
playVideo(surfaceView);
}
private void playVideo(SurfaceView surfaceView) {
try {
// MediaPlayer
final MediaPlayer mediaPlayer = new MediaPlayer();
Uri uri = Uri.parse("http://vfx.mtime.cn/Video/2019/03/17/mp4/190317150237409904.mp4");
mediaPlayer.setDataSource(this, uri);
// SurfaceHolder
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback2() {
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// SurfaceView创建时调用
try {
mediaPlayer.setDisplay(holder);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// 横竖屏切换
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// SurfaceView销毁时调用
mediaPlayer.stop();
mediaPlayer.release();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void getVideoCover() {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Uri uri = Uri.parse("http://vfx.mtime.cn/Video/2019/03/17/mp4/190317150237409904.mp4");
retriever.setDataSource(uri.toString(), new HashMap<String, String>());
Bitmap bitmap = retriever.getFrameAtTime();
imageView.setImageBitmap(bitmap);
// 获取屏幕的宽高
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
// 重新设置SurfaceView宽高
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 计算最佳缩放比例
float widthRatio = width * 1.f / widthPixels;
float heightRatio = height * 1.0f / heightPixels;
float ratio = Math.max(widthRatio, heightRatio);
// 计算视频显示的宽高
int displayWidth = (int) (width / ratio);
int displayHeight = (int) (height / ratio);
// 设置SurfaceView的宽高
ViewGroup.LayoutParams layoutParams = surfaceView.getLayoutParams();
layoutParams.width = displayWidth;
layoutParams.height = displayHeight;
surfaceView.requestLayout();
}
// teamviewer
}