Android music player (prototype)

Source: Internet
Author: User

Suggestion: before learning this instance, Please master the activity lifecycle related events and methods, so that the learning effect will be better.
 

This instance is for reference only and is not a perfect product. Due to time and technical limitations, we apologize for any shortcomings or errors. Hope enthusiastic netizens can continue to improve.

Below is part of the activity code (I usually have detailed comments ):

Package CN. chaoyang. activity; import Java. io. file; import Java. io. ioexception; import android. app. activity; import android. media. mediaplayer; import android. OS. bundle; import android. OS. environment; import android. text. boringlayout. metrics; import android. view. view; import android. widget. button; import android. widget. edittext; // before learning this instance, Please master the activity lifecycle and related methods, so that the learning effect will be better. Public class mainactivity extends activity {private mediaplayer; private edittext txtname; private int postion; private string filename;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); buttonclicklistener listener = new buttonclicklistener (); txtname = (edittext) Th Is. findviewbyid (R. id. inputname); button btnplay = (button) This. findviewbyid (R. id. btnplay); button btnpause = (button) This. findviewbyid (R. id. btnpause); button btnstop = (button) This. findviewbyid (R. id. btnstop); button btnresart = (button) This. findviewbyid (R. id. btnrestart); btnplay. setonclicklistener (listener); btnpause. setonclicklistener (listener); btnstop. setonclicklistener (listener); btnresart. seton Clicklistener (listener);} // after the system recovers, you can re-read the previously saved status value @ overrideprotected void onrestoreinstancestate (bundle savedinstancestate) {This. filename = savedinstancestate. getstring ("FILENAME"); this. postion = savedinstancestate. getint ("postion"); super. onrestoreinstancestate (savedinstancestate);} // in case of an accident, save some status values @ overrideprotected void onsaveinstancestate (bundle outstate) {outstate before the system kills the activity process. putstri Ng ("FILENAME", filename); outstate. putint ("postion", postion); super. onsaveinstancestate (outstate);} // The ondestroy method can kill the program process and completely release the resource @ overrideprotected void ondestroy () {mediaplayer. release (); super. ondestroy () ;}// if the call is over, continue playing the music @ overrideprotected void onresume () {If (postion> 0 & filename! = NULL) {try {play ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} mediaplayer. seekto (postion); postion = 0;} super. onresume () ;}// if you call or send a text message, acticity will pause and stop playing music @ overrideprotected void onpause () {If (mediaplayer. isplaying () {postion = mediaplayer. getcurrentposition (); // Save the current playback point mediaplayer. stop ();} super. onpause ();} private final class buttonclicklistener implements view. oncl Icklistener {@ overridepublic void onclick (view v) {// todo auto-generated method stubmediaplayer = new mediaplayer (); button = (button) V; try {Switch (v. GETID () {// playback case R. id. btnplay: If (! Mediaplayer. isplaying () {play ();} break; // pause case R. id. btnpause: // if the video is being played, press the button to pause. and the text on the button is displayed as "Pause continue); // set the button text} else {// if it is paused, press the button to continue playing // play ();} break; // stop case R. id. btnstop: If (mediaplayer. isplaying () {mediaplayer. stop ();} break; // repeat case R. id. btnrestart: If (mediaplayer. isplaying () {mediaplayer. seekto (0) ;}else {play () ;}break ;}} catch (exception e) {// todo: handle exception }}} private void play () throws ioexception {// obtain the absolute path of the music file filename = txtname. gettext (). tostring (); file = new file (environment. getexternalstoragedirectory (), filename); mediaplayer. reset (); // return mediaplayer. setdatasource (file. getabsolutepath (); // sets the data source mediaplayer to be played. prepare (); mediaplayer. start ();}}

The following is the software layout file code, which is a simple linear layout.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />    <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/labName"    />    <EditText     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/inputName"    />    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/play"    android:id="@+id/btnPlay"    />     <Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/pause"    android:id="@+id/btnPause"    />     <Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/stop"    android:id="@+id/btnStop"    />     <Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/restart"    android:id="@+id/btnRestart"    />    </LinearLayout></LinearLayout>

The following is the string. XML code of the resource file.

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, mainactivity! </String> <string name = "app_name"> MP3 player </string> <string name = "labname"> input song name </string> <string name = "play"> play </string> <string name = "pause"> pause </string> <string name = "stop"> stop </string> <string name = "restart"> duplicate </string> <string name = "txtcontinue"> continue </string> </resources>

 

 

The purpose of this example is to familiarize yourself with the use of audio interfaces and related operations in Android, and to consolidate the knowledge about the activity lifecycle. As for the page layout, the display and control style is very silly.

If you want to develop a complete (music player-related) product, there are too many things to be improved.

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.