Write a app--day 1th day

Source: Internet
Author: User

Write a app--every day 1Day 1. Write a music player

--Write a "Musicplayer" APP today that is a music player. Enables simple music playback functions. ^_^.
How to use: Enter the name of the song you want to listen to in the text box (if it is on the phone), then click Play to enjoy the music! You may say, this is too low! Rest assured, you will love to write and try this app! Besides, this is just the first step! We gradually perfected this app!

编程关键词!:

    • Use of the MediaPlayer class
    • Life cycle of activity
    • How to get rid of Titiebar
    • Use of LinearLayout
    • V_v Your interface Design aesthetics
2.APP Main interface

It is not a kind of mdesign feet (obviously far-fetched), later will be more use of this design language. About the design of the software interface, must be intentions, not only focus on background procedures. For mobile apps that require a high level of user experience, you have to spend a certain amount of thought. Of course, this and your aesthetic has a great relationship!!!! Interface design needs a certain art technology, I can draw, but PS God code software is really not familiar, it's okay, this site is excellent: Easyicon

On the Code

详细解释请看注释

Interface XML

It's a simple interface file.

<relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " android:background  =" #ff646469 " tools:context  =;     <linearlayout  android: Layout_width  = "match_parent"  android:layout_ Height  = "match_parent"  android:orientation
      = "vertical"  android:weightsum  = "1" ;         <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_gravity= "center"  android:paddingtop  = "15DP"  android:textsize  = "35SP"             android:textcolor  = "#fff"  android:text  = "music player" />         <textview  android:layout _margintop  = "25DP"  android:id  = "@+id/text_songname"  android:layout_width  = "wrap_content"  android:layout_height  =" wrap_content " android:layout_gravity  =" center " android:textcolor  =" #fff " android:textsize  = "19SP"  
      
       android:text 
       = "good sister" />         <imageview  android:layou T_margintop  = "15DP"  android:id  = "@+id/image_songimage"  android:layout_width  = "290DP"  android:layout_height  =" 290DP " android:layout_gravity  =" cent Er " android:src  =" @drawable/CD "/>< /span>        <edittext  android:layout _gravity  = "center"  android:id  = "@+id/edit_songname"  android:layout_width  = "wrap_content"  android:layout_height  =" wrap_content " android:hint  =" input song Name "/>         <linearlayout  android:la Yout_margintop  = "30DP"  android:layout_width< /span>= "match_parent"  android:layout_height  = "wrap_content"  android:orientation  = "horizontal" ;             <button  android:id
      = "@+id/button_pause"  android:layout_width  = "wrap_content"  android:layout_height
       = "wrap_content"  android:layout_weight= "3.333"  android:textcolor  = "#ff5c5c5c"  android:text  =" pause "/>             <button  android:id
      = "@+id/button_start"  android:layout_width  = "wrap_content"  android:layout_height
       = "wrap_content"  android:layout_weight= "3.333"  android:textcolor  = "#ff5c5c5c"  android:text  =" play "/>             <buttonandroid:id="@+id/button_stop"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:layout_weight=" 3.333 "  Android:textcolor="#ff5c5c5c"android:text="Stop"/>                                                                                                        </linearlayout>    </linearlayout></relativelayout>
Java Code--mainactivity.java

MainActivity.java

 PackageCom.river.root.musicplayer;Importandroid.app.Activity;ImportAndroid.media.MediaPlayer;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.animation.Animation;ImportAndroid.view.animation.RotateAnimation;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportJava.io.IOException; Public  class mainactivity extends Activity {    PrivateImageView Songimage;PrivateTextView Songtext;PrivateButton Songstart;PrivateButton Songstop;PrivateButton Songpause;PrivateEditText Editsongname;PrivateMediaPlayer Musicplayer;PrivateHandler Handler;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Bound controlSongimage= (ImageView) Findviewbyid (r.id.image_songimage);        songtext= (TextView) Findviewbyid (r.id.text_songname);        Editsongname= (EditText) Findviewbyid (r.id.edit_songname);        Songstart= (Button) Findviewbyid (R.id.button_start);        songstop= (Button) Findviewbyid (r.id.button_stop); Songpause= (Button) Findviewbyid (r.id.button_pause);//instantiation of MediaPlayerMusicplayer=NewMediaPlayer ();//Set a listener for three buttonsSongpause.setonclicklistener (NewButtonlistener ()); Songstop.setonclicklistener (NewButtonlistener ()); Songstart.setonclicklistener (NewButtonlistener ()); }Private  class Buttonlistener implements View. Onclicklistener {        @Override         Public void OnClick(View view) {Try{Switch(View.getid ()) { CaseR.id.button_start://File nameString Filename=editsongname.gettext (). toString (); Songtext.settext (FileName);//Restore Original interface                        if(Songpause.gettext (). Equals ("Continue") {Songpause.settext ("Pause"); }//ResetMusicplayer.reset ();//file pathMusicplayer.setdatasource ("/sdcard/netease/cloudmusic/music/"+filename+". mp3");                        Musicplayer.prepare (); Musicplayer.start (); Break; CaseR.id.button_stop:musicplayer.stop (); Songpause.settext ("Pause"); Break; CaseR.id.button_pause://Notice changes to the interface                        if(Musicplayer.isplaying ())                            {Musicplayer.pause (); (Button) view. SetText ("Continue"); }Else{Musicplayer.start (); (Button) view. SetText ("Pause"); } Break; }            }Catch(IOException e)            {E.printstacktrace (); }        }    }@Override    protected void OnPause() {Super. OnPause ();if(musicplayer!=NULL){if(Musicplayer.isplaying ()) musicplayer.stop (); }    }@Override    protected void Onresume() {Super. Onresume ();if(musicplayer!=NULL){if(Musicplayer.isplaying ()) Musicplayer.start (); }    }@Override    protected void OnDestroy() {Super. OnDestroy ();if(musicplayer!=NULL){if(Musicplayer.isplaying ()) musicplayer.stop ();        Musicplayer.release (); }    }}

The above code is not very difficult, mainly the use of the MediaPlayer class. But pay attention to the change of interface and the application of activity life cycle.

Tomorrow's App

Realize the rotation of the middle picture, and show the progress of the play.

Write a app--day 1th day

Related Article

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.