Service实现音乐播放

来源:互联网 发布:仿链家房产网源码 编辑:程序博客网 时间:2024/05/23 15:47
package com.songjie.music_service.activity;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.View;import android.widget.Button;import com.songjie.music_service.R;/** * 界面处理 */public class Acty_Main extends ActionBarActivity implements View.OnClickListener {    private Button play;    private Button stop;    private Button exit;    private Button close;    private Button pause;    private Bundle bundle;    private static final String TAG = "PlayMusic";    private void init_View() {        play = (Button) findViewById(R.id.play);        stop = (Button) findViewById(R.id.stop);        pause = (Button) findViewById(R.id.pause);        close = (Button) findViewById(R.id.close);        exit = (Button)findViewById(R.id.exit);        bundle = new Bundle();        play.setOnClickListener(this);        stop.setOnClickListener(this);        pause.setOnClickListener(this);        close.setOnClickListener(this);        exit.setOnClickListener(this);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.acty_main);        init_View();    }    @Override    public void onClick(View v) {        int op = -1;        Intent intent = new Intent("org.allin.android.musicService");        switch (v.getId()) {            case R.id.play:                Log.i(TAG, "onClick:playing music");                op = 1;                break;            case R.id.stop:                Log.i(TAG, "onClick:stoping music");                op = 2;                break;            case R.id.pause:                Log.i(TAG, "onClick:pauseing music");                op = 3;                break;            case R.id.close:                Log.i(TAG, "onClick:close");                this.finish();                break;            case R.id.exit:                Log.i(TAG, "onClick:exit");                op = 4;                this.finish();                stopService(intent);                break;        }        bundle = new Bundle();        bundle.putInt("op", op);        intent.putExtras(bundle);        startService(intent);    }}
package com.songjie.music_service.service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import com.songjie.music_service.R;import java.io.IOException;/** * 服务的实现 */public class Serv_Music extends Service {    private Bundle bundle;    private MediaPlayer media_player;    private static final String TAG = "Serv_Music";    public Serv_Music() {    }    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        Log.v(TAG, "onCreate");        if (media_player == null) {            media_player = MediaPlayer.create(this, R.raw.tmp);            media_player.setLooping(false);        }        super.onCreate();    }    @Override    public void onDestroy() {        Log.v(TAG, "onDestroy");        if (media_player != null) {            media_player.stop();            media_player.release();        }    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.v(TAG, "onStartCommand");        if (intent != null) {            bundle = intent.getExtras();            if (bundle != null) {                int op = bundle.getInt("op");                switch (op) {                    case 1:                        play();                        break;                    case 2:                        stop();                        break;                    case 3:                        pause();                        break;                }            }        }        return super.onStartCommand(intent, flags, startId);    }    private void pause() {        if (media_player != null && media_player.isPlaying()) {            media_player.pause();        }    }    private void play() {        if (!media_player.isPlaying()) {            media_player.start();        }    }    public void stop() {        if (media_player != null) {            media_player.stop();            try {                media_player.prepare();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
<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"    tools:context=".Acty_Main">    <Button        android:id="@+id/play"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/play"/>    <Button        android:id="@+id/stop"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/stop"/>    <Button        android:id="@+id/pause"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/pause"/>    <Button        android:id="@+id/close"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/close"/>    <Button        android:id="@+id/exit"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/exit"/></LinearLayout>
0 0