Android开发---Vitamio框架的实战应用

一.准备工作

1.导入模块
(1)创建新项目MyVideoPlayer

(2)导入模块

选择文件-导入模块,选择模块所在路径,勾选所需模块.(我这只选类库,不选sample)

 

(3)修改gradle文件


导入模块后会报错,打开app的.gradle文件,添加代码如上.

dependencies{

    compile fileTree(dir: 'libs', include:['*.jar'])

compile project(':vitamio')

}


点击箭头处的错误处,打开vitamio的.gradle文件,修改红色框中的代码如下:

 

compileSdkVersion19

    buildToolsVersion "19.1.0"

 

    defaultConfig {

        minSdkVersion 15

        targetSdkVersion 19

    }

 

(4)最后点击Try again, 重新编译gradle.如果这是报错信息消失,说明导入成功! 


导入Vitamio模块修改gradle时 ,对照着app的gradle改

2.权限

<uses-permissionandroid:name="android.permission.INTERNET"/>

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

<activity

           android:name=".VideoPlayActivity"

           android:label="@string/title_activity_video_play"

            android:screenOrientation="landscape"><!--强制横屏-->

        </activity>

 

<application

        android:name=".MyApplication"

3.布局
(1)播放界⾯VideoPlayActivity
<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

   android:layout_height="match_parent"

    android:orientation="vertical">

 

    <!--中心容器-->

    <io.vov.vitamio.widget.CenterLayout

       android:layout_width="match_parent"

       android:layout_height="match_parent"

        android:orientation="vertical">

 

        <io.vov.vitamio.widget.VideoView

           android:id="@+id/surface_view"

           android:layout_width="match_parent"

           android:layout_height="match_parent"

           android:layout_centerHorizontal="true"

            android:layout_centerVertical="true"/>

    </io.vov.vitamio.widget.CenterLayout>

 

</LinearLayout>

 

(2)视频选择界面布局

<LinearLayoutxmlns: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"

    >

   

    <Button

        android:id="@+id/btn_local"

        android:text="本地视频"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

        android:onClick="btnClick"

        />

    <Button

        android:id="@+id/btn_net"

        android:text="网络视频"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

        android:onClick="btnClick"

        />

</LinearLayout>

 

二.使用步骤
1.MyApplication

public class MyApplicationextends Application {

    @Override

    public void onCreate() {

        super.onCreate();

        Vitamio.isInitialized(this);//初始化

    }

}

 

2.MainActivity

public class MainActivityextends Activity {

 

    VideoView mVideoView;

 

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

    // 点击按钮,跳转界面

    public void btnClick(View v) {

        //封装意图

        Intent intent = new Intent(this,VideoPlayActivity.class);

        String path = "";// 路径

        if (v.getId() == R.id.btn_local) {//本地

            path = "file://"

                    + Environment.getExternalStorageDirectory().getAbsolutePath()

                    +"/DCIM/100ANDRO/MOV_0006.mp4";// 本地路径

        }

        if (v.getId() == R.id.btn_net) {//网络

            path ="http://www.codepower.cn/v/spl_cocos_raiden.mp4";

        }

        intent.putExtra("path",path);

        startActivity(intent);

    }

}

 

3.VideoPlayActivity

//视频播放界面

public class VideoPlayActivityextends Activity {

 

    /**

     * 视频播放视图

     */

    VideoView mVideoView;

 

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

       requestWindowFeature(Window.FEATURE_NO_TITLE);

       setContentView(R.layout.activity_video_play);

 

 

        //拿到控件

        mVideoView = (VideoView)findViewById(R.id.surface_view);

 

        //拿到Intent

        String path =getIntent().getStringExtra("path");

 

        //调用播放方法

        playfunction(path);

    }

 

    /**

     * 播放方法

     *

     * @param path

     */

    private void playfunction(String path) {

        if (path == "") {

            // Tell the user to provide a mediafile URL/path.

            Toast.makeText(this, "请设置视频的URI地址", Toast.LENGTH_LONG).show();

            return;

        } else {

            /*

            * Alternatively,for streaming mediayou can use

            *mVideoView.setVideoURI(Uri.parse(URLstring));

            */

            mVideoView.setVideoPath(path);//设置路径

            mVideoView.setMediaController(newMediaController(this));

            mVideoView.requestFocus();//获取焦点

           mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override

                public void onPrepared(MediaPlayermediaPlayer) {

                    // optional need Vitamio4.0

                    //播放速度

                   mediaPlayer.setPlaybackSpeed(1.0f);

                }

            });

        }

    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值