android--with activity and service for a simple music player

Source: Internet
Author: User

One, only with activity

Xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.chenshuai.myapplication.ActivityMusic"android:orientation= "vertical">    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "40SP"Android:text= "Playback status"android:textsize= "20SP"android:gravity= "Center_horizontal"Android:id= "@+id/tv_1"/>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Play"Android:onclick= "Play_onclick"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Paused"Android:onclick= "Pause_onclick"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Stop"Android:onclick= "Stop_onclick"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Exit"Android:onclick= "Exit_onclick"/>    </LinearLayout></LinearLayout>

Java

 Packagecom.example.chenshuai.myapplication;ImportAndroid.media.MediaPlayer;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classActivitymusicextendsappcompatactivity {TextView tv_1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_activity_music); Tv_1=(TextView) Findviewbyid (r.id.tv_1); Tv_1.settext ("Playback status: Stop playing ... "); }    //Media Player definition member variable    PrivateMediaPlayer MediaPlayer;  Public voidPlay_onclick (view view) {if(MediaPlayer = =NULL) {            //call the static method of MediaPlayer createMediaPlayer = Mediaplayer.create ( This, R.raw.tangren);        } mediaplayer.start (); Tv_1.settext ("Playback Status: Playing ... "); }     Public voidStop_onclick (view view) {if(MediaPlayer! =NULL) {mediaplayer.stop ();//StopMediaplayer.reset ();// ResetMediaplayer.release ();//Freeing ResourcesMediaPlayer =NULL;//re-assignment to null} tv_1.settext ("Playback status: Stop playing ... "); }     Public voidPause_onclick (view view) {if(MediaPlayer! =NULL&&mediaplayer.isplaying ())            {Mediaplayer.pause (); Tv_1.settext ("Playback status: Pause playback ... "); }    }     Public voidExit_onclick (view view) {Stop_onclick (view);    Finish (); }}

Effect:

Second, with service implementation

The same XML

Activitymusicservice.java
 Packagecom.example.chenshuai.myapplication;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classActivitymusicserviceextendsappcompatactivity {TextView tv_1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_activity_music); Tv_1=(TextView) Findviewbyid (r.id.tv_1); Tv_1.settext ("Playback status 11: Stop playback ... "); }     Public voidPlay_onclick (view view) {Intent Intent=NewIntent ( This, Myservicemusic.class); Intent.putextra ("Action", "play");        StartService (Intent); Tv_1.settext ("Playback status 11: Playing ... "); }     Public voidStop_onclick (view view) {Intent Intent=NewIntent ( This, Myservicemusic.class); Intent.putextra ("Action", "Stop");        StartService (Intent); Tv_1.settext ("Playback status 11: Stop playback ... "); }     Public voidPause_onclick (view view) {Intent Intent=NewIntent ( This, Myservicemusic.class); Intent.putextra ("Action", "pause");        StartService (Intent); Tv_1.settext ("Play status 11: Pause playback ... "); }     Public voidExit_onclick (view view) {Stop_onclick (view);    Finish (); }}
Myservicemusic.java
 Packagecom.example.chenshuai.myapplication;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaPlayer;ImportAndroid.os.IBinder; Public classMyservicemusicextendsService { PublicMyservicemusic () {} @Override Publicibinder onbind (Intent Intent) {//Todo:return The communication channel to the service.        Throw NewUnsupportedoperationexception ("not yet implemented"); }    PrivateMediaPlayer MediaPlayer; @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        //get information about intent to passString action = Intent.getstringextra ("Action"); Switch(action) { Case"Play":                if(MediaPlayer = =NULL) {MediaPlayer= Mediaplayer.create ( This, R.raw.onceagain);                } mediaplayer.start ();  Break;  Case"Stop":                if(MediaPlayer! =NULL) {mediaplayer.stop ();                    Mediaplayer.reset ();                    Mediaplayer.release (); MediaPlayer=NULL; }                 Break;  Case"Pause":                if(MediaPlayer! =NULL&&mediaplayer.isplaying ())                {Mediaplayer.pause (); }                 Break; }        return Super. Onstartcommand (Intent, flags, Startid); }}

Effect:

android--with activity and service for a simple music player

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.