Mobile phone audio and video 3-local video list, audio and video 3-list

Source: Internet
Author: User

Mobile phone audio and video 3-local video list, audio and video 3-list

1. Write layout relative layout: ListView, TextView, and ProgressBar, initialization

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 6 <ListView 7 android: divider = "@ null" 8 android: id = "@ + id/listview" 9 android: layout_width = "match_parent" 10 android: layout_height = "match_parent"/> 11 12 <TextView13 android: id = "@ + id/TV _nomedia" 14 android: layout_width = "wrap_content" 15 android: layout_height = "wrap_content" 16 android: layout_centerInParent = "true" 17 android: text = "no video found... "18 android: textColor =" #000000 "19 android: textSize =" 18sp "20 android: visibility =" gone "/> 21 22 <ProgressBar23 android: id = "@ + id/pb_loading" 24 android: layout_width = "wrap_content" 25 android: layout_height = "wrap_content" 26 android: layout_centerInParent = "true" 27/> 28 29 </RelativeLayout>
Video_pager.xml

 

2. Load the local video, load the video in the Child thread, and load the video into the collection; set the adapter to be in the main thread; Use Handler to set the adapter

1 public class VideoPager extends BasePager {2 3 private ListView listview; 4 private TextView TV _nomedia; 5 private ProgressBar pb_loading; 6 7 8 private VideoPagerAdapter videoPagerAdapter; 9 10/** 11 * data set 12 */13 private ArrayList <MediaItem> mediaItems; 14 15 16 public VideoPager (Context context) {17 super (context ); 18 19} 20 21 private Handler handler = new Handler () {22 @ Override 23 pu Blic void handleMessage (Message msg) {24 super. handleMessage (msg); 25 if (mediaItems! = Null & mediaItems. size ()> 0) {26 // there is data 27 // set adapter 28 videoPagerAdapter = new VideoPagerAdapter (context, mediaItems, true); 29 listview. setAdapter (videoPagerAdapter); 30 // hide the text 31 TV _nomedia.setVisibility (View. GONE); 32} else {33 // No data 34 // text display 35 TV _nomedia.setVisibility (View. VISIBLE); 36} 37 38 39 // ProgressBar hide 40 pb_loading.setVisibility (View. GONE); 41} 42}; 43 44/*** 45 * initializes the control of the current page, with the parent class Call 46 * @ return 47 */48 @ Override 49 public View initView () {50 View view = View. inflate (context, R. layout. video_pager, null); 51 listview = (ListView) view. findViewById (R. id. listview); 52 TV _nomedia = (TextView) view. findViewById (R. id. TV _nomedia); 53 pb_loading = (ProgressBar) view. findViewById (R. id. pb_loading); 54 // set the Click Event of the ListView Item 55 listview. setOnItemClickListener (new MyOnItemClickListen Er (); 56 return view; 57} 58 59 class MyOnItemClickListener implements AdapterView. OnItemClickListener {60 61 @ Override 62 public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {63 64 MediaItem mediaItem = mediaItems. get (position); 65 // Toast. makeText (context, "mediaItem =" + mediaItem. toString (), Toast. LENGTH_SHORT ). show (); 66 67 // 1. enable all playing of the system-implicit Intent 68 // intent Intent = new intent (); 69 // Intent. setDataAndType (Uri. parse (mediaItem. getData (), "video/*"); 70 // context. startActivity (intent); 71 72 // 2. call the self-written player-explicit-a playback address 73 // Int Ent intent = new Intent (context, SystemVideoPlayer. class); 74 // intent. setDataAndType (Uri. parse (mediaItem. getData (), "video/*"); 75 // context. startActivity (intent); 76 // 3. transmit list data-object-serialization 77 Intent intent = new Intent (context, SystemVideoPlayer. class); 78 Bundle bundle = new Bundle (); 79 bundle. putSerializable ("videolist", mediaItems); 80 intent. putExtras (bundle); 81 intent. putExtra ("position", positi On); 82 context. startActivity (intent); 83 84} 85} 86 87 88 @ Override 89 public void initData () {90 super. initData (); 91 LogUtil. e ("local video data is initialized... "); 92 // load the local video data 93 getDataFromLocal (); 94} 95 96/** 97 * get the data 98 */1 from the local sdcard. traverse sdcard with the suffix 99 * // 2. obtain video 100 * // 3 from the content provider. for a 6.0 system, dynamically obtain the permission to read sdcard 101 */102 private void getDataFromLocal () {103 104 new Thread () {105 @ Override106 public void run () {107 super. run (); 108 109 // isGrantExternalRW (Activity) context); 110 // SystemClock. sleep (2000); 111 mediaItems = new ArrayList <> (); 112 ContentResol Ver resolver = context. getContentResolver (); 113 Uri uri = MediaStore. video. media. EXTERNAL_CONTENT_URI; 114 String [] objs = {115 MediaStore. video. media. DISPLAY_NAME, // The name of the video file in sdcard 116 MediaStore. video. media. DURATION, // The total video DURATION is 117 MediaStore. video. media. SIZE, // the video file SIZE is 118 MediaStore. video. media. DATA, // The absolute video address 119 MediaStore. video. media. ARTIST, // Song ARTIST 120 121}; 122 Cursor cursor = resolver. query (uri, objs, Null,); 123 if (cursor! = Null) {124 while (cursor. moveToNext () {125 126 MediaItem mediaItem = new MediaItem (); 127 128 mediaItems. add (mediaItem); // write it on the top 129 130 String name = cursor. getString (0); // The video name 131 mediaItem. setName (name); 132 133 long duration = cursor. getLong (1); // The video duration is 134 mediaItem. setDuration (duration); 135 136 long size = cursor. getLong (2); // the video file size is 137 mediaItem. setSize (size); 138 139 String data = cursor. getString (3); // The Video Stream playback address 140 mediaItem. setData (data); 141 142 String artist = cursor. getString (4); // artist 143 mediaItem. setArtist (artist); 144} 145 146 cursor. close (); 147} 148 149 // Handler sends the message 150 handler. senddemptymessage (10); 151} 152 }. start (); 153 154} 155 156/** 157 * solve the problem that android 6.0 and later versions cannot read external storage permissions. 158 * @ param activity159 * @ return160 */161 public static boolean isGrantExternalRW (Activity activity) {162 if (Build. VERSI ON. SDK_INT> = Build. VERSION_CODES.M & activity. checkSelfPermission (163 Manifest. permission. WRITE_EXTERNAL_STORAGE )! = PackageManager. PERMISSION_GRANTED) {164 165 activity. requestPermissions (new String [] {166 Manifest. permission. READ_EXTERNAL_STORAGE, 167 Manifest. permission. WRITE_EXTERNAL_STORAGE168}, 1); 169 170 return false; 171} 172 173 return true; 174} 175 176}
VideoPager. java

 

3. Set the item click event to play the video.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.