用service实现音乐播放器

MainActivity.java

package com.example.musicplayservice;

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;

/*以service实现的简单的播放MP3功能 * 需要在res/raw中添加MP3文件 * 需要在andoridMainfeist中注册service以及aciton * */
public class MainActivity extends Activity {
    private Button mBtn_play;
    private Button mBtn_stop;
// 定义一个Action
    private String MusicAciton = "com.example.service.action.MUSIC_SERVICE";           

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn_play = (Button) findViewById(R.id.btn_play);
        mBtn_stop = (Button) findViewById(R.id.btn_stop);
        mBtn_play.setOnClickListener(new PlayBtnListener());
        mBtn_stop.setOnClickListener(new StopBtnListener());

    }

// 播放按钮
// 实现这个类时,首先执行的是service.create的方法,然后执行service.start方法,
    class PlayBtnListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent_play = new Intent(MusicAciton);
            intent_play.setClass(MainActivity.this, MusicPlayService.class);
// 启动服务
            startService(intent_play);

        }
    }

// 停止播放并退出按钮
// 实现这个类时,执行的是service.destroy方法
    class StopBtnListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent_stop = new Intent(MusicAciton);
            intent_stop.setClass(MainActivity.this, MusicPlayService.class);
// 停止服务
            stopService(intent_stop);
// 退出程序
            finish();
        }

    }

}

MusicPlayerService.java

package com.example.musicplayservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.view.View.OnCreateContextMenuListener;

public class MusicPlayService extends Service {
    private MediaPlayer MusicPlayer = null;
    private String TAG = "MusicPlay";

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onBind");
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i(TAG, "onCreate");
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        MusicPlayer.stop();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.i(TAG, "onStart");
// 创建一个Mediaplayer实例
        MusicPlayer = MediaPlayer.create(this, R.raw.sky);
// 播放
        MusicPlayer.start();
    }
}

activity_main.xml

<LinearLayout xmlns: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:orientation="vertical" >

    <Button android:id="@+id/btn_play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_play"/>

    <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_stop"/>



</LinearLayout>

androidMainfeist.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.musicplayservice" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" 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:name="com.example.musicplayservice.MusicPlayService">
            <intent-filter >
                <action android:name="com.example.service.Action.MUSIC_SERVICE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

        </service>
    </application>

</manifest>

你可能感兴趣的:(用service实现音乐播放器)