首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

简单视频播放器demo

2024-12-20 来源:化拓教育网

```

当你选择成为别人时,你将失去你自己。——《纳尼亚传奇3》

```

使用图像异步加载库 universalimageloader:

一、介绍

Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程,缓存,内存溢出等很多方面。但是,你也可以参考这个例子来自己写出更好的程序。在此为大家介绍一下。效果图如下:

                                                                                                        1.jpg


1、使用

1.1 在project的build.gradle添加如下代码和引入lib库

android {

    compileSdkVersion 29

    buildToolsVersion "29.0.2"

    defaultConfig {

        applicationId "com.ocwvar.video_svg"

        minSdkVersion 19

        targetSdkVersion 29

        versionCode 1

        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }

    }

//依赖(图像异步加载)

implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'

1.2 运行所需权限

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

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

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

1.3 布局文件

<?xml version="1.0" encoding="utf-8"?>

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <fm.jiecao.jcvideoplayer_lib.JCVideoPlayer

        android:id="@+id/jcvideoplayer"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>

1.4 配置清单文件

<?xml version="1.0" encoding="utf-8"?>

    package="com.ocwvar.video_svg">

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity"

            android:configChanges="orientation"

            android:screenOrientation="portrait">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:name=".ListActivity"

            android:screenOrientation="portrait" />

        <activity

            android:name=".ListViewpagerActivity"

            android:screenOrientation="portrait" />

        <activity

            android:name=".SetSkinActivity"

            android:screenOrientation="portrait" />

    </application>

</manifest>

1.5 activity 中调运

 一行代码调用,大部分需求都能实现,

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    JCVideoPlayer videoController1, videoController2, videoController3;

    Button btnToList, btnToListViewpager, btnToFullscreen, btnToChangecolor;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        /**

        * 如果没有埋点需求,可以不用注册eventbus

        * <br>

        * if you do not want to get Buried Point , you do not need regist eventbus here

        */

        EventBus.getDefault().register(this);

        videoController1 = (JCVideoPlayer) findViewById(R.id.videocontroller1);

       

               

                "简单代码实现视频播放");

        videoController2 = (JCVideoPlayer) findViewById(R.id.videocontroller2);

       

               

                "简单代码实现视频播放", false);

        videoController3 = (JCVideoPlayer) findViewById(R.id.videocontroller3);

        videoController3.setUp("http://121.40.64.47/resource/mp3/music_yangguang3.mp3",//

               

                "简单代码实现视频播放");

        btnToList = (Button) findViewById(R.id.to_list_activity);

        btnToListViewpager = (Button) findViewById(R.id.to_list_viewpager_activity);

        btnToFullscreen = (Button) findViewById(R.id.to_fullscreen);

        btnToChangecolor = (Button) findViewById(R.id.to_changecolor_activity);

        btnToList.setOnClickListener(this);

        btnToListViewpager.setOnClickListener(this);

        btnToFullscreen.setOnClickListener(this);

        btnToChangecolor.setOnClickListener(this);

    }

    @Override

    protected void onPause() {

        super.onPause();

        JCVideoPlayer.releaseAllVideos();

    }

    public void onEventMainThread(VideoEvents event) {

        if (event.type == VideoEvents.POINT_START_ICON) {

            Log.i("Video Event", "POINT_START_ICON" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_START_THUMB) {

            Log.i("Video Event", "POINT_START_THUMB" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_STOP) {

            Log.i("Video Event", "POINT_STOP" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_STOP_FULLSCREEN) {

            Log.i("Video Event", "POINT_STOP_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_RESUME) {

            Log.i("Video Event", "POINT_RESUME" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_RESUME_FULLSCREEN) {

            Log.i("Video Event", "POINT_RESUME_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_CLICK_BLANK) {

            Log.i("Video Event", "POINT_CLICK_BLANK" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_CLICK_BLANK_FULLSCREEN) {

            Log.i("Video Event", "POINT_CLICK_BLANK_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_CLICK_SEEKBAR) {

            Log.i("Video Event", "POINT_CLICK_SEEKBAR" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_CLICK_SEEKBAR_FULLSCREEN) {

            Log.i("Video Event", "POINT_CLICK_SEEKBAR_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_AUTO_COMPLETE) {

            Log.i("Video Event", "POINT_AUTO_COMPLETE" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_AUTO_COMPLETE_FULLSCREEN) {

            Log.i("Video Event", "POINT_AUTO_COMPLETE_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_ENTER_FULLSCREEN) {

            Log.i("Video Event", "POINT_ENTER_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        } else if (event.type == VideoEvents.POINT_QUIT_FULLSCREEN) {

            Log.i("Video Event", "POINT_QUIT_FULLSCREEN" + " title is : " + event.obj + " url is : " + event.obj1);

        }

    }

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.to_list_activity:

                startActivity(new Intent(MainActivity.this, ListActivity.class));

                break;

            case R.id.to_list_viewpager_activity:

                startActivity(new Intent(MainActivity.this, ListViewpagerActivity.class));

                break;

            case R.id.to_fullscreen://进入全屏播放

                JCVideoPlayer.toFullscreenActivity(this,

                        "简单代码实现视频播放");

                break;

            case R.id.to_changecolor_activity:

                startActivity(new Intent(this, SetSkinActivity.class));

                break;

        }

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        EventBus.getDefault().unregister(this);

    }

}

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

显示全文