Android MediaPlayer启动时声音延迟

Android MediaPlayer启动时声音延迟,android,android-mediaplayer,Android,Android Mediaplayer,我正在和Android Studio一起制作生日卡,在MainActivity出现在屏幕上之前,音频文件的播放出现了一个小问题。启动屏幕仍在屏幕上时播放音频文件 我有两个Java文件: 主课 package com.example.android.happybirthday; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;

我正在和Android Studio一起制作生日卡,在MainActivity出现在屏幕上之前,音频文件的播放出现了一个小问题。启动屏幕仍在屏幕上时播放音频文件

我有两个Java文件:

主课

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();
    }
}
还有斯普莱斯·斯普莱斯

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();
    }
}
这是AndroidManifest.xml文件

<?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>

你能给我一些支持吗?
谢谢:)

您需要在活动启动时运行媒体播放器,而不是在“创建”屏幕上运行。
尝试在onStart()或onResume()上运行media player。

活动恢复完成时调用

protected void onPostResume()

谢谢@Sergey和@You Kim,我通过在MainActivity.class中的onStart中添加一个延迟来完成它。我已经尝试过:
@Override protected void onResume(){super.onResume();mySoundfile=MediaPlayer.create(this,R.raw.music);mySoundfile.seekTo(0);mySoundfile.start();}
和此:
@Override protected void onStart(){super.onStart();mySoundfile=MediaPlayer.create(this,R.raw.music);mySoundfile.seekTo(0);mySoundfile.start();}
但是它们仍然在MAIN活动可见之前播放声音。请尝试删除“android.intent.action.MAIN”从主要活动​ 在manifest.Sergey中,我从
activity-android:name=“.MainActivity”
中删除了
android.intent.action.Main
,但它仍然给出相同的结果。
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();
    }
}