第一个应用程序循环环境音频。开始歌曲的四个按钮。当它运行时,它只播放两个按钮,其他两个不起作用。第五个按钮(按钮6)暂停所有音频。我只想播放一个声音,直到按下或暂停一个新的声音,并且当按下一个新的声音时,前一个停止,所以它不会同时播放两个声音(还有一个链接按钮,但我不知道是否有干扰)
谢谢
//main.java
public class MainActivity extends AppCompatActivity {
Button button6, buttonLink, buttonFire, buttonWind, buttonWater,buttonEarth;
boolean isPlaying = false;
MediaPlayer mediaPlayer, mediaPlayer1, mediaPlayer2, mediaPlayer3, mediaPlayer0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
button6 = findViewById(R.id.button6);
buttonLink = findViewById(R.id.buttonLink);
buttonFire = findViewById(R.id.buttonFire);
buttonWind = findViewById(R.id.buttonWind);
buttonWater= findViewById(R.id.buttonWater);
buttonEarth= findViewById(R.id.buttonEarth);
mediaPlayer = MediaPlayer.create(this, R.raw.android_earth);
mediaPlayer1 = MediaPlayer.create(this, R.raw.android_fire);
mediaPlayer2 = MediaPlayer.create(this, R.raw.android_water);
mediaPlayer3 = MediaPlayer.create(this, R.raw.android_wind);
mediaPlayer0 = MediaPlayer.create(this, R.raw.soundvoid);
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer.pause();
mediaPlayer2.pause();
mediaPlayer3.pause();
mediaPlayer1.pause();
}
}});
buttonFire.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer.pause();
mediaPlayer3.pause();
mediaPlayer2.pause();
mediaPlayer0.pause();
mediaPlayer1.start();
mediaPlayer1.setLooping(true);
}else if(!isPlaying){
mediaPlayer1.start();
mediaPlayer1.setLooping(true);
isPlaying = true;
}}});
buttonWind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer.pause();
mediaPlayer1.pause();
mediaPlayer2.pause();
mediaPlayer0.pause();
mediaPlayer3.start();
mediaPlayer3.setLooping(true);
}else if(!isPlaying){
mediaPlayer3.start();
mediaPlayer3.setLooping(true);
isPlaying = true;
}}});
buttonWater.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer.pause();
mediaPlayer1.pause();
mediaPlayer3.pause();
mediaPlayer0.pause();
mediaPlayer2.start();
mediaPlayer2.setLooping(true);
}else if(!isPlaying){
mediaPlayer2.start();
mediaPlayer2.setLooping(true);
isPlaying = true;
}}});
buttonEarth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer1.pause();
mediaPlayer2.pause();
mediaPlayer3.pause();
mediaPlayer0.pause();
mediaPlayer.start();
mediaPlayer.setLooping(true);
}else if(!isPlaying){
mediaPlayer.start();
mediaPlayer.setLooping(true);
isPlaying = true;
}}});
}
////////////////Boton link
public void open(View view){
Intent browserIntent = new Intent (Intent.ACTION_VIEW, Uri.parse("https://linktr.ee/MisCreaciones"));
startActivity(browserIntent);
}
}
//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"
android:background="@drawable/aplicacion_background"
tools:context=".MainActivity" >
<Button
android:id="@+id/button6"
android:layout_width="105dp"
android:layout_height="122dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonLink"
android:layout_width="158dp"
android:layout_height="62dp"
android:layout_marginStart="252dp"
android:layout_marginTop="668dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonFire"
android:layout_width="193dp"
android:layout_height="329dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonWind"
android:layout_width="192dp"
android:layout_height="327dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.96" />
<Button
android:id="@+id/buttonWater"
android:layout_width="196dp"
android:layout_height="324dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.995"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonEarth"
android:layout_width="197dp"
android:layout_height="281dp"
android:layout_marginStart="12dp"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.995"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.88" />
</androidx.constraintlayout.widget.ConstraintLayout>
暂无答案!
目前还没有任何答案,快来回答吧!