簡體   English   中英

Android Sound無法在啟動畫面中播放

[英]Android Sound not playing in splash screen

在啟動畫面顯示時,我有播放聲音的麻煩。 我在“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();
    }

}

任何幫助深表感謝。

而不是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
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM