1

I have a splash screen in my app that loads for 3 seconds, while it is loading i would like a sound effect to be played, i have an ogg file and would like this played every time the the app is launched, can someone show me how to do this please? i have included the splash activity.

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);
    }
}
eleven
  • 6,779
  • 2
  • 32
  • 52
james
  • 157
  • 1
  • 2
  • 11
  • So what have you tried to address your problem? – mittmemo Sep 29 '15 at 13:07
  • I found this, and you could try [StackOverflow-answer][1] [1]: http://stackoverflow.com/a/21043243/2917670 regards – Jose Luis Crisostomo Sep 29 '15 at 13:18
  • so far i have tried mp = MediaPlayer.create(getBaseContext(), R.raw.sound); mp.start(); but i only want the sound whilst the splash screen is on, and to end when the main activity starts – james Sep 29 '15 at 13:24
  • hi jose, thank you for the link, would i have to make a seperate class for the media file to play in the splash? i thought i would be able to do this in the splash activity class – james Sep 29 '15 at 13:26

4 Answers4

3

You can do this by running a thread and mediaplayer. Dont forget to import everything needed.

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);
 }

Also you will have to override the onPause() method like following

 protected void onPause(){
        super.onPause();
        mySong.release();
        finish();
}
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
  • amazing, seems to work, should i not add anything extra in the pause like instead off super.onPause mySong.onpause? – james Sep 29 '15 at 13:54
  • Yes. You need to use `mySong.release()` . I missed it by mistake. I have edited the answer. Thanks @james – Zahan Safallwa Sep 29 '15 at 13:58
1

Hey use this link Splash screen with sound

Sumit Pathak
  • 529
  • 2
  • 7
  • 24
1

Put the sound file in res/raw folder and then create an instance of MediaPlayer, referencing that resource using MediaPlayer.create, > and then call start() on the instance :

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

To stop the sound use :

mp.stop()
Akash Singh
  • 748
  • 1
  • 6
  • 14
  • thank you for your reply Akash, would i put this in the splash activity class or create a seperate class? thanks in advance – james Sep 29 '15 at 13:27
  • In the Splash Activity class. Play the sound in `onCreate()` and do `mp.stop()` in the Runnable. – Akash Singh Sep 29 '15 at 13:30
1

I am not into Android, but wheather there is a seach related to term, "How to play custom sounds in flutter Splash Screen" Your post is following up and i am here to help the Flutter devs community who are not into kotlin, java etc. so no developer go bare hands so for flutter community people who may be searching for this term and not finding internet results I saved you for some place,

Use this kind of Code.

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"))),
          ],
        ),
      ),
    );
  }
}

Don't Forget to Import import 'package:audioplayers/audio_cache.dart';

import 'package:audioplayers/audioplayers.dart'; and in the pubspec.yaml do announce the place for audioplayers: and in the assets too.

Hope it will help, Also You may find errors Regarding the sounds can't be played so don't worry, istead of playing sounds like this, player.play('assets\sounds/notification_ambient.wav'); play like, player.play('sounds/notification_ambient.wav'); don't mention assets always it sometimes don't work, So Enjoy... And Thanks..

Pro Co
  • 341
  • 3
  • 10