• Android学习——使用videoView 来播放视频


    定义一个videoview 组件

    <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

        android:paddingRight="@dimen/activity_horizontal_margin"

        android:paddingTop="@dimen/activity_vertical_margin"

        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <VideoView

            android:id="@+id/videoView"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent" />

    </RelativeLayout>

    调用VideoView 的如下两个方法来加载指定的视频

    setVideoPath(String path);

    setVideo(Url url);

    最后调用VideoView的start(),stop(),pause()来控制视频播放

    package xiaocool.net.videotest;

    import android.graphics.PixelFormat;
    import android.os.Environment;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.MediaController;
    import android.widget.VideoView;

    import java.io.File;


    public class MainActivity extends ActionBarActivity {
    private VideoView videoView;
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    //VID20150324130132.mp4
    videoView=(VideoView)this.findViewById(R.id.videoView);
    //创建MediaController对象
    mediaController=new MediaController(this);
    String path = Environment.getExternalStorageDirectory() + "/";
    String name = path + "VID20150324130132.mp4";
    File video=new File(name);
    if(video.exists()){
    videoView.setVideoPath(video.getAbsolutePath());
    videoView.setMediaController(mediaController);
    mediaController.setMediaPlayer(videoView);
    videoView.requestFocus();
    }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }
    }

    -----------------------------------
    ©著作权归作者所有:来自51CTO博客作者uncom2005的原创作品,请联系作者获取转载授权,否则将追究法律责任
    Android学习——使用videoView 来播放视频
    https://blog.51cto.com/xiaocool/1623931

  • 相关阅读:
    JavaScript实现类的private、protected、public、static以及继承
    OSS网页上传和断点续传(STSToken篇)
    OSS网页上传和断点续传(OSS配置篇)
    Linq sum()时遇到NULL
    SQLSERVER事务日志已满 the transaction log for database 'xx' is full
    笔记本高分辨软件兼容问题,字体太小或模糊
    H5上传图片之canvas
    An error occurred while updating the entries. See the inner exception for details.
    无限级结构SQL查询所有的下级和所有的上级
    SQLserver 进程被死锁问题解决
  • 原文地址:https://www.cnblogs.com/javalinux/p/16200975.html
Copyright © 2020-2023  润新知