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社区是最好的!
答案 0 :(得分:1)
您的应用无法响应,因为您永远不会从onCreate方法返回!
尝试创建Handler并每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);
}
}
}
答案 1 :(得分: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() {
...
}
}