Android uses Vitamio to build its own universal player (3)--local playback (main interface, playlist) _android

Source: Internet
Author: User

Objective

Create a complete available Android player has many features and details to be completed, but also involves a variety of knowledge and content, this chapter will be combined with fragment, Viewpager to build the main player interface, and achieve local playback basic functions. Series of articles to provide screenshots, code description, source download, Welcome to communicate!

Series

1.Android uses Vitamio to build its own universal player (1)--ready

2,Android uses Vitamio to build its own universal player (2)--gesture control brightness, volume, zoom

Body

I. Objectives

1.1 Use fragment, Viewpager to build the main interface

Main function area is divided into: local video, online video, allow sliding switch module

1.2 Display SDcard all video with ListView

Screenshot of Effect:

(statement: Icons are from the network, only for learning research!)

Second, the implementation of the Code

2.1 XML

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:gravity=" center_horizontal "android:layout_width=" Match_parent " android:layout_height= "Match_parent" > <radiogroup android:gravity= "center_vertical" Fill_parent "android:layout_height=" wrap_content "android:orientation=" horizontal "> <RadioButton android:id= "@+id/radio_file" android:checked= true "style=" @style/main_tab_bottom "android:drawabletop=" @drawable/video_file "Android:text=" @string/title_file "/> <radiobutton android:id=" @+id/radio_online "android:drawabletop=" @ Drawable/video_online "style=" @style/main_tab_bottom "android:text=" @string/title_online "/> </radiogroup > <android.support.v4.view.viewpager android:background= "@color/background" android:id= "@+id/pager" Android: Layout_width= "Match_parent" android:layout_height= "match_parent" > </android.suPport.v4.view.viewpager> </LinearLayout> 

This is the overall layout, use RadioButton to switch local video and online video features, specific styles please download items. Viewpager support left and right side sideslip switch function.

2.2 Class

mainfragmentactivity

public class Mainfragmentactivity extends Fragmentactivity {

 private viewpager Mpager;

 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (R.layout.fragment_pager);
 Mpager = (Viewpager) Findviewbyid (R.id.pager);
 Mpager.setadapter (Madapter);
 }

 Private Fragmentpageradapter Madapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {

 /** only execute once/
 @Override public
 Fragment getitem (int position) {
 Fragment result = null;
 Switch (position) {case
 1: Result
 = new Fragmentonline ();//online video break
 ;
 Case 0:
 default: Result
 = new Fragmentfile ()//local video break
 ;
 }
 return result;
 }

 @Override public
 int GetCount () {return 2;}}
 ;
}

Here is a simple example of the use of fragment in conjunction with Viewpager.

Fragmentfile

public class Fragmentfile extends Fragmentbase implements Onitemclicklistener {private Fileadapter madapter; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View v = s
 Uper.oncreateview (Inflater, container, savedinstancestate);
 Madapter = new Fileadapter (getactivity (), NULL);
 Mlistview.setadapter (Madapter);
 Mlistview.setonitemclicklistener (this);
 New Scanvideotask (). Execute ();
 return v; /** Click Start Play * * @Override public void Onitemclick (adapterview<?> parent, view view, int position, long id) {fi
 nal File f = madapter.getitem (position);
 Intent Intent = new Intent (getactivity (), videoviewdemo.class);
 Intent.putextra ("Path", F.getpath ());
 StartActivity (Intent); /** Scan SD Card */private class Scanvideotask extends Asynctask<void, File, void> {@Override protected Void doin
 Background (Void ... params) {Eachallmedias (Environment.getexternalstoragedirectory ());
 return null; } @Override Protected void Onprogressupdate (File ... values) {Madapter.add (values[0]);
 Madapter.notifydatasetchanged (); /** traverse all folders to find out the video file */public void Eachallmedias (file f) {if (f!= null && f.exists () && F.isdirect
 Ory ()) {file[] files = f.listfiles ();
 if (Files!= null) {for (file File:f.listfiles ()) {if (File.isdirectory ()) {Eachallmedias (file);
 else if (file.exists () && file.canread () && fileutils.isvideooraudio (file)) {publishprogress (file); '}}}} Private class Fileadapter extends Arrayadapter<file> {public Fileadapter (context CTX, Arraylis
 T<file> l) {super (CTX, L);
 @Override public View getview (int position, View Convertview, ViewGroup parent) {final File F = getitem (position);
 if (Convertview = null) {final Layoutinflater minflater = getactivity (). Getlayoutinflater ();
 Convertview = minflater.inflate (R.layout.fragment_file_item, NULL); } ((TextView) Convertview.findviewbyid (r.id.title)). SETtext (F.getname ());
 return convertview;  }
 }

}

Code Description:

a). Here is the main feature of this chapter, scanning all video audio files and displaying them.

b). Arrayadapter and fileutils here do not stick to the code, mainly tools and auxiliary classes, please download the project view.

c). Note:The madapter.add operation should be placed in the main thread, otherwise there may be an error.

The above is to the Android Vitamio local playback of data collation, follow-up continue to add thank you.

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.