详细代码如下,我想实现10秒音乐播放,音乐停止后自动停止,然后到另一个活动,当前问题是在启动画面期间按下主页键时发生错误,另一个活动仍然可以进入(这是不合理的......)任何人都可以给我一些帮助吗?
公共类ActivitySplash扩展了Activity { 私有静态MediaPlayer playSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fontsCustom();
playSong = MediaPlayer.create(ActivitySplash.this,
R.raw.fsir200splashmusic);
playSong.start();
Thread timer = new Thread() {
public void run() {
try {
while (playSong.isPlaying()) {
sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent beginActivityInt = new Intent(
"android.intent.action.ACTIVITYMAIN");
startActivity(beginActivityInt);
}
}
};
timer.start();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
return true; // always eat it!
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
} else if (keyCode == KeyEvent.KEYCODE_HOME)
return true;
else
return super.onKeyDown(keyCode, event);
}
public void fontsCustom() {
FontCustom fonts = new FontCustom(ActivitySplash.this);
LinearLayout layout = (LinearLayout) findViewById(R.id.id_layout_splash);
fonts.setupLayoutTypefaces(layout);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
playSong.release();
finish();
}
答案 0 :(得分:0)
public class SplashActivity extends Activity {
Thread t;
MediaPlayer mpl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SplashActivity ss=this;
setContentView(R.layout.activity_splash);
mpl=MediaPlayer.create(this, R.raw.dog1);
mpl.start();
mpl=MediaPlayer.create(this, R.raw.cat3);
mpl.start();
Thread t=new Thread(){
public void run() {
try{
sleep(10000);
}
catch(Exception e){
}
mpl.stop();
Intent i=new Intent(ss,MainActivity.class);
startActivity(i);
}
};
t.start();
}
}
答案 1 :(得分:0)
你必须设置
playsong.OnCompletionListener(ActivitySplash.this)
和
在
public void onCompletion(MediaPlayer arg0)
{
// Write your code
}
请参阅以下代码:
public class SplashActivity extends Activity {
private static MediaPlayer playsong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
playsong = MediaPlayer.create(this, R.raw.pp);
playsong.start();
playsong.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
playsong.release();
finish();
}
}