Код 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:id="@+id/bottomRaz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/i"
tools:context=".MainActivity">
<Button
android:id="@+id/bottomQuest"
style="@style/Widget.MaterialComponents.TimePicker.Button"
android:layout_width="0dp"
android:layout_height="74dp"
android:layout_marginTop="156dp"
android:layout_marginBottom="8dp"
android:background="@drawable/i"
android:text="Квесты"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/black"
android:textSize="50dp"
android:textStyle="normal"
app:layout_constraintBottom_toTopOf="@+id/bottomSettings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bottomSettings"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="74dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:background="@drawable/i"
android:text="Настройки"
android:textColor="@color/black"
android:textSize="50dp"
app:iconTint="@color/black"
app:layout_constraintBottom_toTopOf="@+id/buttonCreators"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottomQuest"
app:layout_constraintVertical_bias="0.0"
app:rippleColor="@color/black"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/buttonCreators"
style="@style/Widget.MaterialComponents.TimePicker.Button"
android:layout_width="428dp"
android:layout_height="79dp"
android:layout_marginBottom="200dp"
android:background="@drawable/i"
android:text="Разработчики"
android:textColor="@color/black"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.529"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottomSettings"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/PlayButton"
style="@android:style/Widget.DeviceDefault.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="207dp"
android:onClick="play"
android:text="Play"
app:layout_constraintBottom_toBottomOf="@id/bottomQuest"
app:layout_constraintEnd_toStartOf="@+id/pauseButton"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/pauseButton"
style="@android:style/Widget.Material.Button.Borderless"
android:layout_width="105dp"
android:layout_height="48dp"
android:layout_marginStart="90dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="133dp"
android:onClick="pause"
android:text="Pause"
app:layout_constraintBottom_toTopOf="@id/bottomQuest"
app:layout_constraintEnd_toEndOf="@id/stopButton"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@id/PlayButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/stopButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginEnd="113dp"
android:layout_marginBottom="131dp"
android:onClick="stop"
android:text="Stop"
app:layout_constraintBottom_toTopOf="@+id/bottomQuest"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/pauseButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java код:
package com.stortor.tolyapp;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public Button bottomQuest;
public Button bottomSettings;
public Button buttonCreators;
public Button playButton;
public Button pauseButton;
public Button stopButton;
final int MAX_STREAMS = 100;
MediaPlayer mediaPlayer1;
int list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer1 = MediaPlayer.create(this, R.raw.dragon);
mediaPlayer1.setOnCompletionListener(mp -> stopPlay());
playButton = findViewById(R.id.PlayButton);
pauseButton = findViewById(R.id.pauseButton);
stopButton = findViewById(R.id.stopButton);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
}
public void stopPlay() {
mediaPlayer1.stop();
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
try {
mediaPlayer1.prepare();
mediaPlayer1.seekTo(0);
playButton.setEnabled(true);
} catch (Throwable t) {
Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void play(View
view) {
mediaPlayer1.start();
playButton.setEnabled(false);
pauseButton.setEnabled(true);
stopButton.setEnabled(true);
}
public void pause(View view) {
mediaPlayer1.pause();
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(true);
}
public void stop(View view) {
stopPlay();
}
public void onDestroy() {
super.onDestroy();
if (mediaPlayer1.isPlaying()) {
stopPlay();
}
}
{
SoundPool sp2 = new SoundPool(MAX_STREAMS, AudioManager.STREAM_ALARM, 1);
list = sp2.load(this, R.raw.listanie, 2);
bottomQuest = (Button) findViewById(R.id.bottomQuest);
bottomQuest.setOnClickListener(view -> {
sp2.play(1, 0, 1, 100, 0, 2);
Intent intent = new Intent(MainActivity.this, QuestActivity.class);
startActivity(intent);
});
bottomSettings = (Button) findViewById(R.id.bottomSettings);
bottomSettings.setOnClickListener(view -> {
sp2.play(1, 0, 1, 100, 0, 2);
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivity(intent);
});
buttonCreators = (Button) findViewById(R.id.buttonCreators);
buttonCreators.setOnClickListener(view -> {
sp2.play(1, 0, 1, 100, 0, 2);
Intent intent = new Intent(MainActivity.this, CreatorsActivity.class);
startActivity(intent);
});
}
}