使用Service播放音乐

 

 res/layout/main.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:layout_marginBottom="20dp"
	android:textColor="@android:color/white"
	android:textStyle="bold"
	android:text="使用Service播放音乐"
	android:gravity="center"/>
<Button 
	android:id="@+id/playButton"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Play Music"/>
<Button 
	android:id="@+id/pauseButton"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Pause Music"/>
<Button 
	android:id="@+id/stopButton"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Stop Music"/>
<Button 
	android:id="@+id/stopService"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Stop Service"/>
</LinearLayout>


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


 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值