• android 54 播放音视频


    mainActivity:

    package com.sxt.day07_09;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.provider.MediaStore.Video;
    import android.provider.MediaStore.Video.Thumbnails;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.VideoView;
    
    public class MainActivity extends Activity {
        ArrayList<HashMap<String, Object>> mVideos;//M
        Gallery mGallery;//V
        VideoAdapter mAdapter;//C,用于列表显示
        VideoView mVideoView;//预览视频控件
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getVideoInfo();
            initView();
            setListener();
        }
    
        private void setListener() {
            mGallery.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    HashMap<String, Object> video=mVideos.get(position);
                    String path=video.get(Video.Media.DATA).toString();
                    mVideoView.setVideoPath(path);//设置视频路径
                    mVideoView.start();//播放
                }
            });
        }
    
        private void initView() {
            mVideoView=(VideoView) findViewById(R.id.vv);
            mGallery=(Gallery) findViewById(R.id.gallery);
            mAdapter=new VideoAdapter(mVideos, this);//M添加到
            mGallery.setAdapter(mAdapter);
        }
    
        private void getVideoInfo() {
            ContentResolver resolver = getContentResolver();
            String[] projection={
                Video.Media._ID,//拿到略缩图
                Video.Media.DATA,//sd卡路径
                Video.Media.TITLE
            };
            mVideos=new ArrayList<HashMap<String,Object>>();
            //查询的是安卓数据库,数据库存的是多媒体文件的信息
            Cursor c = resolver.query(Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
            while(c.moveToNext()){
                HashMap<String, Object> video=new HashMap<String, Object>();
                video.put(Video.Media._ID, c.getInt(c.getColumnIndex(Video.Media._ID)));
                video.put(Video.Media.DATA, c.getString(c.getColumnIndex(Video.Media.DATA)));
                video.put(Video.Media.TITLE, c.getString(c.getColumnIndex(Video.Media.TITLE)));
                mVideos.add(video);
            }
        }
    
        //适配器,用于列表显示
        class VideoAdapter extends BaseAdapter{
            ArrayList<HashMap<String, Object>> videos;
            Context context;
            
            public VideoAdapter(ArrayList<HashMap<String, Object>> videos,
                    Context context) {
                super();
                this.videos = videos;
                this.context = context;
            }
    
            @Override
            public int getCount() {
                return videos.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            @Override
            //没有用缓存做优化
            public View getView(int position, View layout, ViewGroup parent) {
                layout=View.inflate(context, R.layout.item_video, null);
                ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);//显示视频略缩图
                TextView tvTitle=(TextView) layout.findViewById(R.id.tvTitle);//视频标题
                HashMap<String, Object> video=videos.get(position);
                tvTitle.setText(video.get(Video.Media.TITLE).toString());
                int id=(Integer) video.get(Video.Media._ID);
                //Thumbnails类用于获取视频或者图片的略缩图,
                Bitmap bm=Thumbnails.getThumbnail(getContentResolver(), id, Thumbnails.MICRO_KIND, null);
                ivThumb.setImageBitmap(bm);
                return layout;
            }
        }
    }

    主页面:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <Gallery                                    水平滚动
            android:id="@+id/gallery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <VideoView                             预览播放视频控件
            android:id="@+id/vv"
            android:layout_below="@id/gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    
    item_view.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        
        <ImageView                                 每一项布局
            android:id="@+id/ivThumb"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/ic_launcher"/>
        <TextView 
            android:id="@+id/tvTitle"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="安卓"
            android:gravity="center_horizontal"/>
    </LinearLayout>
  • 相关阅读:
    Excel表导入数据库时带小数点的数据会变成科学计数样式的解决方法
    C# 具有合计行的DataGridViewNiceDataGridView1.0
    nginx 【使用echo调试】【地址复写】
    ts 【申明文件】
    createreactapp 【引入ui框架样式,全局样式被处理成模块化样式处理方法】
    node 【node服务器搭建1:安转node 和pm2】
    http 【前后端缓存】【nginx配合缓存】
    nginx 【匹配规则】【开启gzip压缩】
    nginx【nginx配置】
    react 【useMome、useCallback原理详解】
  • 原文地址:https://www.cnblogs.com/yaowen/p/4909315.html
Copyright © 2020-2023  润新知