0

我在这里上了这门课:

public class ViewBeat1 extends MasterView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            stream = getAssets().open("some_gif.gif");
        } catch (IOException e) {
            e.printStackTrace();
        }
        GifMovieView view = new GifMovieView(this, stream);

        setContentView(view);
        run();
    }

    public void run() {

        // Get the ringtone

        int milliseconds = 0;
        final SoundPool sp = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        final int loaded = sp.load(this, R.raw.beat, 1);

        boolean played = false;


        while (milliseconds < 300000) {
            if (milliseconds >= 20000 && !played) {
                sp.stop(loaded);
                sp.release();
                played = true;
                playSong(this.getClass().getSimpleName().toLowerCase(),
                        View6.class.getName(), 4000);
                finish();
            } else if (milliseconds <= 20000) {
                try {
                    sp.play(loaded, 1, 1, 1, 0, 1);
                    Thread.sleep(500);
                    milliseconds += 500;


                } catch (Exception e) {
                    Log.v("out of the thread", e.getMessage().toString());
                }
            }
        }
    }
}

MasterView 从 Activity 扩展而来,只是做了一些这里最可能不需要的东西。GifMovieView 设置一个 gif 动画并将其放入 Contentview。

现在我想每 500 毫秒播放一次这个 500 毫秒的长波文件,有点像快速心跳(120 bpm)。不知何故,当我这样做时,它无法正常工作,并且应用程序不再响应。有没有人有更好的解决方案来实现这一目标?

为您的帮助干杯 - StackOverflow 社区是最好的!

4

2 回答 2

1

您的应用程序变得无响应,因为您从未从 onCreate 方法返回!

尝试创建一个处理程序并每 500 毫秒自动发布一次。像下面这样的东西,也许

public class ViewBeat1 extends MasterView {
    private SoundPool mSoundPool;
    private Handler mHandler;
    private Runnable mPlaySoundRunnable;
    private Runnable mFinishRunnable;
    private int mSoundId;
    private int mPlayCount;
    private long mFinishTime;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        try {
            stream = getAssets().open("some_gif.gif");
        } catch (IOException e) {
            e.printStackTrace();
            finish();
            return;
        }
        setContentView(new GifMovieView(this, stream));

        mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        mHandler = new Handler();

        mPlaySoundRunnable = new Runnable() {
            @Override
            public void run() {
                mPlayCount++;
                mSoundPool.play(mSoundId, 1f, 1f, 1, 0, 1f);

                if (mPlayCount < 40) {
                    mHandler.postDelayed(mPlaySoundRunnable, 500L);
                }
            }
        };

        mFinishRunnable = new Runnable() {
            @Override
            public void run() {
                mSoundPool.stop(mSoundId);
                mSoundPool.release();
                playSong(Activity.this.getClass().getSimpleName().toLowerCase(), View6.class.getName(), 4000);
                finish();
            }
        };

        mSoundId = mSoundPool.load(this, R.raw.beat, 1);
        if (mSoundId > 0) {
            mHandler.post(mPlaySoundRunnable);
        }

        mFinishTime = System.currentTimeMillis() + 30000L;
        mHandler.postAtTime(mFinishRunnable, mFinishTime);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeCallbacks(mFinishRunnable);
        mHandler.removeCallbacks(mPlaySoundRunnable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.postAtTime(mFinishRunnable, mFinishTime);
        if (mPlayCount < 40) {
            mHandler.post(mPlaySoundRunnable);
        }
    }
}
于 2013-08-29T20:10:55.377 回答
0

您不应该在 gui 线程中这样做,因为它几乎会阻塞所有内容。这就是为什么您的应用不再响应的原因。

像这样开始一个新线程

public class ViewBeat1 extends MasterView implements Runnable {
    ...
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...
        // instead of calling run() directly, start in new thread
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() {
    ...
    }
}
于 2013-08-29T19:01:01.253 回答