Write an APP every day _ 1st days-simple music player and app music player

Source: Internet
Author: User

Write an APP every day _ 1st days-simple music player and app music player
Write an APP every day.1Day 1. Write a music player

-- Today, I wrote a "MusicPlayer" APP, which is a music player. Implements simple music playback. ^_^.
How to Use: enter the name of the song you want to listen to in the text box (provided there is in the phone), and click play to enjoy the music! You may say it's too LOW! Rest assured that you will like to write and try this application! Besides, this is only the first step! In the future, we will gradually improve this APP!

Programming keywords! :

  • Use of the MediaPlayer class
  • Activity Lifecycle
  • How to remove TitieBar
  • Use of LinearLayout
  • V_v your Interface Design Aesthetics
2. Main APP Interface

-- Is there a sense of MDesign (obviously far-fetched) that will be used more in the future. You must be careful about the design of the software interface. For mobile apps with extremely high user experience requirements, you must spend a certain amount of time. Of course, this has a lot to do with your aesthetics !!!! The design of the interface requires some art technology. I can draw a picture, but the PS code software is really not familiar, it doesn't matter, this website is great: easyicon

Code on

For more information, see the notes.

Interface xml

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 = ". mainActivity "> <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: layout_marginTop = "15dp" android: id = "@ + id/image_songImage" android: layout_width = "290dp" android: layout_height = "290dp" android: layout_gravity = "center" android: src = "@ drawable/cd"/> <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: layout_marginTop = "30dp" android: layout_width = "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"/> <Button android: 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> </RelativeLayout>
Java code -- MainActivity. java

MainActivity.java

Package com. river. root. musicplayer; import android. app. activity; import android. media. mediaPlayer; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. animation. animation; import android. view. animation. rotateAnimation; import android. widget. button; import android. widget. editText; import android. widget. imageView; import android. widget. tex TView; import java. io. IOException; public class MainActivity extends Activity {private ImageView songImage; private TextView songText; private Button songStart; private Button songStop; private Button songPause; private EditText editSongName; private MediaPlayer musicPlayer; private Handler handler; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); s EtContentView (R. layout. activity_main); // bind the control songImage = (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); // instantiate MediaPlayer musicPlayer = new Me DiaPlayer (); // set the listener songPause for the three buttons. setOnClickListener (new ButtonListener (); songStop. setOnClickListener (new ButtonListener (); songStart. setOnClickListener (new ButtonListener ();} private class ButtonListener implements View. onClickListener {@ Override public void onClick (View view) {try {switch (view. getId () {case R. id. button_start: // file name String fileName = editSongName. getText (). toString (); son GText. setText (fileName); // restore the original interface if (songPause. getText (). equals ("continue") {songPause. setText ("pause");} // reset musicPlayer. reset (); // file path musicPlayer. setDataSource ("/sdcard/netease/cloudmusic/" + fileName + ". mp3 "); musicPlayer. prepare (); musicPlayer. start (); break; case R. id. button_stop: musicPlayer. stop (); songPause. setText ("pause"); break; case R. id. button_pause: // note that if (musicPlayer. isPlaying ()){ MusicPlayer. pause (); (Button) view ). setText ("continue");} else {musicPlayer. start (); (Button) view ). setText ("paused");} 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 for the use of the MediaPlayer class. But pay attention to the changes in the interface and the application of the Activity lifecycle.

Tomorrow's APP

Rotate the intermediate image and display the playback progress.

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.