VideoView和SurfaceView视频播放
2015-09-15 20:02
399 查看
VideoView是SurfaceView的子类
播放视频必须的写权限
播放视频必须的写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
VideoView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始播放"/> <VideoView android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>
public class MainActivity extends AppCompatActivity { private Button mButtonStart; private VideoView mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonStart= (Button) findViewById(R.id.button_start); mVideoView= (VideoView) findViewById(R.id.videoview); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4"); mVideoView.setMediaController(new MediaController(getApplicationContext())); mVideoView.start(); } }); }
SurfaceView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始播放"/> <SurfaceView android:id="@+id/surfaceview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
public class MainActivity extends AppCompatActivity { private Button mButtonStart; private SurfaceView mSurfaceView; private MediaPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSurfaceView= (SurfaceView) findViewById(R.id.surfaceview); mButtonStart= (Button) findViewById(R.id.button_start); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (player==null){ player=new MediaPlayer(); } player.reset(); try { player.setDataSource(Environment.getExternalStorageDirectory()+"/aa.mp4");//设置视频地址 player.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置播放声音leix player.setDisplay(mSurfaceView.getHolder());//设置视频播放位置 player.prepare(); player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.start(); } }); } catch (IOException e) { e.printStackTrace(); } } }); }
相关文章推荐
- Servlet与JSP 相关面试题
- hightmaps 按地图上显示的统计数据
- 雨林木风Win10出现"Windows任务的主机进程已停止工作"怎么办?
- iOS 常用的几个第三方库
- java参数传递
- 【笔记】DOM事件探秘(慕课)
- 周志华点评机器学习会议
- falsh屏蔽右键 防止弹出窗口
- vmware上centos连接网络总结
- 用C#编写Windows服务
- 基于接口的动态代理
- Centos6安装WordPress(二)WordPress安装
- 如何理解JAVA的跨平台特性
- Swift_基础部分
- 1325 Is It A Tree?
- UIScrollView
- 以个人用户在服务器运行 caffe 点滴
- 小荷才露尖尖角之struts的秘密
- Android的视频播放之VideoView与SurfaceView
- POJ 3468 线段树区间求和