安卓Service和Broadcast实现简单的音乐播放器

做实验的时候使用Service和Broadcast实现了一个功能较为简单的音乐播放器,可以对音乐进行播放、暂停和停止。
主要思路:
1、使用Service在后台播放音乐
2、Broadcast发送广播通知Activity更改界面
程序运行界面:

图1 播放界面
图2 暂停界面
图3 停止界面
实现代码:
1、布局界面XML如下











    

    


2、主Acitivy如下

package com.example.test6;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class Music extends AppCompatActivity {
TextView musictext;
ImageView photo;
ImageButton startbutton;
Integer state = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);

    IntentFilter inf = new IntentFilter();
    inf.addAction("com.user.action");
    registerReceiver(broad,inf);

    musictext = findViewById(R.id.musictext);
    photo = findViewById(R.id.photo);
    startbutton = findViewById(R.id.start);
    ImageButton stopbutton = findViewById(R.id.stop);

    startbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (state){
                case 1:
                    state = 2;
                    break;
                default:
                    state = 1;
                    break;
            }
            Log.v("当前状态","1");
            Intent intent = new Intent(Music.this,MusicService.class);
            intent.putExtra("action",state);
            startService(intent);
            Log.v("intent传值","1");
        }
    });

    stopbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Music.this,MusicService.class);
            stopService(intent);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broad);
}

public BroadcastReceiver broad = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int i = intent.getIntExtra("action",-1);
        switch(i){
            case 1:
                musictext.setText("China-X");
                photo.setImageResource(R.drawable.yjtp);
                startbutton.setImageResource(R.drawable.pause);
                break;
            case 2:
                startbutton.setImageResource(R.drawable.start);
                musictext.setText("音乐暂停");
                break;
            case 3:
                state = 3;
                musictext.setText("暂无播放音乐");
                photo.setImageResource(R.drawable.qielogo);
                startbutton.setImageResource(R.drawable.start);
                break;
        }
    }
};

}

3、Service类如下

package com.example.test6;

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

public class MusicService extends Service {

public IBinder onBind(Intent intent){
    return null;
}

private MediaPlayer mp;

public void onCreate(){
    super.onCreate();

}

public void onStart(Intent intent,int startId){
    super.onStart(intent,startId);
    int i = intent.getIntExtra("action",0);
    if(i==1){
        if(null==mp) {
            mp = MediaPlayer.create(this, R.raw.mymusic);
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopSelf();
                }
            });
        }
        mp.start();
    }else if(i==2){
        if(mp!=null&&mp.isPlaying()){
            mp.pause();
        }
    }
    Log.v("zhuangtai",String.valueOf(i));
    Intent in = new Intent("com.user.action");
    in.putExtra("action",i);
    sendBroadcast(in);
}

public void onDestroy(){
    super.onDestroy();
    mp.stop();
    Intent in = new Intent("com.user.action");
    in.putExtra("action",3);
    sendBroadcast(in);
}

}

原文链接:https://blog.csdn.net/weixin_43868299/article/details/106599899

你可能感兴趣的:(安卓Service和Broadcast实现简单的音乐播放器)