MusicPlayer

发布时间 2023-06-18 03:53:45作者: 洛殊

1 布局文件 activity_main

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hellow"
        android:textSize="26sp"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/Stop"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/white"
            android:scaleType="centerInside"
            android:src="@drawable/stop"
            tools:ignore="SpeakableTextPresentCheck" />

        <ImageButton
            android:id="@+id/Start"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/white"
            android:scaleType="centerInside"
            android:src="@drawable/start"
            tools:ignore="SpeakableTextPresentCheck" />

        <ImageButton
            android:id="@+id/pause"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/white"
            android:scaleType="centerInside"
            android:src="@drawable/pause"
            tools:ignore="SpeakableTextPresentCheck" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/inner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/inner"
        android:textSize="26sp"/>
    <CheckBox
        android:id="@+id/sd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sd"
        android:textSize="26sp"/>
</LinearLayout>

2 控制文件 MainActivity

package com.example.musicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    CheckBox ch1,ch2;
    TextView txt;
    ImageButton stop,start,pause;
    MediaPlayer mediaPlayer;
    //sd卡中的文件
    String sdCard=new String("/sdcard/music/music2.mp3");
    //系统内资源文件
    int res_file =R.raw.music1;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer=new MediaPlayer();
        //查询系统内的资源
        ch1=(CheckBox) findViewById(R.id.inner1);
        ch2=findViewById(R.id.sd);
        txt=findViewById(R.id.txt);
        stop=findViewById(R.id.Stop);
        start=findViewById(R.id.Start);
        pause=findViewById(R.id.pause);

        setRegist();


        stop.setOnClickListener(new StopClick());
        start.setOnClickListener(new StartClick());
        pause.setOnClickListener(new PauseClick());
    }

    //动态获取sd卡权限
    private void setRegist() {
        if(Build.VERSION.SDK_INT>=23){
            int REQUEST_CODE_CONTACT=101;
            final int REQUEST_EXTERNAL_SRORAGE =1;
            String [] PERMISSION_STORAGE={Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
            //验证是否许可权限
            for(String str:PERMISSION_STORAGE){
                if(this.checkSelfPermission(str)!= PackageManager.PERMISSION_GRANTED){
                    this.requestPermissions(PERMISSION_STORAGE,REQUEST_CODE_CONTACT);
                    return;
                }
            }
        }
    }

    //停止按钮
    private class StopClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(mediaPlayer.isPlaying()){
                mediaPlayer.reset();
                mediaPlayer.release();
            }

        }
    }

    //开始按钮
    private class StartClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String str="";
            if(ch1.isChecked()){
                str=str+"\n"+ch1.getText();
                try{
                   mediaPlayer=MediaPlayer.create(MainActivity.this,res_file);
                   mediaPlayer.start();
                }catch (Exception e){
                    Log.i("ch1","res error");
                }
            }
            if(ch2.isChecked()){
                str=str+"\n"+ch2.getText();
                try{
                    mediaPlayer=new MediaPlayer();
                    mediaPlayer.setDataSource(sdCard);
                    mediaPlayer.prepare();
                    mediaPlayer.start();

                }catch (Exception e){
                    Log.i("ch2","sd error");
                }
            }
            txt.setText(str);
        }
    }

    //暂停
    private class PauseClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(mediaPlayer.isPlaying()){
                mediaPlayer.pause();
            }else mediaPlayer.start();
        }
    }

}

3 音乐资源

自选 在 res/raw下