Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android的闪屏中播放音频_Android - Fatal编程技术网

如何在android的闪屏中播放音频

如何在android的闪屏中播放音频,android,Android,如何在启动屏幕期间播放音频。 需要指导。您可以使用该类播放音频文件 范例 MediaPlayer player = new MediaPlayer(); player.setDataSource("/sdcard/audiotrack.mp3"); player.prepare(); player.start(); 您可以使用该类播放音频文件 范例 MediaPlayer player = new MediaPlayer(); player.setDataSource("/sdcard/aud

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

您可以使用该类播放音频文件

范例

MediaPlayer player = new MediaPlayer();
player.setDataSource("/sdcard/audiotrack.mp3");
player.prepare();
player.start();

您可以使用该类播放音频文件

范例

MediaPlayer player = new MediaPlayer();
player.setDataSource("/sdcard/audiotrack.mp3");
player.prepare();
player.start();
我的方法是(无需外部声音,因为我将声音文件放在我的资源文件夹中):

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

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

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

  • 破坏整个活动:

    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中停止声音:

     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

    我更喜欢第一种选择。

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

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

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

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

  • 破坏整个活动:

    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中停止声音:

     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 2.2之前,用户只有有限的内存来保存应用程序。因此,最好将它们存储在外部内存中。如果你有一些小的声音文件,这并不重要,但是如果你有很多的话,那就不重要了。应该注意的是,在Android 2.2之前,用户只有有限的内存来保存应用程序。因此,最好将它们存储在外部内存中。如果你有一些小的声音文件,这并不重要,但如果你有很多,它会。