我正在玩Android Studio来创建生日卡,并且在MainActivity甚至在屏幕上播放之前播放的音频文件出现了轻微问题。当闪屏仍在屏幕上时播放音频文件。
我有两个Java文件:
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();
}
}
和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();
}
}
这是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>
你能给予一些支持吗? 谢谢:))
答案 0 :(得分:1)
感谢@Sergey和@You Kim,我设法通过在MainActivity.class中添加onStart延迟来完成它
答案 1 :(得分:0)
您需要在活动开始时运行媒体播放器,而不是在创建时运行。 尝试在onStart()或onResume()上运行媒体播放器。
答案 2 :(得分:0)
Called when activity resume is complete
protected void onPostResume()