简体   繁体   English

iOS将哔声添加到条形码扫描仪

[英]iOS add beep to barcode scanner

I have been using the Scanning Barcodes with iOS 7 (Objective-C) from infragistics and it works well. 我一直在使用Infragistics的iOS 7扫描条形码(Objective-C),效果很好。 I am trying to add a beep when it completes a successful scan but keep getting an error. 我试图在成功完成扫描后添加提示音,但始终出现错误。 I'm sure its an easy fix for someone who knows more than I. 对于那些比我了解更多的人,我相信这是一个简单的解决方案。

-(void)loadBeepSound{
    // Get the path to the beep.mp3 file and convert it to a NSURL object.
    NSString *beepFilePath = [NSString stringWithFormat:@"%@/beep.mp3", [[NSBundle mainBundle] resourcePath]];
    NSURL *beepURL = [NSURL URLWithString:beepFilePath];

    NSError *error;

    // Initialize the audio player object using the NSURL object previously set.
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
    if (error) {
        // If the audio player cannot be initialized then log a message.
        NSLog(@"Could not play beep file.");
        NSLog(@"%@", [error localizedDescription]);
        NSLog(@"beepFilePath %@", beepFilePath);
    }
    else{
        // If the audio player was successfully initialized then load it in memory.
        [_audioPlayer prepareToPlay];
    }
}

The Error is logging as - 错误记录为-

-Could not play beep file.
-The operation couldn’t be completed. (OSStatus error -10875.)
-beepFilePath /private/var/mobile/Containers/Bundle/Application/22BB6164-82F8-46E5-B4B6-  4C91BCA2B7E9/ReThink Edu App.app/beep.mp3

and so it seems to be an issue with loading the beep sound but it is in the main bundle and the target membership box is ticked. 因此加载蜂鸣声似乎是一个问题,但它在主捆绑包中,并且选中了目标成员资格框。 Maybe I'm just trying to go about this the wrong way! 也许我只是想以错误的方式进行操作!

Instead of this: 代替这个:

    NSURL *beepURL = [NSURL URLWithString:beepFilePath]; 

Try this instead: 尝试以下方法:

    NSURL *beepURL = [NSURL fileURLWithPath:beepFilePath];

When you use [NSURL URLWithString:beepFilePath]; 当您使用[NSURL URLWithString:beepFilePath]; it only gets string of the folder path like Users/***/folder/etc . 它只获取文件夹路径的字符串,例如Users/***/folder/etc

So use [NSURL fileURLWithPath:beepFilePath]; 因此,请使用[NSURL fileURLWithPath:beepFilePath]; this code to will full path like file:///Users/***/folder/etc . 此代码将完整路径如file:///Users/***/folder/etc

Working Code: 工作代码:

NSURL *beepURL = [NSURL fileURLWithPath:beepFilePath];

Optional Method: 可选方法:

NSURL *beepURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", beepFilePath]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM