Android studio——播放音乐

一、整体布局 

二、 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service  android:enabled="true" android:name=".AudioService"/>
    </application>

</manifest>

二、Java

1.MainActivity.java

package com.example.myapplication;

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 {
    Button btn1,btn2;
     Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new  myClick());
        btn2.setOnClickListener(new  myClick());
        intent = new Intent(MainActivity.this, AudioService.class);
    }
    class myClick implements View.OnClickListener {
        public void onClick(View v){
            if (v==btn1)
            {
                MainActivity.this.startService(intent);
            }
            if (v==btn2)
            {
                MainActivity.this.stopService(intent);
            }
        }

    }

}

2.AudioService.java

package com.example.myapplication;

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

public class AudioService extends Service {
      MediaPlayer play;
    public IBinder onBind(Intent intent){return  null;}
public  void  onCreate()
{
    super.onCreate();
    play=MediaPlayer.create(this,R.raw.wojide);
    toastMessage("创建后台服务");
}


public  int onStartCommand(Intent intent,int flags,int startId)
{
    super.onStartCommand(intent,flags,startId);
    play.start();
    toastMessage("开始播放");
    return  START_STICKY;
}
public  void  onDestroy()
{
    play.release();
    super.onDestroy();
    toastMessage("停止播放");

}

    private void toastMessage(String message) {
        Toast toast=Toast.makeText(AudioService.this,message,Toast.LENGTH_LONG);
        toast.show();
    }
}

三、xml

activity_main.xml

<?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/button1"
        android:layout_width="160dp"
        android:layout_height="60dp"
        android:layout_marginStart="140dp"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="436dp"
        android:text="播放音乐服务器"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="160dp"
        android:layout_height="60dp"
        android:text="关闭音乐服务器"
        app:layout_constraintStart_toStartOf="@+id/button1"
        app:layout_constraintTop_toBottomOf="@+id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>

四、在res下新建一个名为raw,把MP3格式音乐放里面

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值