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();实现音乐暂停