android中使用VideoView播放视频
来源:互联网 发布:阿里域名备案 编辑:程序博客网 时间:2024/05/21 09:55
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yh.myapplication.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/play" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="play" /> <Button android:id="@+id/pause" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="pause" /> <Button android:id="@+id/replay" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="replay" /> </LinearLayout> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/placeholder" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/apple" /></LinearLayout>
MainActivity.java
import android.media.MediaPlayer;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = (VideoView) findViewById(R.id.video_view); Button play = (Button) findViewById(R.id.play); Button pause = (Button) findViewById(R.id.pause); Button replay = (Button) findViewById(R.id.replay); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { //Called when the video is ready to play View placeholder = findViewById(R.id.placeholder); placeholder.setVisibility(View.GONE); } }); play.setOnClickListener(this); pause.setOnClickListener(this); replay.setOnClickListener(this); huanchong(); initVideoPath(); } private void huanchong() { MediaController mc = new MediaController(MainActivity.this);//Video是我类名,是你当前的类 videoView.setMediaController(mc);//设置VedioView与MediaController相关联 } private void initVideoPath() { // File file = new File("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"); videoView.setVideoPath("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.play: if (!videoView.isPlaying()) { videoView.start(); } break; case R.id.pause: if (videoView.isPlaying()) { videoView.pause(); } break; case R.id.replay: if (videoView.isPlaying()) { videoView.resume(); } break; } } @Override protected void onDestroy() { super.onDestroy(); if (videoView != null) { videoView.suspend(); } }}代码很简单,一看就可以明白的哦,希望大家有好的框架或者好的意见,一起相互学习哦。
0 0
- 在Android中使用VideoView播放视频
- 在Android中使用VideoView播放视频
- 在Android中使用VideoView播放视频
- android中使用VideoView播放视频
- Android--使用VideoView播放视频
- 【Android】使用VideoView播放视频
- Android:使用VideoView播放视频
- Android--使用VideoView播放视频
- android中VideoView无法播放此视频
- Android中自定义VideoView视频播放器
- Android使用VideoView播放网络视频
- Android使用VideoView播放网络视频
- Android使用VideoView播放网络视频
- Android使用VideoView播放网络视频
- Android使用VideoView播放网络视频
- Android之多媒体--使用VideoView播放视频
- Android使用VideoView播放网络视频
- Android使用VideoView播放网络视频
- 扑克牌程序
- winXP 32bit 安装AS
- Zeppelin 的安装及简单使用
- c++基本数据类型
- 设计模式—浅析三种工厂模式的特点
- android中使用VideoView播放视频
- Genymotion下载虚拟设备超时的问题
- javascript中的原型和创建对象的原型模式的剖析
- Python中的垃圾回收机制
- 用maven重构ssh项目遇到的问题
- 对比Amazon Echo,Google Home为何只采用了2个麦克风?
- MongoDB 3.X 用户权限控制
- overload与返回类型的关系
- 学生分组的小程序