Add looping sound button to AppRTCDemo
This exposes the issue where AVAudioPlayer will stop playing when the
VoiceProcessing I/O audio unit is initialized.
BUG=
R=haysc@webrtc.org, henrika@webrtc.org
Review URL: https://codereview.webrtc.org/1710053004 .
Cr-Commit-Position: refs/heads/master@{#11750}
diff --git a/webrtc/examples/objc/AppRTCDemo/ios/ARDMainView.m b/webrtc/examples/objc/AppRTCDemo/ios/ARDMainView.m
index e809cb3..6f52657 100644
--- a/webrtc/examples/objc/AppRTCDemo/ios/ARDMainView.m
+++ b/webrtc/examples/objc/AppRTCDemo/ios/ARDMainView.m
@@ -10,6 +10,8 @@
#import "ARDMainView.h"
+#import <AVFoundation/AVFoundation.h>
+
#import "UIImage+ARDUtilities.h"
// TODO(tkchin): retrieve status bar height dynamically.
@@ -123,12 +125,23 @@
UISwitch *_loopbackSwitch;
UILabel *_loopbackLabel;
UIButton *_startCallButton;
+ UIButton *_audioLoopButton;
+ AVAudioPlayer *_audioPlayer;
}
@synthesize delegate = _delegate;
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
+ NSString *audioFilePath =
+ [[NSBundle mainBundle] pathForResource:@"mozart" ofType:@"mp3"];
+ NSURL *audioFileURL = [NSURL URLWithString:audioFilePath];
+ _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL
+ error:nil];
+ _audioPlayer.numberOfLoops = -1;
+ _audioPlayer.volume = 1.0;
+ [_audioPlayer prepareToPlay];
+
_appLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_appLabel.text = @"AppRTCDemo";
_appLabel.font = [UIFont fontWithName:@"Roboto" size:34];
@@ -171,8 +184,6 @@
[_loopbackLabel sizeToFit];
[self addSubview:_loopbackLabel];
- _startCallButton = [[UIButton alloc] initWithFrame:CGRectZero];
-
_startCallButton = [UIButton buttonWithType:UIButtonTypeSystem];
_startCallButton.backgroundColor = [UIColor blueColor];
_startCallButton.layer.cornerRadius = 10;
@@ -191,6 +202,22 @@
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_startCallButton];
+ // Used to test what happens to sounds when calls are in progress.
+ _audioLoopButton = [UIButton buttonWithType:UIButtonTypeSystem];
+ _audioLoopButton.layer.cornerRadius = 10;
+ _audioLoopButton.clipsToBounds = YES;
+ _audioLoopButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
+ _audioLoopButton.titleLabel.font = controlFont;
+ [_audioLoopButton setTitleColor:[UIColor whiteColor]
+ forState:UIControlStateNormal];
+ [_audioLoopButton setTitleColor:[UIColor lightGrayColor]
+ forState:UIControlStateSelected];
+ [self updateAudioLoopButton];
+ [_audioLoopButton addTarget:self
+ action:@selector(onToggleAudioLoop:)
+ forControlEvents:UIControlEventTouchUpInside];
+ [self addSubview:_audioLoopButton];
+
self.backgroundColor = [UIColor whiteColor];
}
return self;
@@ -237,8 +264,15 @@
_loopbackLabel.center = CGPointMake(loopbackModeLabelCenterX,
CGRectGetMidY(loopbackModeRect));
- CGFloat startCallTop =
+ CGFloat audioLoopTop =
CGRectGetMaxY(loopbackModeRect) + kCallControlMargin * 3;
+ _audioLoopButton.frame = CGRectMake(kCallControlMargin,
+ audioLoopTop,
+ _audioLoopButton.frame.size.width,
+ _audioLoopButton.frame.size.height);
+
+ CGFloat startCallTop =
+ CGRectGetMaxY(_audioLoopButton.frame) + kCallControlMargin * 3;
_startCallButton.frame = CGRectMake(kCallControlMargin,
startCallTop,
_startCallButton.frame.size.width,
@@ -247,6 +281,29 @@
#pragma mark - Private
+- (void)updateAudioLoopButton {
+ if (_audioPlayer.playing) {
+ _audioLoopButton.backgroundColor = [UIColor redColor];
+ [_audioLoopButton setTitle:@"Stop sound"
+ forState:UIControlStateNormal];
+ [_audioLoopButton sizeToFit];
+ } else {
+ _audioLoopButton.backgroundColor = [UIColor greenColor];
+ [_audioLoopButton setTitle:@"Play sound"
+ forState:UIControlStateNormal];
+ [_audioLoopButton sizeToFit];
+ }
+}
+
+- (void)onToggleAudioLoop:(id)sender {
+ if (_audioPlayer.playing) {
+ [_audioPlayer stop];
+ } else {
+ [_audioPlayer play];
+ }
+ [self updateAudioLoopButton];
+}
+
- (void)onStartCall:(id)sender {
NSString *room = _roomText.roomText;
// If this is a loopback call, allow a generated room name.