直到将相机从 Qr 代码移开才播放音频 - iOS
Audio not playing until moving the camera away from Qr code - iOS
我是 iOS
development
的新手,我正在开发一个应用程序,我想在检测到 QR code
时播放一些音频。我能够检测到 QR code
并播放音频,但问题是当我将相机从 QR code
移开时仅播放音频。因此,如果我将相机指向 QR code
,它不会播放音频,但只要我移动相机(即没有检测到 QR code
),音频就会开始播放。我不希望这种行为,我希望在检测到 QR code
后立即播放音频。任何建议都会很棒,因为我刚开始 iOS
development
2 3 天前。谢谢。
-(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection(AVCaptureConnection *)connection{
NSString *detected=@"";
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
detected=[NSString stringWithString:[metadataObj stringValue]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"cbp" ofType:@"mp3"];
NSURL *url = [NSURL URLWithString:path];
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[_audioPlayer play];
}
}
}
不要在这个 AVFoundation
委托方法中初始化 AVAudioPlayer
,扫描开始前用声音文件加载播放器。
(void)loadSound{
NSString *beepFilePath = [[NSBundle mainBundle] pathForResource:@"cbp" ofType:@"mp3"];
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
if (error) {
NSLog(@"Could not play sound file.");
NSLog(@"%@", [error localizedDescription]);
}
else{
[_audioPlayer prepareToPlay];
}
}
您可以在 viewWillAppear()
method.And 中调用 loadSound()
函数,您的 AVFoundation
委托方法将如下所示:-
(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection(AVCaptureConnection *)connection{
NSString *detected=@"";
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
detected=[NSString stringWithString:[metadataObj stringValue]];
if (_audioPlayer) {
[_audioPlayer play];
}
}
}
}
试试这个。
我是 iOS
development
的新手,我正在开发一个应用程序,我想在检测到 QR code
时播放一些音频。我能够检测到 QR code
并播放音频,但问题是当我将相机从 QR code
移开时仅播放音频。因此,如果我将相机指向 QR code
,它不会播放音频,但只要我移动相机(即没有检测到 QR code
),音频就会开始播放。我不希望这种行为,我希望在检测到 QR code
后立即播放音频。任何建议都会很棒,因为我刚开始 iOS
development
2 3 天前。谢谢。
-(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection(AVCaptureConnection *)connection{
NSString *detected=@"";
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
detected=[NSString stringWithString:[metadataObj stringValue]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"cbp" ofType:@"mp3"];
NSURL *url = [NSURL URLWithString:path];
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[_audioPlayer play];
}
}
}
不要在这个 AVFoundation
委托方法中初始化 AVAudioPlayer
,扫描开始前用声音文件加载播放器。
(void)loadSound{
NSString *beepFilePath = [[NSBundle mainBundle] pathForResource:@"cbp" ofType:@"mp3"];
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
if (error) {
NSLog(@"Could not play sound file.");
NSLog(@"%@", [error localizedDescription]);
}
else{
[_audioPlayer prepareToPlay];
}
}
您可以在 viewWillAppear()
method.And 中调用 loadSound()
函数,您的 AVFoundation
委托方法将如下所示:-
(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection(AVCaptureConnection *)connection{
NSString *detected=@"";
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
detected=[NSString stringWithString:[metadataObj stringValue]];
if (_audioPlayer) {
[_audioPlayer play];
}
}
}
}
试试这个。