Android声音未在初始屏幕中播放 [英] Android Sound not playing in splash screen

查看:95
本文介绍了Android声音未在初始屏幕中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示初始屏幕时,我在播放声音时遇到麻烦.我已经在"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆