如何在Android的启动画面中播放音频 [英] How to play audio in splash screen in android

查看:55
本文介绍了如何在Android的启动画面中播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在启动画面期间播放音频.需要指导.

How to play an audio during splash screen. Guidance needed.

推荐答案

我的方法(不需要外部声音,因为我将声音文件放在资源文件夹中):

My way to do this (no external sound needed, since I put my soundfile in my resources-folder):

在onCreate中:

In onCreate:

mp = MediaPlayer.create(getBaseContext(), R.raw.sound); /*Gets your 
soundfile from res/raw/sound.ogg */
mp.start(); //Starts your sound

//Continue with your run/thread-code here

请记住以.ogg格式播放声音;它在Android中完全受支持.

Remember to have the sound in .ogg-format; it's fully supported in Android.

以下有关启动屏幕停止活动时处理声音的重要事项:

有两种一般方法可以在启动屏幕停止时管理启动屏幕(及其内部的声音):

There are two general ways to manage the Splash Screen (and the sound inside it) when it's stopped:

  1. 破坏整个活动:

  1. Destroy the whole activity:

protected void onStop() {
  super.onStop();

  ur.removeCallbacks(myRunnable); /*If the application is stopped;
remove the callback, so the next time the 
application starts it shows the Splash Screen again, and also, so the
thread-code,
don't continue after the application has stopped */

  finish();
  onDestroy();
}

  • 或者您也可以在onStop中停止声音:

  • Or you can just stop the sound in onStop:

     protected void onStop() {
    super.onStop();
    if(mp.isPlaying()){ //Must check if it's playing, otherwise it may be a NPE
        mp.pause(); //Pauses the sound
        ur.removeCallbacks(myRunnable);
        }
    }
    

  • 如果选择第二种方法,则还必须在onStart方法中启动Callback和MediaPlayer.

    If you choose the second alternative you also have to start your Callback and MediaPlayer in the onStart-method.

    更喜欢第一个选择.

    这篇关于如何在Android的启动画面中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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