Android使用Service播放音樂執行個體代碼

來源:互聯網
上載者:User

Android Service 的使用:

我們可以建立一個Android程式,在src目錄下建立一個Activity,一個繼承自Service類的服務類;同時在資源檔夾res目錄下建立一個raw的檔案夾存放音頻檔案,如把music.mp3音樂檔案放在該目錄下。該程式的主介面如下:

xml布局

 代碼如下 複製代碼

<?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="音樂播放服務"/>
    <Button
       android:id="@+id/startMusic"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="開啟音樂播放服務"/>
    <Button
       android:id="@+id/stopMusic"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="停止音樂播放服務"/>
   <Button
      android:id="@+id/bindMusic"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="綁定音樂播放服務"/>
   <Button
      android:id="@+id/unbindMusic"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="解除 ——綁定音樂播放服務"/>
</LinearLayout>

java代碼

 代碼如下 複製代碼

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);
   }
  }
 }
}

服務還需要在AndroidManifest.xml註冊後才能使用:

 代碼如下 複製代碼

<?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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.