SharedProperties.java
public void initPlayMusic(Context context, int count) {
playMusic = MediaPlayer.create(context, count);
playMusic.setVolume(100, 100);
playMusic.setLooping(false);
}
public void playMusic() {
playMusic.start();
}
public void stopMusic() {
if(playMusic!=null) {
playMusic.stop();
playMusic.release();
playMusic=null;
}
}
public void initPlayMusic1(Context context, int count) {
playMusic1 = MediaPlayer.create(context, count);
playMusic1.setVolume(100, 100);
playMusic1.setLooping(false);
}
public void playMusic1() {
playMusic1.start();
}
public void stopMusic1() {
if(playMusic1!=null) {
playMusic1.stop();
playMusic1.release();
playMusic1=null;
}
}
我的观点类
在此处将SharedProperties.java称为sp
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.backButton:
Intent videoIntent = new Intent(PlayPage1.this,MainActivity.class);
startActivity(videoIntent);
finish();
break;
case R.id.imageView2:
sp.stopMusic();
if(!test){
test = true;
sp.playMusic(); Toast.makeText(getApplicationContext(),"play Music ",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"play Music1 ",Toast.LENGTH_SHORT).show();
sp.playMusic1();
test = false;
}
sp.playSFXWrong();
sp.getSFXWrong().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
txtone.setEnabled(true);
txtthree.setEnabled(true);
}
});
answer=false;
break;
case R.id.imageView3:
t
sp.stopMusicx();
if(!test){
// Toast.makeText(getApplicationContext(),"play Music1 ",Toast.LENGTH_SHORT).show();
test = true;
sp.playMusic(); Toast.makeText(getApplicationContext(),"play Music ",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"play Music1 ",Toast.LENGTH_SHORT).show();
test = false;
sp.playMusic1();
}
sp.playSFXWrong();
sp.getSFXWrong().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
txtone.setEnabled(true);
txttwo.setEnabled(true);
}
});
answer=false;
break;
}
}
如果调用stopMusic()
,我可以进行此音频重叠预防;它提供NullPointerException
所以请找我的错误
否则pauseMusic()
也无法在这里工作。