android - 如何在android的启动画面中播放音频

标签 android

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

最佳答案

我这样做的方式(不需要外部声音,因为我把我的声音文件放在我的资源文件夹中):

在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 完全支持它。

下面是关于在 Splash Screen Activity 停止时处理声音的重要事项:

有两种通用的方法来管理停止时的启动画面(以及其中的声音):

  1. 销毁整个 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();
    }
    
  2. 或者您可以在 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 方法中启动您的回调和 MediaPlayer。

更喜欢第一种选择。

关于android - 如何在android的启动画面中播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3370211/

相关文章:

java - 如何通过上下文菜单从 SQLite 数据库中删除项目?

java - Android 中的电话

android - 播放计费库连接和查询sku详情

Android Asynctasks,在 Activity 之间传递字符串

android - 如何更改工具栏上的菜单图标大小?

java - 即使主项目有语法错误,也可以在 android studio 中独立测试简单的 java 代码

android - 'setHasOptionsMenu(Boolean) : Unit' is deprecated. 在 Java 中已弃用

android - 如何在 Android 中使用 native 代码将文件从一个目录复制到另一个目录?

android - Android 中的分层窗口

android - 禁用 Android 资源/图像/PNG 优化