Recent. NET open source really let C # fire A, long heard that mono for Android has not been quiet to see, last weekend to look, indeed good, front-end interface Axml writing with Java Android Development no difference, backstage with C # In fact, a lot of Windows methods can be used, Do an Android music player, mainly achieve music playback, on a Duflot, music playlist, random play, scan all the music in SD card.
First show a set of interface Ah, say there is a picture of the truth AH:
Project structure
The LRC is the lyrics processing method
Musicservice is an Android service that plays music, serves broadcast, and interfaces to receive receiver
SqlHelper is a number of methods for the local database SQLite after the song scan.
Talk about a few difficult points:
Music Scan:
The main is to traverse the directory, the code is as follows
ilist<filesysteminfo> visiblethings = new list<filesysteminfo> (); var dir = new DirectoryInfo (directory); try {foreach (var item in dir. Getfilesysteminfos (). Where (item = Item. IsVisible ())) {Visiblethings.add (item); Isdirectory ()) {refreshfileslist (item. FullName);} if (item. Isfile () && (item. Fullname.substring (item. Fullname.lastindexof (".") + 1)) = = "MP3") {//is a music file}
ListView Add data, this mainly used to Listadapter,listadapter is to create a new foreground XAML interface, each interface to store a song, and then finally the traversal of all songs into n interface, which n interface added to the ListView,
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/linearlayout1 "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "&G T <textview android:id= "@+id/songid" android:layout_width= "wrap_content" android:layout_height= "0dip "Android:textcolor=" #FF7F3300 "android:textsize=" 0dip "android:textstyle=" Italic "Android:vis ibility= "Invisible"/> <textview android:id= "@+id/songtitle" android:layout_width= "Wrap_content" android:layout_height= "50dip" android:textsize= "20dip" android:textcolor= "#FFFFFF" Android:paddi ngleft= "5dip" android:gravity= "Center_vertical|left"/> <textview android:id= "@+id/songurl" Android:layout_width= "Wrap_content" android:layout_height= "0dip" android:visibility= "Invisible"/></li Nearlayout>
Background code:
Using system;using system.collections.generic;using system.linq;using system.text;using Android.App;using Android.content;using android.os;using android.runtime;using android.views;using Android.Widget;using Android.Util; namespace Musicplayer{public class songlistadapter:baseadapter<songdetail>{list<songdetail> items; Activity context;public Songlistadapter (activity context, list<songdetail> items): Base () {This.context = context ; this.items = items;} public override long Getitemid (int position) {return position;} public override Songdetail This[int Position]{get {return items[position];}} public override int Count{get {return items. Count; }}public override view GetView (int position, view Convertview, ViewGroup parent) {var item = items[position]; View view = convertview;if (view = = null) View = context. Layoutinflater.inflate (Resource.Layout.SongItem, null); view. Findviewbyid<textview> (Resource.Id.songid). Text = item.id. ToString (); view. Findviewbyid<textview>(Resource.Id.songtitle). Text = Item. Title;view. Findviewbyid<textview> (Resource.Id.songurl). Text= (item. Path); return view;}}}
ListView Join Method:
Lv = findviewbyid<listview> (Resource.Id.listView1); Lv.adapter = new Songlistadapter (this, sonlist);
Where Sonlist is a list of all the songs that are traversed except the collection
As for the Android service, the main thing is to inherit service, the main code:
[Service] public class Mainservice:service {public override void OnCreate () { base. OnCreate (); Log.debug ("Xamarin", "Create Service"); public override Startcommandresult Onstartcommand (Android.Content.Intent Intent, startcommandflags flags, int startid) { log.debug ("Xamarin", "Start Service"); return startcommandresult.sticky; } public override void OnDestroy () { base. OnDestroy (); Log.debug ("Xamarin", "close Service"); public override Android.OS.IBinder Onbind (Android.Content.Intent Intent) { return null; }}
The last lyrics show, the main use is to parse the lyrics, parsing lyrics C # below There are code to refer to, do not know how to parse the search WinForm do music player lyrics is how to do, in fact, a play but money position and play time converted into position a match, Here's how to parse the lyrics:
public class LRC {//<summary>///song//</summary> public string Title {ge T Set }///<summary>//Artist///</summary> public string Artist {get; set;} <summary>////album///</summary> public string Album {get; set;} <summary>///Lyrics author///</summary> public string Lrcby {get; set;} <summary>///offset///</summary> public string offset {get; set;} <summary>//lyrics///</summary> public dictionary<double, string> Lrcword = new Dictionary<double, string> (); <summary>///Receive lyrics information///</summary>//<param name= "Lrcpath" > Lyrics path </param> ; <returns> Return lyrics information (LRC instance) </returns> public static LRC INITLRC (string lrcpath) {LRC LRC = new LRC (); using (FileStream fs = new FileStream (Lrcpath, FileMode.Open, FileAccess.Read, FileShare.Read)) { String line; using (StreamReader sr = new StreamReader (FS, Encoding.default)) {while (line = Sr. ReadLine ()) = null) {if (line. StartsWith ("[Ti:")) {LRC. Title = Splitinfo (line); } else if (line. StartsWith ("[AR:")]) {LRC. Artist = Splitinfo (line); } else if (line. StartsWith ("[Al:")) {LRC. Album = Splitinfo (line); } else if (line. StartsWith ("[By:")) {LRC. Lrcby = Splitinfo (line); } else if (line. StartsWith ("[Offset:")) {LRC. Offset = Splitinfo (line); } else {regex regex = new Regex (@ "\[([0-9.:]*) \] + (. *) ", regexoptions.compiled); MatchCollection mc = Regex. Matches (line); Double time = Timespan.parse ("XX:" + mc[0). GROUPS[1]. Value). TotalSeconds; String word = mc[0]. GROUPS[2]. Value; lRC. Lrcword.add (time, word); }}}} return LRC; }///<summary>//handling information (private method)///</summary>//<param name= "line" ></pa ram>//<returns> return basic information </returns> static string Splitinfo (string line) {R Eturn line. Substring (line. IndexOf (":") + 1). TrImend ('] '); }} One line of code: LRC lrc= LRC.INITLRC ("TEST.LRC");
It's almost done, and finally a menu is created
public override bool Oncreateoptionsmenu (Imenu menu) {var m1 = menu. ADD (0, 1, 0, "song list"); var m2 = menu. ADD (0,2,0, "exit"); return base. Oncreateoptionsmenu (menu);} public override bool Onoptionsitemselected (Imenuitem Item) {switch (item). ITEMID) {case 1:break;case 2:break;} Return base. onoptionsitemselected (item);}
Well, that's almost all, as for music playback is done with the Mediaplaye system API.
Xamarin.android Developing music player