Search code examples
androidandroid-mediaplayer

Android MediaPlayer sound delayed on start


I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.

I have two Java files:

MainActivity.class

package com.example.android.happybirthday;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mySoundfile;

    @Override
    protected void onPause() {
        super.onPause();
        mySoundfile.release();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySoundfile = MediaPlayer.create(this, R.raw.music);
        mySoundfile.setLooping(true);
        mySoundfile.setVolume(100, 100);
        mySoundfile.seekTo(0);
        mySoundfile.start();
    }
}

and Splash.class

package com.example.android.happybirthday;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        Thread mythread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(3000); // 3 second delay for cold start
                    Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
                    startActivity(startMainApp); // open MainActivity
                    finish(); // close this activity
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        mythread.start();
    }
}

This is the AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.happybirthday">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Splash"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Could you kindly give some support? Thanks :)


Solution

  • you need to run the media player when the activity is started, not on the create. try running media player on onStart() or onResume().