1

在启动画面显示时,我无法播放声音。我在“res”目录下创建了“raw”目录,并将 droid.mp3 文件放在那里(大约 150Kb)。

这是负责启动画面的外观和声音的java文件的代码:

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();
    }

}

任何帮助深表感谢。

4

1 回答 1

3

而不是 Thread 尝试使用Handler.postDelayed作为:

 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延迟设置为:

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
   }
于 2013-03-09T19:26:51.900 回答