Android声音未在初始屏幕中播放 [英] Android Sound not playing in splash screen
问题描述
显示初始屏幕时,我在播放声音时遇到麻烦.我已经在"res"目录下创建了"raw"目录,并将droid.mp3文件放在那里(大约150Kb).
Im having troubles with playing sound while the splash screen shows. Ive created the "raw" directory under "res" directory and put the droid.mp3 file there (around 150Kb).
这是负责启动屏幕的外观和声音的java文件的代码:
This is the code of the java file responsible for the appearance and sound of the splash screen:
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class SplashActivity extends Activity
{
public MediaPlayer splashSound;
protected void onCreate(Bundle splashBundle)
{
super.onCreate(splashBundle);
setContentView(R.layout.splash);
splashSound = MediaPlayer.create(SplashActivity.this, R.raw.droid);
Thread t1 = new Thread() {
public void run() {
try
{
sleep(5000);
}
catch (InterruptedException IE)
{
IE.printStackTrace();
}
finally
{
Intent mainActivityIntent=new Intent("com.example.stamapp.MAINACTIVITY");
startActivity(mainActivityIntent);
}
}
};
t1.start();
}
@Override
protected void onPause() {
super.onPause();
splashSound.release();
finish();
}
}
非常感谢您的帮助.
推荐答案
instead of Thread try is using Handler.postDelayed as:
Handler handler;
protected void onCreate(Bundle splashBundle)
{
super.onCreate(splashBundle);
setContentView(R.layout.splash);
handler = new Handler();
splashSound = MediaPlayer.create(SplashActivity.this,
R.raw.droid);
splashSound.start(); //<<<play sound on Splash Screen
handler.postDelayed(runnable, 5000);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
//start your Next Activity here
}
};
第二种方法是将 MediaPlayer.setOnCompletionListener 添加到MediaPlayer实例,该实例在媒体源播放完成时调用而没有将5000
Delay设置为:
and second way is add MediaPlayer.setOnCompletionListener to MediaPlayer instance which invoke when playback of a media source has completed without putting 5000
Delay as :
protected void onCreate(Bundle splashBundle)
{
super.onCreate(splashBundle);
setContentView(R.layout.splash);
splashSound = MediaPlayer.create(SplashActivity.this,
R.raw.droid);
splashSound.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer splashSound) {
splashSound.stop();
splashSound.release();
//start your Next Activity here
}
});
splashSound.start(); //<<<play sound on Splash Screen
}
这篇关于Android声音未在初始屏幕中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!