Android——用Activity和Service实现简单的音乐播放器

一、只用Activity 容易出现问题

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.example.chenshuai.myapplication.ActivityMusic"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40sp"
        android:text="播放状态"
        android:textSize="20sp"
        android:gravity="center_horizontal"
        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>

java

package com.example.chenshuai.myapplication;

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

public class ActivityMusic extends AppCompatActivity {

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

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

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

    //媒体播放器 定义成员变量
    private MediaPlayer mediaPlayer;
    public void play_onclick(View view)
    {
        if (mediaPlayer == null) {
            //调用MediaPlayer的静态方法create
            mediaPlayer = MediaPlayer.create(this, R.raw.tangren);
        }

        mediaPlayer.start();

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

    public void stop_onclick(View view)
    {
        if (mediaPlayer != null) {
            mediaPlayer.stop();//停止
            mediaPlayer.reset();//重置
            mediaPlayer.release();//释放资源
            mediaPlayer = null;//重新赋值为空
        }
        tv_1.setText("播放状态:停止播放。。。");
    }
    public void pause_onclick(View view)
    {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();

            tv_1.setText("播放状态:暂停播放。。。");
        }
    }
    public void exit_onclick(View view)
    {
        stop_onclick(view);
        finish();
    }
}

效果:

Android——用Activity和Service实现简单的音乐播放器_第1张图片

二、用Service实现

同一个xml

ActivityMusicservice.java
package com.example.chenshuai.myapplication;

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

public class ActivityMusicservice extends AppCompatActivity {

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

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

        tv_1.setText("播放状态11:停止播放。。。");
    }
    public void play_onclick(View view)
    {
        Intent intent = new Intent(this,MyServiceMusic.class);

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

        startService(intent);

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

    public void stop_onclick(View view)
    {
        Intent intent = new Intent(this,MyServiceMusic.class);

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

        startService(intent);

        tv_1.setText("播放状态11:停止播放。。。");
    }
    public void pause_onclick(View view)
    {
        Intent intent = new Intent(this,MyServiceMusic.class);

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

        startService(intent);

        tv_1.setText("播放状态11:暂停播放。。。");
    }
    public void exit_onclick(View view)
    {
        stop_onclick(view);
        finish();
    }
}
MyServiceMusic.java
package com.example.chenshuai.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MyServiceMusic extends Service {
    public MyServiceMusic() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    private MediaPlayer mediaPlayer;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //获取意图传递的信息
        String action = intent.getStringExtra("action");

        switch (action)
        {
            case "play":
                if (mediaPlayer == null)
                {
                    mediaPlayer = MediaPlayer.create(this,R.raw.onceagain);
                }
                mediaPlayer.start();

                break;
            case "stop":
                if (mediaPlayer !=null)
                {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                break;
            case "pause":
                if (mediaPlayer !=null && mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();
                }
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

minifest.xml

<service
            android:name=".MyServiceMusic"
            android:enabled="true"
            android:exported="true" />

 

效果:

Android——用Activity和Service实现简单的音乐播放器_第2张图片

 

你可能感兴趣的:(Android——用Activity和Service实现简单的音乐播放器)