2017-04-09 3 views
0

Android Studioで誕生日カードを作成するプレイをしていて、MainActivityが画面に表示される前にオーディオファイルにわずかな問題が発生しました。スプラッシュ画面がまだ画面に表示されている間、オーディオファイルが再生されます。起動時にAndroid MediaPlayerの音が遅れる

私は2つのJavaファイルがあります。これは、AndroidManifest.xmlにはファイルです

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

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

アクティビティが開始されたときに、作成時ではなくメディアプレーヤーを実行する必要があります。 onStart()またはonResume()でメディアプレーヤーを実行してみてください。活動再開が完了したときに呼び出され

+0

内ONSTARTの遅延を追加することによって行わ取得するには: '@Override protected void onResume(){ super.onResume(); mySoundfile = MediaPlayer.create(this、R.raw.music); mySoundfile.seekTo(0); mySoundfile.start(); } ' これ: ' @Override保護ボイドONSTART(){ super.onStart()。 mySoundfile = MediaPlayer.create(this、R.raw.music); mySoundfile.seekTo(0); mySoundfile.start(); } ' でもMainActivityが表示される前に音が再生されます。 – davemib123

+0

マニフェストのメインアクティビティから "android.intent.action.MAIN"を削除してください。 – Sergey

+0

Sergeyさん、 'android.intent.action.Main'を' activity android:name = "。MainActivity"から削除しましたが、それでも同じ結果が出ます。 – davemib123

0

protected void onPostResume() 
0
@Sergeyと@Youキムは、私が管理

おかげで、私はこれを試してみたMainActivity.class

関連する問題