AS实现音乐播放与暂停

1.首先下载喜欢的音乐,在res里的raw里导入

2.创建MusicActivity.java及相应activity_music的xml文件

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MusicActivity extends AppCompatActivity {
    Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_music);

        button1=findViewById(R.id.buttonm1);
        button2=findViewById(R.id.buttonm2);
        Intent intent=new Intent(MusicActivity.this,MyService.class);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startService(intent);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(intent);
            }
        });


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}

其中button1和button2监听xml文件里两个按钮,intent实现从MusicActivity到MyService的跳转

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MusicActivity">

    <TextView
        android:id="@+id/textViewMusic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Music"
        android:gravity="center"
        android:textSize="40sp" />

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

    <Button
        android:id="@+id/buttonm2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="STOPSERVICE" />
</LinearLayout>

3.MyService.java:

package com.example.myapplication;

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

public class MyService extends Service {
    MediaPlayer mediaPlayer;

    public MyService() {
        Log.d("zyt","MyService...");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("zyt","MyService onStartCommand...");
        mediaPlayer=MediaPlayer.create(this,R.raw.music2);
        mediaPlayer.start();
        return super.onStartCommand(intent, flags, startId);

    }

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

    @Override
    public void onDestroy() {
        Log.d("zyt","MyService onDestroy...");
        mediaPlayer.stop();
        super.onDestroy();
    }
}

其中R.raw.后接音乐文件

mediaPlayer.start();实现音乐播放

mediaPlayer.stop();实现音乐暂停

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值