好吧,所以发生的事情是我制作了一个Android应用程序,当我编译这个代码时,它构建得很好并且运行良好,但是当你发出垃圾声或按下足够的声音时,按钮会停止播放声音。有时所有按钮或只是几个。无论哪种方式,我真的需要修复它,并且可能有一些我不知道的愚蠢。所以请帮助,谢谢。我不得不在底部删除一些代码,所以如果它看起来像缺少某些东西,那是因为我不得不将其删除。此外,所有的声音都有效。当你发垃圾邮件或按下很多按钮时,它只会出现故障。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer potatomp = MediaPlayer.create(this, R.raw.apotatoflewaroundmyroom);
Button potato = (Button)this.findViewById(R.id.potato);
potato.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(potatomp.isPlaying()) {
potatomp.seekTo(0);
}
else {
potatomp.start();
}
}
});
final MediaPlayer Timemp = MediaPlayer.create(this, R.raw.aintnobodygottimefordat);
Button Time = (Button)this.findViewById(R.id.Time);
Time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Timemp.isPlaying()) {
Timemp.seekTo(0);
}
else {
Timemp.start();
}
}
});
final MediaPlayer Ballsmp = MediaPlayer.create(this, R.raw.ballsareperfect);
else {
getthecameramp.start();
}
}
});
final MediaPlayer graphmp = MediaPlayer.create(this, R.raw.graph);
Button graph = (Button)this.findViewById(R.id.graph);
graph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(graphmp.isPlaying()) {
graphmp.seekTo(0);
}
else {
graphmp.start();
}
}
});
final MediaPlayer hagaymp = MediaPlayer.create(this, R.raw.hagay);
Button hagay = (Button)this.findViewById(R.id.hagay);
hagay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(hagaymp.isPlaying()) {
hagaymp.seekTo(0);
}
else {
hagaymp.start();
}
}
});
final MediaPlayer hatersouttheremp = MediaPlayer.create(this, R.raw.hatersoutthere);
Button hatersoutthere = (Button)this.findViewById(R.id.hatersoutthere);
hatersoutthere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(hatersouttheremp.isPlaying()) {
hatersouttheremp.seekTo(0);
}
else {
hatersouttheremp.start();
}
}
});
final MediaPlayer im11mp = MediaPlayer.create(this, R.raw.im11);
Button im11 = (Button)this.findViewById(R.id.im11);
im11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(im11mp.isPlaying()) {
im11mp.seekTo(0);
}
else {
im11mp.start();
}
}
});
final MediaPlayer imaginationmp = MediaPlayer.create(this, R.raw.imagination);
Button imagination = (Button)this.findViewById(R.id.imagination);
imagination.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(imaginationmp.isPlaying()) {
imaginationmp.seekTo(0);
}
else {
imaginationmp.start();
}
}
});
final MediaPlayer iridocyclitismp = MediaPlayer.create(this, R.raw.iridocyclitis);
Button iridocyclitis = (Button)this.findViewById(R.id.iridocyclitis);
iridocyclitis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(iridocyclitismp.isPlaying()) {
iridocyclitismp.seekTo(0);
}
else {
iridocyclitismp.start();
}
}
});
final MediaPlayer isyoublindmp = MediaPlayer.create(this, R.raw.isyoublind);
Button isyoublind = (Button)this.findViewById(R.id.isyoublind);
isyoublind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isyoublindmp.isPlaying()) {
isyoublindmp.seekTo(0);
}
else {
isyoublindmp.start();
}
}
});
final MediaPlayer patriciamp = MediaPlayer.create(this, R.raw.patricia);
final Button patricia = (Button)this.findViewById(R.id.patricia);
patricia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(patriciamp.isPlaying()) {
patriciamp.seekTo(0);
}
else {
patriciamp.start();
}
}
});
final MediaPlayer raviolimp = MediaPlayer.create(this, R.raw.ravioli);
Button ravioli = (Button)this.findViewById(R.id.ravioli);
ravioli.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(raviolimp.isPlaying()) {
raviolimp.seekTo(0);
}
else {
raviolimp.start();
}
}
});
final MediaPlayer justdoitmp = MediaPlayer.create(this, R.raw.justdoit);
Button justdoit = (Button)this.findViewById(R.id.justdoit);
justdoit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(justdoitmp.isPlaying()) {
justdoitmp.seekTo(0);
}
else {
justdoitmp.start();
}
}
});
final MediaPlayer lebronjamesmp = MediaPlayer.create(this, R.raw.lebronjames);
Button lebronjames = (Button)this.findViewById(R.id.lebronjames);
lebronjames.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(lebronjamesmp.isPlaying()) {
lebronjamesmp.seekTo(0);
}
else {
lebronjamesmp.start();
}
}
});
final MediaPlayer michaelscottmp = MediaPlayer.create(this, R.raw.michaelscott);
Button michaelscott = (Button)this.findViewById(R.id.michaelscott);
michaelscott.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(michaelscottmp.isPlaying()) {
michaelscottmp.seekTo(0);
}
else {
michaelscottmp.start();
}
}
});
final MediaPlayer mlgairhornmp = MediaPlayer.create(this, R.raw.mlgairhorn);
Button mlgairhorn = (Button)this.findViewById(R.id.mlgairhorn);
mlgairhorn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mlgairhornmp.isPlaying()) {
mlgairhornmp.seekTo(0);
}
else {
mlgairhornmp.start();
}
}
});
final MediaPlayer mynameisjeffmp = MediaPlayer.create(this, R.raw.mynameisjeff);
Button mynameisjeff = (Button)this.findViewById(R.id.mynameisjeff);
mynameisjeff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mynameisjeffmp.isPlaying()) {
mynameisjeffmp.seekTo(0);
}
else {
mynameisjeffmp.start();
}
}
});
final MediaPlayer noimnotmp = MediaPlayer.create(this, R.raw.noimnot);
final Button noimnot = (Button)this.findViewById(R.id.noimnot);
noimnot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(noimnotmp.isPlaying()) {
noimnotmp.seekTo(0);
}
else {
noimnotmp.start();
}
}
});
final MediaPlayer nonofmybusinessmp = MediaPlayer.create(this, R.raw.nonofmybusiness);
Button nonofmybusiness = (Button)this.findViewById(R.id.nonofmybusiness);
nonofmybusiness.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(nonofmybusinessmp.isPlaying()) {
nonofmybusinessmp.seekTo(0);
}
else {
nonofmybusinessmp.start();
}
}
});
final MediaPlayer nothingisimpossiblemp = MediaPlayer.create(this, R.raw.nothingisimpossible);
Button nothingisimpossible = (Button)this.findViewById(R.id.nothingisimpossible);
nothingisimpossible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(nothingisimpossiblemp.isPlaying()) {
nothingisimpossiblemp.seekTo(0);
}
else {
nothingisimpossiblemp.start();
}
}
});
final MediaPlayer onequestionforyoump = MediaPlayer.create(this, R.raw.onequestionforyou);
Button onequestionforyou = (Button)this.findViewById(R.id.onequestionforyou);
onequestionforyou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onequestionforyoump.isPlaying()) {
onequestionforyoump.seekTo(0);
}
else {
onequestionforyoump.start();
}
}
});
final MediaPlayer reallymp = MediaPlayer.create(this, R.raw.really);
Button really = (Button)this.findViewById(R.id.really);
really.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(reallymp.isPlaying()) {
reallymp.seekTo(0);
}
else {
reallymp.start();
}
}
});
final MediaPlayer retardalertmp = MediaPlayer.create(this, R.raw.retardalert);
Button retardalert = (Button)this.findViewById(R.id.retardalert);
retardalert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(retardalertmp.isPlaying()) {
retardalertmp.seekTo(0);
}
else {
retardalertmp.start();
}
}
});
final MediaPlayer shalissamp = MediaPlayer.create(this, R.raw.shalissa);
final Button shalissa = (Button)this.findViewById(R.id.shalissa);
shalissa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(shalissamp.isPlaying()) {
shalissamp.seekTo(0);
}
else {
shalissamp.start();
}
}
});
final MediaPlayer sharknomp = MediaPlayer.create(this, R.raw.sharkno);
final Button sharkno = (Button)this.findViewById(R.id.sharkno);
sharkno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(sharknomp.isPlaying()) {
sharknomp.seekTo(0);
}
else {
sharknomp.start();
}
}
});
final MediaPlayer surprisemp = MediaPlayer.create(this, R.raw.surprise);
final Button surprise = (Button)this.findViewById(R.id.surprise);
surprise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(surprisemp.isPlaying()) {
surprisemp.seekTo(0);
}
else {
surprisemp.start();
}
}
});
final MediaPlayer thiskidisoncrackmp = MediaPlayer.create(this, R.raw.thiskidisoncrack);
final Button thiskidisoncrack = (Button)this.findViewById(R.id.thiskidisoncrack);
thiskidisoncrack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(thiskidisoncrackmp.isPlaying()) {
thiskidisoncrackmp.seekTo(0);
}
else {
thiskidisoncrackmp.start();
}
}
});
final MediaPlayer thomasthedankenginemp = MediaPlayer.create(this, R.raw.thomasthedankengine);
final Button thomasthedankengine = (Button)this.findViewById(R.id.thomasthedankengine);
thomasthedankengine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(thomasthedankenginemp.isPlaying()) {
thomasthedankenginemp.seekTo(0);
}
else {
thomasthedankenginemp.start();
}
}
});
final MediaPlayer rkoouttanowheremp = MediaPlayer.create(this, R.raw.rkoouttanowhere);
final Button rkoouttanowhere = (Button)this.findViewById(R.id.rkoouttanowhere);
rkoouttanowhere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(rkoouttanowheremp.isPlaying()) {
rkoouttanowheremp.seekTo(0);
}
else {
rkoouttanowheremp.start();
}
}
});
final MediaPlayer turtlesmp = MediaPlayer.create(this, R.raw.turtles);
final Button turtles = (Button)this.findViewById(R.id.turtles);
turtles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(turtlesmp.isPlaying()) {
turtlesmp.seekTo(0);
}
else {
turtlesmp.start();
}
}
});
final MediaPlayer twentyonemp = MediaPlayer.create(this, R.raw.twentyone);
final Button twentyone = (Button)this.findViewById(R.id.twentyone);
twentyone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(twentyonemp.isPlaying()) {
twentyonemp.seekTo(0);
}
else {
twentyonemp.start();
}
}
});
final MediaPlayer whatarethosemp = MediaPlayer.create(this, R.raw.whatarethose);
final Button whatarethose = (Button)this.findViewById(R.id.whatarethose);
whatarethose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(whatarethosemp.isPlaying()) {
whatarethosemp.seekTo(0);
}
else {
whatarethosemp.start();
}
}
});
final MediaPlayer whatareyouwaitingformp = MediaPlayer.create(this, R.raw.whatareyouwaitingfor);
final Button whatareyouwaitingfor = (Button)this.findViewById(R.id.whatareyouwaitingfor);
whatareyouwaitingfor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(whatareyouwaitingformp.isPlaying()) {
whatareyouwaitingformp.seekTo(0);
}
else {
whatareyouwaitingformp.start();
}
}
});
final MediaPlayer whatsnineplustenmp = MediaPlayer.create(this, R.raw.whatsnineplusten);
final Button whatsnineplusten = (Button)this.findViewById(R.id.whatsnineplusten);
whatsnineplusten.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (whatsnineplustenmp.isPlaying()) {
whatsnineplustenmp.seekTo(0);
} else {
whatsnineplustenmp.start();
}
}
});
final MediaPlayer youstupidmp = MediaPlayer.create(this, R.raw.youstupid);
final Button youstupid = (Button)this.findViewById(R.id.youstupid);
youstupid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (youstupidmp.isPlaying()) {
youstupidmp.seekTo(0);
} else {
youstupidmp.start();
}
}
});
final MediaPlayer whywouldyousaythatmp = MediaPlayer.create(this, R.raw.whywouldyousaythat);
final Button whywouldyousaythat = (Button)this.findViewById(R.id.whywouldyousaythat);
whywouldyousaythat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (whywouldyousaythatmp.isPlaying()) {
whywouldyousaythatmp.seekTo(0);
} else {
whywouldyousaythatmp.start();
}
}
});
final MediaPlayer yourenotmydadmp = MediaPlayer.create(this, R.raw.yourenotmydad);
final Button yourenotmydad = (Button)this.findViewById(R.id.yourenotmydad);
yourenotmydad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yourenotmydadmp.isPlaying()) {
yourenotmydadmp.seekTo(0);
} else {
yourenotmydadmp.start();
}
}
});
final MediaPlayer whyyoumadsinglemp = MediaPlayer.create(this, R.raw.whyyoumadsingle);
final Button whyyoumadsingle = (Button)this.findViewById(R.id.whyyoumadsingle);
whyyoumadsingle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (whyyoumadsinglemp.isPlaying()) {
whyyoumadsinglemp.seekTo(0);
} else {
whyyoumadsinglemp.start();
}
}
});
final MediaPlayer wheredyoufindthismp = MediaPlayer.create(this, R.raw.wheredyoufindthis);
final Button wheredyoufindthis = (Button)this.findViewById(R.id.wheredyoufindthis);
wheredyoufindthis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (wheredyoufindthismp.isPlaying()) {
wheredyoufindthismp.seekTo(0);
} else {
wheredyoufindthismp.start();
}
}
});
final MediaPlayer wheresthepizzamp = MediaPlayer.create(this, R.raw.wheresthepizza);
final Button wheresthepizza = (Button)this.findViewById(R.id.wheresthepizza);
wheresthepizza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (wheresthepizzamp.isPlaying()) {
wheresthepizzamp.seekTo(0);
} else {
wheresthepizzamp.start();
}
}
});
final MediaPlayer itsjohncenamp = MediaPlayer.create(this, R.raw.itsjohncena);
final Button itsjohncena = (Button)this.findViewById(R.id.itsjohncena);
itsjohncena.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (itsjohncenamp.isPlaying()) {
itsjohncenamp.seekTo(0);
} else {
itsjohncenamp.start();
}
}
});
final MediaPlayer nosleepmp = MediaPlayer.create(this, R.raw.nosleep);
final Button nosleep = (Button)this.findViewById(R.id.nosleep);
nosleep.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (nosleepmp.isPlaying()) {
nosleepmp.seekTo(0);
} else {
nosleepmp.start();
}
}
});
Button stop = (Button)this.findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view) {
}
if(hatersouttheremp.isPlaying()){
hatersouttheremp.pause();
hatersouttheremp.seekTo(0);
}
if(im11mp.isPlaying()){
im11mp.pause();
im11mp.seekTo(0);
}
if(imaginationmp.isPlaying()){
imaginationmp.pause();
imaginationmp.seekTo(0);
}
if(iridocyclitismp.isPlaying()){
iridocyclitismp.pause();
iridocyclitismp.seekTo(0);
}
if(isyoublindmp.isPlaying()){
isyoublindmp.pause();
isyoublindmp.seekTo(0);
}
if(patriciamp.isPlaying()){
patriciamp.pause();
patriciamp.seekTo(0);
}
if(raviolimp.isPlaying()){
raviolimp.pause();
raviolimp.seekTo(0);
}
if(justdoitmp.isPlaying()){
justdoitmp.pause();
justdoitmp.seekTo(0);
}
if(lebronjamesmp.isPlaying()){
lebronjamesmp.pause();
lebronjamesmp.seekTo(0);
}
if(rkoouttanowheremp.isPlaying()){
rkoouttanowheremp.pause();
rkoouttanowheremp.seekTo(0);
}
if(michaelscottmp.isPlaying()){
michaelscottmp.pause();
michaelscottmp.seekTo(0);
}
if(mlgairhornmp.isPlaying()){
mlgairhornmp.pause();
mlgairhornmp.seekTo(0);
}
if(mynameisjeffmp.isPlaying()){
mynameisjeffmp.pause();
mynameisjeffmp.seekTo(0);
}
if(noimnotmp.isPlaying()){
noimnotmp.pause();
noimnotmp.seekTo(0);
}
if(nonofmybusinessmp.isPlaying()){
nonofmybusinessmp.pause();
nonofmybusinessmp.seekTo(0);
}
if(nothingisimpossiblemp.isPlaying()){
nothingisimpossiblemp.pause();
nothingisimpossiblemp.seekTo(0);
}
if(onequestionforyoump.isPlaying()){
onequestionforyoump.pause();
onequestionforyoump.seekTo(0);
}
if(reallymp.isPlaying()){
reallymp.pause();
reallymp.seekTo(0);
}
if(retardalertmp.isPlaying()){
retardalertmp.pause();
retardalertmp.seekTo(0);
}
if(shalissamp.isPlaying()){
shalissamp.pause();
shalissamp.seekTo(0);
}
if(itsjohncenamp.isPlaying()){
itsjohncenamp.pause();
itsjohncenamp.seekTo(0);
}
if(sharknomp.isPlaying()){
sharknomp.pause();
sharknomp.seekTo(0);
}
if(surprisemp.isPlaying()){
surprisemp.pause();
surprisemp.seekTo(0);
}
if(whyyoumadsinglemp.isPlaying()){
whyyoumadsinglemp.pause();//wrong place
whyyoumadsinglemp.seekTo(0);
}
if(thiskidisoncrackmp.isPlaying()){
thiskidisoncrackmp.pause();
thiskidisoncrackmp.seekTo(0);
}
if(thomasthedankenginemp.isPlaying()){
thomasthedankenginemp.pause();
thomasthedankenginemp.seekTo(0);
}
if(turtlesmp.isPlaying()){
turtlesmp.pause();
turtlesmp.seekTo(0);
}
if(twentyonemp.isPlaying()){
twentyonemp.pause();
twentyonemp.seekTo(0);
}
if(whatarethosemp.isPlaying()){
whatarethosemp.pause();
whatarethosemp.seekTo(0);
}
if(whatareyouwaitingformp.isPlaying()){
whatareyouwaitingformp.pause();
whatareyouwaitingformp.seekTo(0);
}
if(whatsnineplustenmp.isPlaying()){
whatsnineplustenmp.pause();
whatsnineplustenmp.seekTo(0);
}
if(youstupidmp.isPlaying()){
youstupidmp.pause();
youstupidmp.seekTo(0);
}
if(whywouldyousaythatmp.isPlaying()){
whywouldyousaythatmp.pause();
whywouldyousaythatmp.seekTo(0);
}
if(yourenotmydadmp.isPlaying()){
yourenotmydadmp.pause();
yourenotmydadmp.seekTo(0);
}
if(wheredyoufindthismp.isPlaying()){
wheredyoufindthismp.pause();
wheredyoufindthismp.seekTo(0);
}
if(wheresthepizzamp.isPlaying()){
wheresthepizzamp.pause();
wheresthepizzamp.seekTo(0);
}
}
});
}}
答案 0 :(得分:0)
看看这篇文章: MediaPlayer stops playing the sounds - Android 也许您应该考虑回收单个MediaPlayer而不是使用多个实例。
如果您仍然遇到问题,另一个在同时播放多个声音文件时效果很好的选项是SoundPool http://developer.android.com/reference/android/media/SoundPool.html