AIDL远程服务(音乐播放器后台运行)

xml代码

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hanqi.test7.MusicPlay1"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放状态:"
        android:id="@+id/tv_1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放"
            android:onClick="play_OnClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:onClick="pause_OnClick"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"
            android:onClick="stop_OnClick"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="退出"
            android:onClick="exit_OnClick"/>
    </LinearLayout>

</LinearLayout>

activity方法:

package com.hanqi.test7;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MusicPlay1 extends AppCompatActivity {

    TextView tv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play1);

        tv_1 = (TextView)findViewById(R.id.tv_1);

        tv_1.setText("播放状态:停止");

    }

    //媒体播放器
    private MediaPlayer mediaPlayer;

    public void play_OnClick(View v)
    {
        if(mediaPlayer == null) {
            //调用mediaPlayer的静态方法create()
            mediaPlayer = MediaPlayer.create(this, R.raw.dilemma);
        }

        mediaPlayer.start();
        mediaPlayer.isLooping();

        tv_1.setText("播放状态:正在播放...");
    }

    public void stop_OnClick(View v)
    {
        if (mediaPlayer != null){

            mediaPlayer.stop();//停止
            mediaPlayer.reset();//重置
            mediaPlayer.release();//释放
            mediaPlayer = null;//置空

        }
        tv_1.setText("播放状态:停止播放");

    }
    public void  pause_OnClick(View v)
    {
        if(mediaPlayer !=null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();
            tv_1.setText("播放状态:播放暂停");

        }

    }
    public void exit_OnClick(View v)
    {
        stop_OnClick(v);

        finish();
    }
}

实现视图效果:

Service方法:
package com.hanqi.test7;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MusicPlay2 extends AppCompatActivity {

    TextView tv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play1);

        tv_1 = (TextView)findViewById(R.id.tv_1);

        tv_1.setText("播放状态1:停止");

    }


    public void play_OnClick(View v)
    {
        Intent intent = new Intent(this,MusicPlayService.class);

        intent.putExtra("action","play");

        startService(intent);

        tv_1.setText("播放状态1:正在播放...");
    }

    public void stop_OnClick(View v)
    {

        Intent intent = new Intent(this,MusicPlayService.class);

        intent.putExtra("action","stop");

        startService(intent);

        tv_1.setText("播放状态1:停止");

    }
    public void  pause_OnClick(View v)
    {

        Intent intent = new Intent(this,MusicPlayService.class);

        intent.putExtra("action","pause");

        startService(intent);

        tv_1.setText("播放状态1:暂停");

    }
    public void exit_OnClick(View v)
    {
        stop_OnClick(v);

        finish();
    }
}

实现视图效果:

posted @ 2016-04-23 21:49  D(a/e)mon  阅读(472)  评论(0编辑  收藏  举报