Preface
Keywords: Vitamio, VPlayer, Android player, Android audio and video, and Android open-source player
This chapter Android universal Player Local playback of the main functions (Cache playback list and A-Z quick query function) completed, and play components have little to do, but use some practical technology, welcome to exchange!
StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com
Series
1. Use Vitamio to build your Android universal player (1) -- Preparation
2. Use Vitamio to build your Android universal player (2)-gesture control of brightness, volume, and scaling
3. Use Vitamio to create your Android universal player (3)-local playback (main interface and playback list)
Body
I. Objectives
1.1 A-Z Quick Switch find videos
The A-Z on the contacts on the phone quickly found the use of this, find files more convenient. This is also the MI chat of "learning ".
1.2 cache scan video list
Scan the SD card for the first time and read it from the database. Add a listener in the next article.
1.3
II. Implementation
Core code:
Public class FragmentFile extends FragmentBase implements OnItemClickListener {
Private FileAdapter mAdapter;
Private TextView first_letter_overlay;
Private ImageView alphabet_scroller;
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
View v = super. onCreateView (inflater, container, savedInstanceState );
//~~~~~~~~~ Bind controls
First_letter_overlay = (TextView) v. findViewById (R. id. first_letter_overlay );
Alphabet_scroller = (ImageView) v. findViewById (R. id. alphabet_scroller );
//~~~~~~~~~ Bind event
Alphabet_scroller.setClickable (true );
Alphabet_scroller.setOnTouchListener (asOnTouch );
MListView. setOnItemClickListener (this );
//~~~~~~~~~ Load data
If (new SQLiteHelper (getActivity (). isEmpty ())
New scanvideotask(cmd.exe cute ();
Else
New datatask(cmd.exe cute ();
Return v;
}
/** Click start playback */
@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id ){
Final PFile f = mAdapter. getItem (position );
Intent intent = new Intent (getActivity (), VideoViewDemo. class );
Intent. putExtra ("path", f. path );
StartActivity (intent );
}
Private class DataTask extends AsyncTask <Void, Void, ArrayList <PFile> {
@ Override
Protected void onPreExecute (){
Super. onPreExecute ();
MLoadingLayout. setVisibility (View. VISIBLE );
MListView. setVisibility (View. GONE );
}
@ Override
Protected ArrayList <PFile> doInBackground (Void... params ){
Return FileBusiness. getAllSortFiles (getActivity ());
}
@ Override
Protected void onPostExecute (ArrayList <PFile> result ){
Super. onPostExecute (result );
MAdapter = new FileAdapter (getActivity (), FileBusiness. getAllSortFiles (getActivity ()));
MListView. setAdapter (mAdapter );
MLoadingLayout. setVisibility (View. GONE );
MListView. setVisibility (View. VISIBLE );
}
}
/** Scan SD card */
Private class ScanVideoTask extends AsyncTask <Void, File, ArrayList <PFile> {
Private ProgressDialog pd;
Private ArrayList <File> files = new ArrayList <File> ();
@ Override
Protected void onPreExecute (){
Super. onPreExecute ();
Pd = new ProgressDialog (getActivity ());
Pd. setMessage ("scanning video files ...");
Pd. show ();
}
@ Override
Protected ArrayList <PFile> doInBackground (Void... params ){
//~~~ Traverse folders
EachAllMedias (Environment. getExternalStorageDirectory ());
//~~~ Warehouse receiving
SQLiteHelper sqlite = new SQLiteHelper (getActivity ());
SQLiteDatabase db = sqlite. getWritableDatabase ();
Try {
Db. beginTransaction ();
SQLiteStatement stat = db. compileStatement ("insert into files (" + FilesColumns. COL_TITLE + "," + FilesColumns. COL_TITLE_PINYIN + "," + FilesColumns. COL_PATH + "," + FilesColumns. COL_LAST_ACCESS_TIME + ") VALUES (?,?,?,?) ");
For (File f: files ){
String name = FileUtils. getFileNameNoEx (f. getName ());
Int index = 1;
Stat. bindString (index ++, name); // title
Stat. bindString (index ++, PinyinUtils. chineneToSpell (name); // title_pinyin
Stat. bindString (index ++, f. getPath (); // path
Stat. bindLong (index ++, System. currentTimeMillis (); // last_access_time
Stat.exe cute ();
}
Db. setTransactionSuccessful ();
} Catch (BadHanyuPinyinOutputFormatCombination e ){
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Db. endTransaction ();
Db. close ();
}
//~~~ Query data
Return FileBusiness. getAllSortFiles (getActivity ());
}
@ Override
Protected void onProgressUpdate (final File... values ){
File f = values [0];
Files. add (f );
Pd. setMessage (f. getName ());
}
/** Traverse all folders to find the video file */
Public void eachAllMedias (File f ){
If (f! = Null & f. exists () & f. isDirectory ()){
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 );
}
}
}
}
}
@ Override
Protected void onPostExecute (ArrayList <PFile> result ){
Super. onPostExecute (result );
MAdapter = new FileAdapter (getActivity (), result );
MListView. setAdapter (mAdapter );
Pd. dismiss ();
}
}
Private class FileAdapter extends ArrayAdapter <PFile> {
Public FileAdapter (Context ctx, ArrayList <PFile> l ){
Super (ctx, l );
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
Final PFile 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. title );
Return convertView;
}
}
/**
* A-Z
*/
Private OnTouchListener asOnTouch = new OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN: // 0
Alphabet_scroller.setPressed (true );
First_letter_overlay.setVisibility (View. VISIBLE );
MathScrollerPosition (event. getY ());
Break;
Case MotionEvent. ACTION_UP: // 1
Alphabet_scroller.setPressed (false );
First_letter_overlay.setVisibility (View. GONE );
Break;
Case MotionEvent. ACTION_MOVE:
MathScrollerPosition (event. getY ());
Break;
}
Return false;
}
};
/**
* Display characters
*
* @ Param y
*/
Private void mathScrollerPosition (float y ){
Int height = alphabet_scroller.getHeight ();
Float charHeight = height/28366f;
Char c = 'a ';
If (y <0)
Y = 0;
Else if (y> height)
Y = height;
Int index = (int) (y/charHeight)-1;
If (index <0)
Index = 0;
Else if (index> 25)
Index = 25;
String key = String. valueOf (char) (c + index ));
First_letter_overlay.setText (key );
Int position = 0;
If (index = 0)
MListView. setSelection (0 );
Else if (index = 25)
MListView. setSelection (mAdapter. getCount ()-1 );
Else {
For (PFile item: mAdapter. getAll ()){
If (item. title_pinyin.startsWith (key )){
MListView. setSelection (position );
Break;
}
Position ++;
}
}
}
}
Code Description:
The Code is based on the previous article and adds the playlist cache and quick search functions.
A). The pinyin4j open-source project is used to extract Chinese characters in the file name, so that you can.
B). A-Z this part of the code is through the decompilation reference chat, more practical value
C). transactions are used in the warehouse receiving part.
For other code, see the project code.
Note:The sample code is not fully considered and may be supplemented in subsequent chapters. Please be careful not to use the Code directly! For example, check whether the SD card is available.
Iii. Project download
Vitamio-Demo2012-6-8.zip
Iv. Vtamio and VPlayer
Vitamio: http://vov.io
VPlayer: http://vplayer.net (with more than 5 million users using Vitamio's most successful products)
End
This week, I was a little busy releasing a new version. I wrote this article only on Friday. I finally tried my best to lean towards a formal product even though I didn't have the break-in function, provide useful code as much as possible.