我正在创建一个包含按钮的Android应用程序,点击每个按钮播放一个mp3文件。
问题是:
代码:
package com.example.puddly;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
public class GameActivity extends Activity implements OnClickListener {
Button btnDonkey,btnHorse,btnDog,btnFrog,btnChicken,btnCow,btnPig,btnDuck,btnCat;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
btnCat=(Button)findViewById(R.id.btnCat);
btnDonkey=(Button) findViewById(R.id.btnMonkey);
btnHorse=(Button) findViewById(R.id.btnElephent);
btnDog=(Button) findViewById(R.id.btnDog);
btnFrog=(Button) findViewById(R.id.btnFrog);
btnChicken=(Button) findViewById(R.id.btnChicken);
btnCow=(Button) findViewById(R.id.btnCow);
btnPig=(Button) findViewById(R.id.btnPig);
btnDuck=(Button)findViewById(R.id.btnLion);
btnHorse.setOnClickListener(this);
btnDog.setOnClickListener(this);
btnFrog.setOnClickListener(this);
btnChicken.setOnClickListener(this);
btnCow.setOnClickListener(this);
btnPig.setOnClickListener(this);
btnDuck.setOnClickListener(this);
btnDonkey.setOnClickListener(this);
btnCat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCat:
ObjectAnimator animY = ObjectAnimator.ofFloat(btnCat, "translationY", -100f, 0f);
animY.setDuration(1000);//1sec
animY.setInterpolator(new BounceInterpolator());
animY.setRepeatCount(0);
animY.start();
mp=MediaPlayer.create(this,R.raw.catname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnMonkey:
Animation animRotate = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
v.startAnimation(animRotate);
mp=MediaPlayer.create(this,R.raw.monkeyname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnElephent:
Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
v.startAnimation(animTranslate);
mp=MediaPlayer.create(this,R.raw.elephantname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnDog:
Animation animAlpha = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
v.startAnimation(animAlpha);
mp=MediaPlayer.create(this,R.raw.dogname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnFrog:
Animation animScale = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
v.startAnimation(animScale);
mp=MediaPlayer.create(this,R.raw.frogname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnCow:
Animation animScale2 = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
v.startAnimation(animScale2);
mp=MediaPlayer.create(this,R.raw.cowname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnChicken:
Animation animScale3 = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
v.startAnimation(animScale3);
mp=MediaPlayer.create(this,R.raw.chickenname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnPig:
Animation animScale4 = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
v.startAnimation(animScale4);
mp=MediaPlayer.create(this,R.raw.pigname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
case R.id.btnLion:
Animation animScale5 = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
v.startAnimation(animScale5);
mp=MediaPlayer.create(this,R.raw.lionname);
mp.start();
mp.setVolume(0.5F, 0.5F);
break;
}
}
}
答案 0 :(得分:0)
在我看来,你的设计方法很糟糕。每次要播放新曲目时,都不应创建新的MediaPlayer实例。相反,使用new运算符在onCreate()
中创建一个MediaPlayer。然后,当用户点击按钮时,首先reset()
mp,然后是setDataSource()
,然后是prepare()
,最后是start()
。应该工作
case R.id.btnMonkey:
Animation animRotate = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
v.startAnimation(animRotate);
mp.reset();
mp.setDataSource("Your mp3 file source here");
mp.prepare();
mp.start();
mp.setVolume(0.5F, 0.5F);
在onCreate()
初始化mp变量mp = new MediaPlayer()
答案 1 :(得分:0)