【Android 多媒体应用】使用 VideoView 播放视频
2024-09-28 14:04:18
1.MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private VideoView videoView;
private Button btn_start;
private Button btn_pause;
private Button btn_stop; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
videoView = (VideoView) findViewById(R.id.videoView);
btn_start = (Button) findViewById(R.id.btn_start);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_stop = (Button) findViewById(R.id.btn_stop); btn_start.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this); //根据文件路径播放
videoView.setVideoPath("/sdcard/TopGirl.mp4");
videoView.setMediaController(new MediaController(this));
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
videoView.start();
break;
case R.id.btn_pause:
videoView.pause();
break;
case R.id.btn_stop:
videoView.stopPlayback();
break;
}
}
}
2.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"> <VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开始" /> <Button
android:id="@+id/btn_pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停 " /> <Button
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
</LinearLayout> </LinearLayout>
最新文章
- 分享一款自己改进的皮肤“verdant”.
- 十分钟了解分布式计算:Google Dataflow
- AWS re:Invent 2014回顾
- BZOJ 1087 【SCOI2005】 互不侵犯King
- Bridge(桥接)-对象结构型模式
- 项目开发中遇到的extjs常见问题
- bzoj4561: [JLoi2016]圆的异或并
- GCC笔记
- blocked because of many connection errors; unblock with 'mysqladmin flush-hosts;MySQL在远程访问时非常慢的解决方法;MySql链接慢的解决方法
- SkyEye的使用
- sr4000自带API和opencv结合获取图像
- Linux的nginx环境的vue 部署
- “《编程珠玑》(第2版)第2章”:A题(二分搜索)
- ArcGis Classic COM Add-Ins插件开发的一般流程 C#
- mybatis-generator 自动生成mapper以及实体类
- web安全系列1:入侵的途径
- iOS之iOS11、iPhone X、Xcode9 适配指南
- bcolz的新操作
- java课上测试心得
- javascript类式继承函数最优版