我试图构建一个闹钟应用程序。当警报开启时,用户必须在关闭之前扫描一个匹配的QR码。我查看了此链接以播放声音:How to play ringtone/alarm sound in Android,我正在使用QR码扫描器的zxing库中的ScanningViaIntent:https://code.google.com/p/zxing/。如何在Android中停止声音
于是我开始了声音的在onStart()活动:
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(AlarmRequirementsActivity.this);
integrator.initiateScan();
}
}
这里返回扫描仪的结果:
@Override
public void onStart(){
super.onStart();
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
}
然后用户通过按下一个按钮开始扫描:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
System.out.println("scanREsult" + scanResult);
System.out.println("requestCode: " + requestCode);
TextView result =(TextView)findViewById(R.id.scanResult);
if (resultCode == RESULT_OK) {
String scanResultString = intent.getStringExtra("SCAN_RESULT");
if(scanResultString .equals(matchString))
{
result.setText("You found it!");
r.stop();
}
else
{
result.setText("\"" + scanResultString + "\""+ " did not match");
}
System.out.println(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
}
}
// else continue with any other code you need in the method
}
正如您所看到的,我在成功匹配后调用r.stop()。但是,这些是我的问题:
- 活动从扫描仪返回后重新启动。这场比赛是否成功并不重要。
- 这导致两个闹钟铃声现在
正在播放我试图把它在onCreate()方法,但无济于事为好。
UPDATE:
我已经试过:
@Override
public void onStart(){
super.onStart();
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
if(!r.isPlaying())
{
r.play();
}
}
,这下面。这两个与同样的问题
if(scanResultString .equals(matchString))
{
result.setText("You found it!");
if(r.isPlaying())
{
r.stop();
}
}
这样做。刚把它移到了onCreate()。我现在已经强迫这个方向成为肖像,这是一种处理方向变化的懒惰方式,我猜。 – 2013-04-20 19:52:26