Android uses service to play music instance code

Source: Internet
Author: User
Tags bind

Use of the Android service:

We can create an Android program that creates an activity in the SRC directory, a service class that inherits from the services class, and creates a raw folder in the Resource folder res directory to store audio files, such as putting Music.mp3 music files in the directory. The main interface of the program is as follows:

XML layout

The code is as follows Copy Code

<?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= "Welcome to Andy ' s blog!"
Android:textsize= "16sp"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Music playback service"/>
<button
Android:id= "@+id/startmusic"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Open music playback service"/>
<button
Android:id= "@+id/stopmusic"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Stop music playback service"/>
<button
Android:id= "@+id/bindmusic"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Bind music playback service"/>
<button
Android:id= "@+id/unbindmusic"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Unbind--Bind music playback service"/>
</LinearLayout>

Java code

The code is as follows Copy Code

Musicservice.java

Package com.zeph.android.service;

Import Android.app.Service;
Import android.content.Intent;
Import Android.media.MediaPlayer;
Import Android.os.IBinder;

public class Musicservice extends Service {
Private MediaPlayer Mmediaplayer;

@Override
Public IBinder Onbind (Intent arg0) {
return null;
}

@Override
public void OnCreate () {
Super.oncreate ();
Mmediaplayer = Mediaplayer.create (this, R.RAW.MUSIC01);
}

@Override
public void OnDestroy () {
Super.ondestroy ();
Mmediaplayer.stop ();
Mmediaplayer.release ();
}

  @Override
 public void OnStart (Intent Intent, int startid) {
  super.onstart (Intent, Startid);
  int operate = Intent.getintextra ("Operate", 3);
  switch (operate) {
  case 0:
   if (!mmediaplayer.isplaying ()) {
    mmediaplayer.start ();
   }
   break;
  case 1:
   if (mmediaplayer.isplaying ()) {
     Mmediaplayer.pause ();
   }
   break;
  case 2:
   if (mmediaplayer.isplaying ()) {
     Mmediaplayer.stop ();
    mmediaplayer = Mediaplayer.create (this, R.RAW.MUSIC01);
   }
   break;
  default:
   break
  
 }

Servicetestactivity.java

Package com.zeph.android.service;

Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Servicetestactivity extends activity {
Private Button PlayButton;
Private Button Pausebutton;
Private Button Stopbutton;
Private Button StopService;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
PlayButton = (Button) Findviewbyid (R.id.playbutton);
Pausebutton = (Button) Findviewbyid (R.id.pausebutton);
Stopbutton = (Button) Findviewbyid (R.id.stopbutton);
StopService = (Button) Findviewbyid (R.id.stopservice);

Playbutton.setonclicklistener (New Buttononclicklistener ());
Pausebutton.setonclicklistener (New Buttononclicklistener ());
Stopbutton.setonclicklistener (New Buttononclicklistener ());
Stopservice.setonclicklistener (New Buttononclicklistener ());
}

public class Buttononclicklistener implements Onclicklistener {

@Override
public void OnClick (view view) {
Intent Intent = new Intent ();
Intent.setclass (Getapplicationcontext (), musicservice.class);
if (view = = PlayButton) {
Intent.putextra ("operate", 0);
StartService (Intent);
else if (view = = Pausebutton) {
Intent.putextra ("Operate", 1);
StartService (Intent);
else if (view = = Stopbutton) {
Intent.putextra ("Operate", 2);
StartService (Intent);
else if (view = = StopService) {
StopService (Intent);
}
}
}
}

The service also needs to be registered with Androidmanifest.xml:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.pocketdigi.service"
Android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Main"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:enabled= "true" android:name=. Music "/>
</application>
</manifest>

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.