视频

 
 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.moive.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>

package com.example.moive;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView localRV;
    private LinearLayoutManager manager;
    private List<VideoInfo>mDatas;
    private VideoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        localRV = (RecyclerView) findViewById(R.id.rv);
        //设置布局管理器
        //1.context
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        localRV.setLayoutManager(manager);
        //获取数据源(获取本地sd卡当中的视频资源,因为是系统数据库的资源,所以通过contentResolver获取)
        mDatas=new ArrayList<>();
        //获得适配器对象
        adapter = new VideoAdapter(this, mDatas);//生成adapter对象
        //设置适配器
        localRV.setAdapter(adapter);
        //获取本地的视频资源  存放发到数据源里面
        locaLocaData();//自己定义名字
    }
    //创建一个获取本地视频的方法
    private void locaLocaData() {

        //1.获取contentResolver对象
        ContentResolver resolver = getContentResolver();
        //2.获取访问的URI地址
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        //3.执行访问的工作  获取Cursor对象
        Cursor cursor = resolver.query(uri, null, null, null, MediaStore.Video.Media.DISPLAY_NAME);
        //4.遍历cursor获取信息,存放到对象中
        while (cursor.moveToNext()){
            //返回资源名称
            String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
            //返回资源路径
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
            //返回作者名称
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ARTIST));
            //返回大小
            long bSize = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
            long mSize=bSize/1024/1024;
//            1kb = 1024b      1mb=1024kb
            //时间
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
            //时间变为String类型
            SimpleDateFormat zh = new SimpleDateFormat("mm:ss");//转换器对象
            Date date = new Date(duration);
            String time = zh.format(date);

            //创建对象,将数据都存储在对象当中
            VideoInfo videoInfo = new VideoInfo(name, path, time, mSize + "", artist);
            //接着将数据添加到数据源中
            mDatas.add(videoInfo);
            // 数据源发生变化了,提示适配器更新数据
            adapter.notifyDataSetChanged();
        }
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#2f312f"
    tools:context="com.example.moive.PlayVideoActivity">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:textSize="20sp"
        android:text="视频名称"/>

    <VideoView
        android:id="@+id/play_vv"
        android:layout_width="match_parent"
        android:layout_height="260dp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="视频详情内容:"
        android:textColor="#f4da58"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_marginTop="20dp"/>

    <TextView
        android:id="@+id/play_tv_author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="制作人员:"
        android:textColor="#f5e462"
        android:textStyle="bold"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/play_tv_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="视频时长:"
        android:textColor="#ffd454"
        android:textStyle="bold"
        android:textSize="15sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/item_video_iv"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"/>


    <TextView
        android:id="@+id/item_video_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#f4a507"
        android:textStyle="bold"
        android:text="龙珠超"/>
</RelativeLayout>
package com.example.moive;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class PlayVideoActivity extends AppCompatActivity {
    VideoInfo info;
    private TextView nameTv,authorTv,timeTv;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);
//        接受上一个界面传递的数据
        Intent intent = getIntent();
        info = (VideoInfo) intent.getSerializableExtra("video");
//        查找控件
        nameTv    =  findViewById(R.id.tv_name);
        authorTv  =  findViewById(R.id.play_tv_author);
        timeTv    =  findViewById(R.id.play_tv_time);
        videoView =  findViewById(R.id.play_vv);

        nameTv.setText(info.getName());
        authorTv.setText("制作人:"+info.getAuthor());
        timeTv.setText("视频时长:"+info.getTime());

        videoView.setVideoPath(info.getPath());
        videoView.setMediaController(new MediaController(this));
        videoView.start();
    }
}
package com.example.moive;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 帅比浩宇 on 2018/1/30.
 */

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder>{

    private Context context;
    private List<VideoInfo>mDatas;
    //构造方法进行传递
    public VideoAdapter(Context context, List<VideoInfo> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //固定
        View itemView= LayoutInflater.from(context).inflate(R.layout.item_local_video,parent,false);
        //获取viewHolder对象
        VideoViewHolder holder = new VideoViewHolder(itemView);
        //返回viewHolder
        return holder;
    }

    @Override
    public void onBindViewHolder(VideoViewHolder holder, int position) {
        //获得指定位置的数据源
        final VideoInfo info = mDatas.get(position);
        //进行设置
        holder.tv.setText(info.getName());//视频名称传在tv上面
        //通过路径生成视频缩略图对象的方法
        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(info.getPath(), MediaStore.Video.Thumbnails.MINI_KIND);
        //通过bitmap设置
        holder.iv.setImageBitmap(bitmap);


        //设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,PlayVideoActivity.class);
                intent.putExtra("video",info);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {

        return mDatas.size();//返回集合的长度
    }

    class VideoViewHolder extends RecyclerView.ViewHolder{//内部类
        ImageView iv;
        TextView tv;

        public VideoViewHolder(View itemView) {
            //初始化每一个控件
            super(itemView);
            iv= itemView.findViewById(R.id.item_video_iv);
            tv= itemView.findViewById(R.id.item_video_tv);
        }
    }
}
package com.example.moive;

import java.io.Serializable;

/**
 * Created by 帅比浩宇 on 2018/1/30.
 */

public class VideoInfo implements Serializable {
    private String name;//视频的名称
    private String path;//视频的路径
    private String time;//视频的时间
    private String size;//视频的大小
    private String author;//视频制作者

    @Override
    public String toString() {
        return "VideoInfo{" +
                "name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", time='" + time + '\'' +
                ", size='" + size + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public VideoInfo() {
    }

    public VideoInfo(String name, String path, String time, String size, String author) {
        this.name = name;
        this.path = path;
        this.time = time;
        this.size = size;
        this.author = author;
    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.moive"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.moive">

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 读取sd卡的权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/p"
        android:label="视频播放器          "
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PlayVideoActivity"></activity>
    </application>

</manifest>



猜你喜欢

转载自blog.csdn.net/p__henry/article/details/79350599
今日推荐