我对此感到困惑,我引用了很多其他帖子。我并没有要求任何人完成我的代码,只是指出我出错的地方并引导我走向正确的方向。我想在单击图像按钮时播放音频文件,但在单击另一个图像按钮时停止播放。我遇到的问题是,如果我按下所有图像按钮,它们将同时播放音频。
package com.application.cats.catsshapecolorapp;
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class ColorPage extends AppCompatActivity {
Context context = this;
MediaPlayer mpPurple,mpBlue,mpRed,mpGreen,mpYellow,mpPink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_page);
mpPurple = MediaPlayer.create(context, R.raw.purpleaudiotest);
ImageButton purpleB = (ImageButton) findViewById(R.id.purpleButton);
purpleB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpPurple.isPlaying()) {
mpPurple.stop();
mpPurple.release();
mpPurple = MediaPlayer.create(context, R.raw.purpleaudiotest);
} mpPurple.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpBlue = MediaPlayer.create(context, R.raw.blueaudiotest);
ImageButton blueB = (ImageButton) findViewById(R.id.blueButton);
blueB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpBlue.isPlaying()) {
mpBlue.stop();
mpBlue.release();
mpBlue = MediaPlayer.create(context, R.raw.blueaudiotest);
} mpBlue.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpRed = MediaPlayer.create(context, R.raw.redaudiotest);
ImageButton redB = (ImageButton) findViewById(R.id.redButton);
redB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpRed.isPlaying()) {
mpRed.stop();
mpRed.release();
mpRed = MediaPlayer.create(context, R.raw.redaudiotest);
} mpRed.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpGreen = MediaPlayer.create(context, R.raw.greenaudiotest);
ImageButton greenB = (ImageButton) findViewById(R.id.greenButton);
greenB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpGreen.isPlaying()) {
mpGreen.stop();
mpGreen.release();
mpGreen = MediaPlayer.create(context, R.raw.greenaudiotest);
} mpGreen.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpYellow = MediaPlayer.create(context, R.raw.yellowaudiotest);
ImageButton yellowB = (ImageButton) findViewById(R.id.yellowButton);
yellowB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpYellow.isPlaying()) {
mpYellow.stop();
mpYellow.release();
mpYellow = MediaPlayer.create(context, R.raw.yellowaudiotest);
} mpYellow.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpPink = MediaPlayer.create(context, R.raw.pinkaudiotest);
ImageButton pinkB = (ImageButton) findViewById(R.id.pinkButton);
pinkB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mpPink.isPlaying()) {
mpPink.stop();
mpPink.release();
mpPink = MediaPlayer.create(context, R.raw.pinkaudiotest);
} mpPink.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
}
更新:现在的问题是,无论我点击什么图像按钮,pinkaudiotest始终是第一个播放的音频文件。我必须再次点击才能听到正确的音频文件。
答案 0 :(得分:0)
您每次点击都会创建一个新的MediaPlayer实例。只需使用一个。
if (media.isPlaying()) {
media.stop();
media.release();
media= MediaPlayer.create(context, R.raw.purpleaudiotest);
} media.start();
答案 1 :(得分:0)
试试这个,
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(R.raw.name);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();