0

アクティビティがあります。これはsplash activityです。2 つの音楽をロードしたいのですが、ロードが完了したらウェルカム ページを開始したいのですが、残念ながら何か問題がありました。

logロードが完了したときに作成したものを取得しましたが、2つの音楽がロードされていないときにもログを取得しました..

ログ

Loaded 1 true
Loaded 2 true
1 not two
2 not two

コード

package com.Syriatel.EatTel;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class Splash extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        new Load1().execute(1);
        new Load2().execute(2);
    }

    public int soundID, soundID2;

    private SoundPool soundPool1, soundPool2;
    boolean isLoad1, isLoad2;

    class Load2 extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            if (isLoad1 && isLoad2) {
                Intent intent = new Intent(Splash.this, Welcome.class);
                finish();
                startActivity(intent);
            }else{
                Log.e("2", "not two");
            }
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
            soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    isLoad2 = true;
                    Log.e("Loaded 2", "true");
                }
            });
            soundID2 = soundPool2.load(getApplicationContext(), R.raw.select, 1);
            return 1;
        }

    }

    class Load1 extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            if (isLoad1 && isLoad2) {
                Intent intent = new Intent(Splash.this, Welcome.class);
                finish();
                startActivity(intent);
            }else{
                Log.e("1", "not two");
            }
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            soundPool1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
            soundPool1.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    isLoad1 = true;
                    Log.e("Loaded 1", "true");
                }
            });
            soundID = soundPool1.load(getApplicationContext(), R.raw.thip, 1);
            return 1;
        }
    }
}
4

4 に答える 4

2

問題は、それぞれの内部から別のスレッドを作成していることですAsynctask。電話すると

soundID = soundPool1.load(getApplicationContext(), R.raw.thip, 1);

別のスレッドが音楽のロードを開始します。ただし、 はAsyncTaskそのスレッドで継続し、 complete doInBackground、 go to に到達onPostexecuteすると、どちらのフラグも設定されませsoundPool1.load()

あなたのコードは期待どおりに実行されています!

これを解決するには、コード構造を使用して、両方の doInBackgroundメソッドにコードを追加する必要があります。更新された番号2は次のとおりです。

protected Integer doInBackground(Integer... params) {
    soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            isLoad2 = true;
            Log.e("Loaded 2", "true");
        }
    });
    soundID2 = soundPool2.load(getApplicationContext(), R.raw.select, 1);

    // New Code so we wait until the load is complete
    while(isLoad2 == false) {
        Thread.sleep(1000); // 1 second (change as you feel fit)
    }

onPostExecuteその後、音楽が実際にロードされたときにのみ到達します。フラグを確実に初期化することも賢明であるため、各 AsyncTask で次のようにします。

@override
protected void onPreExecute() {
    isLoadX = false
}

多くの AsyncTasks を計画している場合は、これこれを読む必要があります。

于 2013-07-11T07:05:38.677 に答える
1

onCreate() から次のコードを削除し、load1 の PostExecute() に配置します。

new Load2().execute(2);

load2 の PostExecute() に launch your welcome アクティビティを記述します。

于 2013-07-11T06:50:16.917 に答える
0

ここのロジックは間違っています。2 つのシミュレート ジョブを開始しました。そして、それぞれのジョブで、ロードされた両方の像を要求します。

if (isLoad1 && isLoad2) {

}else{

}

私の解決策は、3番目のスレッドを作成できることです。両方のロード状況を確認してください。準備ができていない場合は、スリープしてもう一度確認してください。この助けを願っています

于 2013-07-11T06:50:14.887 に答える