在启动画面上播放声音启动

时间:2015-09-29 13:02:38

标签: android

我的应用程序中有一个启动画面,加载3秒,而它正在加载我想要播放的声音效果,我有一个ogg文件,并希望每次启动应用程序时播放,可以有人告诉我该怎么做呢?我已经包含了泼水活动。

public class SplashActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getSupportActionBar().hide();
            setContentView(R.layout.activity_splash);

            Runnable runable3secs = new Runnable() {
                @Override
                public void run() {
                    nextActivity();
                    finish();
                }
            };
        Handler myHandler = new Handler();
        myHandler.postDelayed(runable3secs,3000);
    }

    public void nextActivity(){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}

4 个答案:

答案 0 :(得分:2)

您可以通过运行线程和媒体播放器来完成此操作。别忘了导入所需的一切。

private void radioButtonDisabler(){

 for(int i = 0; i < mBottomRadioGroup.getChildCount(); i++){
        mBottomRadioGroup.getChildAt(i).setEnabled(false);
    }
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for(int i = 0; i < mBottomRadioGroup.getChildCount(); i++){
                mBottomRadioGroup.getChildAt(i).setEnabled(true);
            }
        }
    }, 1000); //Re-enable after each one after desired time.
}

此外,您必须覆盖public class SplashActivity extends AppCompatActivity { MediaPlayer mySong; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_splash); mySong=MediaPlayer.create(Splash.this,R.raw.your_audio_file); mysong.start(); Thread timer=new Thread(){ public void run(){ try{ sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); }finally{ nextActivity(); }}} ; timer.start(); } } public void nextActivity(){ Intent intent = new Intent(this,MainActivity.class); startActivity(intent); } 方法,如下所示

onPause()

答案 1 :(得分:1)

嘿,请使用此链接Splash screen with sound

答案 2 :(得分:1)

将声音文件放入res/raw文件夹,然后创建MediaPlayer实例,使用MediaPlayer.create引用该资源,&gt;然后在实例上调用start():

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

停止使用声音:

mp.stop()

答案 3 :(得分:1)

我不喜欢 Android,但是否有一个与术语“如何在 flutter Splash Screen 中播放自定义声音”相关的搜索你的帖子正在跟进,我在这里帮助不喜欢 kotlin 的 Flutter 开发者社区、java 等,所以没有开发人员会赤手空拳,所以对于可能正在搜索这个词但找不到互联网结果的 Flutter 社区的人,我把你保存在某个地方,

使用这种代码。

final player = AudioCache(); //Define the player

    class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: Future.delayed(Duration(seconds: 3)),
        builder: (context, AsyncSnapshot snapshot) {
          // Show splash screen while waiting for app resources to load:
          if (snapshot.connectionState == ConnectionState.waiting) {
            player.play('sounds/notification_ambient.wav'); //Here is what will play sounds.

            return MaterialApp(home: Splash());
          } else {
            // Loading is done, return the app:
            return MaterialApp(
                debugShowCheckedModeBanner: false,
                home: TodoApp(),
                title: 'Toodolee',
                theme: ThemeData(
                  brightness: Brightness.light,
                  fontFamily: "WorkSans",
                ));
          }
        });
  }
}

class Splash extends StatefulWidget {
  @override
  _SplashState createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white70,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FadeIn(
              duration: Duration(milliseconds: 1200),
              child: Center(
                  child: Icon(CarbonIcons.checkmark,
                      size: 90, color: Colors.black87)),
            ),
            FadeOut(
                duration: Duration(milliseconds: 1100),
                child: Center(child: Text("Made by Proco :love"))),
          ],
        ),
      ),
    );
  }
}

不要忘记导入 import 'package:audioplayers/audio_cache.dart';

import 'package:audioplayers/audioplayers.dart'; 并在 pubspec.yaml 中声明 audioplayers: 和资产中的位置。

希望它会有所帮助,您也可能会发现有关无法播放声音的错误,所以不要担心,而不是播放这样的声音,player.play('assets\sounds/notification_ambient.wav');播放,player.play('sounds/notification_ambient.wav');不要总是提到资产它有时不起作用,所以享受......谢谢......