Mobile audio 14th day, local music list display and playback (using video playback layout)

Source: Internet
Author: User

Code has been hosted into the code cloud, interested small partners can download to see

Https://git.oschina.net/joy_yuan/MobilePlayer


650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9D/09/wKioL1l5WtfTvbtnAACZXgsvkO8317.jpg-wh_500x0-wm_ 3-wmp_4-s_2640459651.jpg "title=" qq picture 20170727111543.jpg "alt=" Wkiol1l5wtftvbtnaaczxgsvko8317.jpg-wh_50 "/>


The Android system provides interfaces such as Mediascanner,mediaprovider,mediastore, and provides a set of database tables that are available to users through content provider. When the phone is powered on or an SD card is plugged in and other events occur, the system will automatically scan the SD card and phone memory media files, such as audio,video, pictures, etc., put the corresponding information into a defined database table. In this program, we do not need to care about how to scan the phone files, as long as you know how to query and use this information.

A series of data tables are defined in Mediastore, and we can get all the information we need through the query interface provided by Contentresolver. Below we focus on querying the music file information on the SD card.

Let's look at the query interface for Contentresolver:

Cursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder);


Uri: Indicates the name of the database to query, plus the name of the table, from the mediastore we can find the parameters of the corresponding information, please refer to the development documentation.

Projection: Specifies which columns in the database table are queried, and the corresponding information is included in the returned cursor. NULL returns all information.

Selection: Specifying query criteria

Selectionargs: Is there a parameter in selection? This symbol is where the actual value can be substituted for this question mark. What if selection this? , then this string array can be null.

SortOrder: Specifying the order in which query results are sorted



First, music player data acquisition

1, according to Videopager to obtain local video methods, but also from the Android system to obtain local audio

/** *  from the local SD card to obtain data, there are 2 ways to  * 1, traverse the SD card, according to the suffix name  * 2, from the content provider, the system has its own to scan all media information.  * 3/6.0 after the system, need to add dynamic Permissions  */private void getdatafromlocal ()  {     mediaList=new ArrayList<> ();     new thread () {          @Override         public void  Run ()  {            super.run ();             systemclock.sleep (;    )         //to get content resolver based on context              contentresolver resolver = context.getcontentresolver ();             Uri uri=  Mediastore.audio.media.external_content_uri;            string[] objs={                      mediastore.audio.media.display_name, //Video file name                      mediastore.audio.media.duration,  //Video Duration                      mediastore.audio.media.size, //File Size                      mediastore.audio.media.data, //the absolute address of the video                      mediastore.audio.media.artist,  //singer, artist (audio may have this field)              }; &Nbsp;          cursor cursor = resolver.query (uri, objs, null, null, null);             if  (cursor!=null) {                 while (Cursor.movetonext ()) {                     mediaitem item=new mediaitem ();                      string name=cursor.getstring (0);  //name                      item.setname (name);                     long  duration=cursor.getlong (1);  //                     item.setduration (Duration);                     long size=cursor.getlong (2);  // Audio size                      item.setsize (size);                     string data=cursor.getstring (3); absolute address of  //audio                       item.setdata (data);                     string artist=cursor.getstring (4);  //Artists           &nBsp;          item.setartist (artist);                      Medialist.add (item);  //put each item data in the collection                  }                 cursor.close ();             }             //messages, prompting to load the audio   in media           handler.sendemptymessage (0);         }    }.start ();}

2, after loading all audio data, will send a message to Handler,handler to Setadapter

Private handler handler=new handler () {     @Override      Public void handlemessage (message msg)  {         Super.handlemessage (msg);        if  (mediaList!=null&& Medialist.size () >0) {            //have data              //Setting Adapter              myadapter=new myadapter (Context,medialist,isvideo);             listview.setadapter (Myadapter);             //toast.maketext (Context, "with data", Toast.length_short). Show ();             nomedia.setvisibility (View.GONE);         }else{            //No data              //Text Display              toast.maketext (Context, "no data", Toast.length_short). Show ();         }        pb_loding.setvisibility ( View.gone);         //progressbar Hidden     }};

3, in the public myadapter to display the data of the ListView

package com.yuanlp.mobileplayer.adapter;import android.content.context;import  android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import  Android.widget.baseadapter;import android.widget.imageview;import android.widget.textview;import  com.yuanlp.mobileplayer.R;import com.yuanlp.mobileplayer.bean.MediaItem;import  com.yuanlp.mobileplayer.utils.utils;import java.util.list;/** * created by  原立鹏   on 2017/7/16. */public class myadapter extends baseadapter {     private Context context;    private List<MediaItem>  Medialist;    private utils utils;    private boolean  isvideo;    public myadapter (context context,list<mediaitem>  Medialist,boolean isvideo) {&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;   this.context=context;        this.medialist= medialist;        this.isvideo=isvideo;        utils=new utils ();    }     @Override      public int getcount ()  {        return  Medialist.size ();    }     @Override     public  object getitem (int position)  {        return  medialist.get (position);    }     @Override      public long getitemid (int position)  {         return position;    }     @Override      Public view getview (Int poSition, view convertview, viewgroup parent)  {         ViewHolder viewHolder=null;        if  ( Convertview==null) {            view view  = layoutinflater.from (context). Inflate (R.layout.medialayout, null);             convertView=view;             viewholder=new viewholder ();             viewHolder.iv_icon=  (ImageView)  convertview.findviewbyid (R.id.iv_icon) ;            viewholder.tv_name=  (TextView)  convertview.findviewbyid (R.id.tv_name);             viewHolder.tv_time=  (TextView)  convertview.findviewbyid (r.id.tv_time);             viewHolder.tv_size=  (TextView)  convertview.findviewbyid (r.id.tv_size);             convertview.settag (Viewholder);         }else{             viewholder=  (Viewholder)  convertview.gettag ();         }         //Get Data          Mediaitem item=medialist.get (position);         viewholder.tv_ Name.settext (Item.getname ());         viewholder.tv_size.settext ( Android.text.format.Formatter.formatFileSize (Context,item.getsize ()));         viewholder.tv_time.settext (Utils.strinGfortime ((int)  item.getduration ()));        if  (!isVideo) {             //from audio, then replace the picture in the ListView              viewholder.iv_icon.setimageresource ( R.DRAWABLE.MUSIC_DEFAULT_BG);        }         return convertView;    }    /**      *  public control class containing the controls to be displayed for each row      */    private  static class viewholder{        imageview iv_icon ;        textview tv_name;         TextView tv_time;        TextView tv_size;     }}


So local music can also show and play, but the play page is shared with the video page, is ugly, so behind will do a music-specific playback interface, then there are lyrics display, please look forward to.

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1951354

Mobile audio 14th day, local music list display and playback (using video playback layout)

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.