ios - 使用 iBeacon 触发音频

标签 ios objective-c avaudioplayer ibeacon

我正在尝试制作一个使用 iBeacons 在一定距离内触发音频的应用程序。在这种情况下,它是在紧邻信标时。我写了一个执行此操作的 if 语句,唯一的问题是只要设备在附近,音频文件就会一直重新启动。我希望它只触发一次,播放并完成。不要一遍又一遍地触发。

要播放的音频在这里定义。

-(void)playSound
{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/05 Rooster.m4a", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 1;
audioPlayer.currentTime = 0;
audioPlayer.volume = 100.0;

[audioPlayer play];
}

稍后在if语句中调用音频

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
beacon = [beacons lastObject];

self.beaconFoundLabel.text = @"In Range";
self.proximityUUIDLabel.text = beacon.proximityUUID.UUIDString;
self.majorLabel.text = [NSString stringWithFormat:@"%@", beacon.major];
self.minorLabel.text = [NSString stringWithFormat:@"%@", beacon.minor];
self.accuracyLabel.text = [NSString stringWithFormat:@"%f", beacon.accuracy];

if (beacon.proximity == CLProximityUnknown) {
    self.distanceLabel.text = @"Unknown Proximity";
}

else if (beacon.proximity == CLProximityImmediate)
{
    self.distanceLabel.text = @"Immediate";

    self.view.backgroundColor = [UIColor greenColor];
    _portrait.hidden = YES;
    _portraitText.hidden = YES;
    _landscape.hidden = NO;
    _landscapeText.hidden = NO;

    _artworkTitle.text = @"Impression, Sunrise (Impression, soleil levant), 1872";


    UIImage *photo = [UIImage imageNamed: @"Sunrise.png"];
    [_landscape setImage:photo];
    _landscapeText.text = @"Dated 1872, its subject is the harbour of Le Havre in France, using very loose brush strokes that suggest rather than delineate it. Monet explained the title later: \n\nLandscape is nothing but an impression, and an instantaneous one, hence this label that was given us, by the way because of me. I had sent a thing done in Le Havre, from my window, sun in the mist and a few masts of boats sticking up in the foreground.\n\n They asked me for a title for the catalogue, it couldn't really be taken for a view of Le Havre, and I said: 'Put Impression.'";

    [self playSound];


}

最佳答案

定义一个类似Bool isPlaying的属性,并在viewDidLoad中将其设置为false

实现(这是播放完当前歌曲后调用的):

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
        self.isPlaying = false;
}

在玩游戏时不要管理 iBeacon 更改

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
  if (self.isPlaying) return;

...

  [self playSound];
  self.isPlaying = true;
}

希望这能有所帮助

关于ios - 使用 iBeacon 触发音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22944934/

相关文章:

iphone - 推送通知警报文本的最大长度是多少?

ios - 快速控制应用程序流

ios - 如何在 iOS 配置门户中创建新的应用程序 ID?

ios - XCode 将不再在设备/模拟器上运行我的应用程序

objective-c - C# 接口(interface)和 Objective C @prototypes 是否相同?

ios - 检测到堆损坏 - 仅限 iPhone 5S

objective-c - NSArrayController 和特定实体

iPhone - 使用 AVAudioPlayer 防止播放声音结束时发出滴答声

iphone - iOS AVAudioPlayer OSStatus 错误-43

ios - 如何停止在 Xcode 中播放不同类的背景音乐?