首页 > 代码库 > Android 两种方式播放视频
Android 两种方式播放视频
主MainActivity的布局配置文件
这是activity_main.xml。主要出现两个按钮,按钮一通过mediaplayer播放,按钮二通过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="com.shao.MainActivity" > <Button android:id="@+id/btnSurfaceView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start SurfaceView" /> <Button android:id="@+id/btnVideoView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start VideoView" /> </LinearLayout>
主Activity。用于控制两个按钮,设置布局
package com.shao; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { public Button btnSurfaceView; public Button btnVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.btnSurfaceView = (Button) findViewById(R.id.btnSurfaceView); this.btnVideoView = (Button) findViewById(R.id.btnVideoView); this.btnSurfaceView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SurfaceViewActivity.class); startActivity(intent); } }); this.btnVideoView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, VideoViewActivity.class); startActivity(intent); } }); } }
1、通过MediaPlayer
package com.shao; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class SurfaceViewActivity extends Activity { private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Callback SurfaceHolderCallbac = new Callback() { private MediaPlayer mediaPlay; @Override public void surfaceDestroyed(SurfaceHolder holder) { mediaPlay.release(); } @Override public void surfaceCreated(SurfaceHolder holder) { mediaPlay = MediaPlayer.create(SurfaceViewActivity.this, R.raw.jikexueyuan); try { mediaPlay.prepare(); } catch(Exception e) { e.printStackTrace(); } mediaPlay.setDisplay(surfaceHolder); mediaPlay.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); surfaceView = new SurfaceView(this); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(SurfaceHolderCallbac); setContentView(surfaceView); } }
需要实例CallBack属性,并且覆盖里面的方法。主要的内容还是在onCreate里面,addCallback加入了这个SurfaceHolderCallbac。
2、通过VideoView
package com.shao; import android.app.Activity; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class VideoViewActivity extends Activity { public VideoView videoView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); videoView = new VideoView(this); videoView.setVideoPath("/storage/sdcard1/jikexueyuan.mp4"); videoView.setMediaController(new MediaController(this)); videoView.start(); setContentView(videoView); } }
没啥好注意的,就是把这个VideoView 设置了setContentView里面。然后调用了一个setMediaController,这是一个系统自带的播放控制组件。
Android 两种方式播放视频
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。