概述
VideoDetailActivity
import android.content.Intent;
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
import com.animee.day20.R;
public class VideoDetailActivity extends AppCompatActivity {
private VideoView videoView;
private TextView nameTv,timeTv,sizeTv,pathTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_detail);
videoView = (VideoView) findViewById(R.id.id_video_detail);
nameTv = (TextView)findViewById(R.id.tv_name);
timeTv = (TextView)findViewById(R.id.tv_time);
sizeTv = (TextView)findViewById(R.id.tv_size);
pathTv = (TextView)findViewById(R.id.tv_path);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
nameTv.setText("视频名称:"+name);
String path = intent.getStringExtra("path");
pathTv.setText("视频路径:"+path);
String size = intent.getStringExtra("size");
long l = Long.parseLong(size);
long lSize = l / 1024 / 1024;
sizeTv.setText("视频大小:"+lSize+"MB");
String time = intent.getStringExtra("time");
int i = Integer.parseInt(time) / 1000;
int min = i / 60;
int second = i % 60;
timeTv.setText("视频时间:"+min+"分钟"+second+"秒");
videoView.setVideoPath(path);
videoView.setMediaController(new MediaController(this));
videoView.start();
}
}
activity_video_detail
<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.animee.day08.demo05.VideoDetailActivity">
<VideoView
android:id="@+id/id_video_detail"
android:layout_width="match_parent"
android:layout_height="220dp" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="视频详细信息"
android:textSize="26sp"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="视频名称:"
android:textSize="16sp"
/>
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="视频时长:"
android:textSize="16sp"
/>
<TextView
android:id="@+id/tv_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="视频大小:"
android:textSize="16sp"
/>
<TextView
android:id="@+id/tv_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="视频路径:"
android:textSize="16sp"
/>
</LinearLayout>
LocalVideoActivity
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.animee.day20.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LocalVideoActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private static final String TAG = "lengleng";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_video);
mRecyclerView = (RecyclerView) findViewById(R.id.id_local_rv);
// 设置布局管理器
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(manager);
// 获取数据源
List<Map<String, Object>> data = getLocalVideoData();
// 设置适配器
LocalVideoAdapter adapter = new LocalVideoAdapter(this, data);
mRecyclerView.setAdapter(adapter);
}
public List<Map<String,Object>> getLocalVideoData(){
List<Map<String,Object>>mDatas = new ArrayList<>();
// 1.获得ContentResolver对象
ContentResolver resolver = getContentResolver();
// 2.获取Uri对象 --->获取sd卡当中所有的视频信息
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
// 3.获取Cursor对象
Cursor cursor = resolver.query(videoUri, null, null, null, MediaStore.Video.Media.DISPLAY_NAME);
// 4.遍历Cursor
while (cursor.moveToNext()) {
// 获取视频名称
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
// 获取视频的路径
byte[] buf = cursor.getBlob(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
String path = new String(buf, 0, buf.length - 1);
// 获取视频的时间长度
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
// 获取视频的大小
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
Map<String,Object>map = new HashMap<>();
map.put("name",name);
map.put("path",path);
map.put("duration",duration);
map.put("size",size);
mDatas.add(map);
}
return mDatas;
}
}
activity_local_video
<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.animee.day08.demo05.LocalVideoActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_local_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
LocalVideoAdapter
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 com.animee.day20.R;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2017/12/5.
*/
public class LocalVideoAdapter extends RecyclerView.Adapter<LocalVideoAdapter.LocalViewHolder>{
private Context context;
private List<Map<String,Object>>mDatas;
public LocalVideoAdapter(Context context, List<Map<String, Object>> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public LocalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.item_local_video,parent,false);
LocalViewHolder holder = new LocalViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(LocalViewHolder holder, int position) {
final Map<String, Object> map = mDatas.get(position);
String name = map.get("name").toString();
holder.nameTv.setText(name);
String path = map.get("path").toString();
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
holder.thumbIv.setImageBitmap(thumbnail);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取意图对象
Intent intent = new Intent();
intent.setClass(context,VideoDetailActivity.class);
intent.putExtra("name",map.get("name").toString());
intent.putExtra("path",map.get("path").toString());
intent.putExtra("size",map.get("size").toString());
intent.putExtra("time",map.get("duration").toString());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mDatas.size();
}
class LocalViewHolder extends RecyclerView.ViewHolder{
ImageView thumbIv;
TextView nameTv;
public LocalViewHolder(View itemView) {
super(itemView);
thumbIv = (ImageView) itemView.findViewById(R.id.id_item_iv);
nameTv = (TextView) itemView.findViewById(R.id.id_item_tv);
}
}
}
item_local_video
<?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"
android:padding="5dp">
<ImageView
android:id="@+id/id_item_iv"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/id_item_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震惊,邻家母猪为何半夜惨叫...."
android:textSize="22sp"
android:layout_margin="10dp"
android:textColor="@color/colorAccent"/>
</RelativeLayout>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
最后
以上就是缥缈高跟鞋为你收集整理的Android 安卓使用VideoView播放本地视频的全部内容,希望文章能够帮你解决Android 安卓使用VideoView播放本地视频所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复