Android MP3 player

Source: Internet
Author: User

Layout file

Three buttons are set up in the layout file to play, pause, and resume playback

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start Playback"Android:onclick= "Play"/><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Pause playback"Android:onclick= "Pause"/><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Continue playing"Android:onclick= "Continueplay"/></LinearLayout>
Activity

Start the service in activity, implement the MP3 playback function in the service, and then expose the function to activity through a binder object. You can invoke the methods in the service in the activity.

 PackageXidian.dy.com.chujia;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {IMusic music; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Intent Intent=NewIntent ( This, Musicservice.class);        StartService (Intent); Bindservice (Intent,Newmyconnection (), bind_auto_create); }     Public voidPlay (View v) {music.play (); }     Public voidPause (View v) {music.pause (); }     Public voidContinueplay (View v) {music.continueplay (); }    classMyConnectionImplementsserviceconnection{@Override Public voidonserviceconnected (componentname name, IBinder service) {music=(IMusic) service; } @Override Public voidonservicedisconnected (componentname name) {} }}
Service

Create a MediaPlayer in the service to control music playback

 PackageXidian.dy.com.chujia;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaPlayer;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;Importandroid.support.annotation.Nullable;Importjava.io.IOException;/*** Created by Dy on 2016/7/29.*/ Public classMusicserviceextendsService {MediaPlayer player; @Override Public voidOnCreate () {player=NewMediaPlayer (); Super. OnCreate (); } @Nullable @Override Publicibinder onbind (Intent Intent) {return NewMybinder (); }     Public voidContinueplay () {Player.start (); }     Public voidpause () {player.pause (); }     Public voidPlay () {player.reset (); Try{Player.setdatasource ("/sdcard/bc.mp3");            Player.prepare ();        Player.start (); } Catch(IOException e) {e.printstacktrace (); }} @Override Public voidOnDestroy () {Super. OnDestroy ();        Player.stop ();    Player.release (); }    classMybinderextendsBinderImplementsimusic{@Override Public voidPlay () {Musicservice. This. Play (); } @Override Public voidpause () {Musicservice. This. Pause (); } @Override Public voidContinueplay () {musicservice. This. Continueplay (); }    }}
IMusic

Imusic is an interface class, primarily to enable the invocation of methods in the service in the activity.

 Package Xidian.dy.com.chujia; /**  */Publicinterface  IMusic    {void  play ()    ; void pause ();     void continueplay ();}
Manifest file

The service needs to be registered in the manifest file

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Xidian.dy.com.chujia"><ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">    <ActivityAndroid:name=". Mainactivity "Android:label= "Main interface">        <Intent-filter>            <ActionAndroid:name= "Android.intent.action.MAIN" />            <categoryAndroid:name= "Android.intent.category.LAUNCHER" />        </Intent-filter>    </Activity>    <ServiceAndroid:name=". Musicservice "/></Application></Manifest>

Android MP3 player

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.