I am trying to build a splash screen with music in the background, but the music doesn't play.
Here is my code:
package com.example.thebasicseries;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
MediaPlayer logoMusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno );
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(5000);
Intent menuIntent = new Intent("com.example.thebasicseries.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
logoMusic.release();
}
There isn't any error message showing before or after running the app.
LOL, you created the MP :
logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno )
;
But never play it :
logoMusic.play();
更好的是,从视频中为启动画面剪切特定部分,并像gif图像一样使用它,它将起作用
i have got it work!
package com.example.thebasicseries;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
MediaPlayer logoMusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno );
logoMusic.start();
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(5000);
Intent menuIntent = new Intent("com.example.thebasicseries.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
@Override //so i just create an onPause method and release my song
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release(); //while it is onPause i release the song
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.