播放本地音乐
按钮的话用的插件是butterknife 所以没有findViewById;
插件使用的话会放在最后;
strings.xml
<string name="play">Play</string>
<string name="pause">Pause</string>
<string name="replay">Replay</string>
<string name="music">Music</string>
<string name="stop">Stop</string>
1.创建布局activity_main.xml:
布局就三个按钮:播放,暂停和重新播放
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/play"/>
<Button
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pause"/>
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stop"/>
</LinearLayout>
2.创建activity:
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer = new MediaPlayer();
@BindView(R.id.play)
Button play;
@BindView(R.id.pause)
Button pau