1. Experimental content
- Build the application interface layout
(1) Create a new Android project, add a button (button) in the default layout interface, as shown in the figure below:
2) Build the corresponding java code, add the response event of the button, and jump to another interface musicplayer.xml, the corresponding activity name is musicPlayer.java, add three imagebuttons in its interface, corresponding to the player's play, exit (background play), exit (stop playing) operations, as shown in the following figure:
- Build the Service component
(1) Create a new MusicService class to inherit the Service, and realize the control of the MediaPlayer by rewriting the onStartCommand() and onDestroy() methods. - Implement a simple music player
(1) Improve the code in musicPlayer.java, and start MusicService through the startService method. And realize the function of each button. Among them:
Play button: Click the button to start playing music (play the fixed music file for-love.mp3, please import the file into the asset or raw directory of the project in advance), click the button again to pause the playback.
Exit (Background Play): Click the button to exit the musicplayer interface (return to the initial interface), but the music is still playing in the background.
Exit (stop playing): Click the button to exit the musicplayer interface (return to the initial interface), and stop playing music at the same time.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginTop="23dp"
android:text="打开播放器"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".musicplayer">
<ImageButton
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_media_play" />
<ImageButton
android:id="@+id/btn_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/btn_play"
app:layout_constraintTop_toTopOf="@+id/btn_play"
app:srcCompat="@android:drawable/ic_menu_revert" />
<ImageButton
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintBottom_toBottomOf="@+id/btn_return"
app:layout_constraintStart_toEndOf="@+id/btn_return"
app:srcCompat="@android:drawable/ic_lock_power_off" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = findViewById(R.id.start);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this,musicplayer.class);
startActivity(intent);
}
});
}
}
package com.example.myapplication7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class musicplayer extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musicplayer);
final Intent intent =new Intent(musicplayer.this,MusicService.class);
ImageButton btn_play=(ImageButton) findViewById(R.id.btn_play);
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( MusicService.isplay==false)
{
startService(intent);
Toast.makeText(getApplicationContext(), "正在播放", Toast.LENGTH_LONG).show();
}
else
{
stopService(intent);
Toast.makeText(getApplicationContext(), "暂停播放", Toast.LENGTH_LONG).show();}
}});
ImageButton btn_return=(ImageButton) findViewById(R.id.btn_return);
btn_return.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( MusicService.isplay==true)
{
Intent intent2 = new Intent( musicplayer.this,MainActivity.class);
startActivity(intent2);
Toast.makeText(getApplicationContext(), "正在播放", Toast.LENGTH_LONG).show();
}
}});
ImageButton btn_stop=(ImageButton) findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( MusicService.isplay==true)
{
stopService(intent);
Intent intent1 = new Intent( musicplayer.this,MainActivity.class);
startActivity(intent1);
Toast.makeText(getApplicationContext(), "停止播放", Toast.LENGTH_LONG).show();
}
}});
}
}
package com.example.myapplication7;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
static boolean isplay;
MediaPlayer player;
public MusicService() {
}
@Override
public void onCreate() {
player = MediaPlayer.create(this,R.raw.for_love);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!player.isPlaying())
{
player.start();
isplay=player.isPlaying();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
player.stop();
isplay=player.isPlaying();
player.release();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
MusicService: By rewriting OnCreate(), onDestroy(), onStartCommand(), the music playback of the background service can be realized
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication7">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication7">
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".musicplayer" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>