【发布时间】:2017-03-03 19:30:54
【问题描述】:
我在 Xamarin 中开发的 Android 应用中使用 ZXing 扫描二维码并开始自动播放相应的音频文件。
我的问题是,当我从扫描中获得结果时,加载音频播放器活动需要一些时间,因此由于后续成功扫描,它会被调用两次或更多次。
有没有办法在我得到正确结果后立即停止连续扫描?
代码如下:
//Start scanning
scanner.ScanContinuously(opt, HandleScanResult);
}
private void HandleScanResult(ZXing.Result result)
{
string msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
{
msg = result.Text;
var playerActivity = new Intent(myContext, typeof(AudioActivity));
//-------------------------------------------------------------
// Prerequisite: load all tracks onto "Assets/tracks" folder
// You can put here qr code - track assignments here below
// msg: decoded qr code
// playerActivity.Putextra second parameter is a relative path
// under "Assets" directory
//--------------------------------------------------------------
//Iterate through tracks stored in assets and load their titles into an array
System.String[] trackArray = Application.Context.Assets.List("tracks");
bool trackFound = false;
foreach (string track in trackArray)
{
if (track.Equals(msg + ".mp3"))
{
playerActivity.PutExtra("Track", "tracks/" + msg + ".mp3");
for (int i = 0; i < PostList.postList.Count; i++)
{
if (PostList.postList.ElementAt(i).code.Equals(msg))
playerActivity.PutExtra("TrackTitle", PostList.postList.ElementAt(i).title);
}
myContext.StartActivity(playerActivity);
trackFound = true;
}
}
谢谢!
【问题讨论】:
-
你试过
scanner.Stop()吗? -
如果我在 HandleScanResult 中放入类似的内容,则会收到如下错误消息:错误 CS0103 The name 'scanner' does not exist in the current context
-
让它成为类的成员变量?
-
没用,显然是scanner.Cancel();由于错误而无法正常工作...?
标签: android xamarin zxing scanning continuous