// Sample code for speaker testing
// Take an NSButton for example. In `xib`, set the title of the button in the on and off state to "Stop Test" and "Start Test".
- (IBAction)speakerTest:(NSButton *)btn {
NSString *path = [[NSBundle mainBundle] pathForResource:@"test-32000-mono" ofType:@"mp3"];
if (btn.state == NSControlStateValueOn) {
// Click "Start Test".
__weak __typeof(self) wself = self;
[self.trtcEngine startSpeakerDeviceTest:path onVolumeChanged:^(NSInteger volume, BOOL playFinished) {
// The subsequent steps involve the UI and need to be executed in the main queue.
dispatch_async(dispatch_get_main_queue(), ^{
// `_updateOutputVolume` means updating the speaker volume indicator on the UI.
[wself _updateOutputVolume:volume];
if (playFinished) {
// Set the button status to "Start Test" after playback is completed.
sender.state = NSControlStateValueOff;
}
});
}];
} else {
// Click "Stop Test".
[self.trtcEngine stopSpeakerDeviceTest];
[self _updateOutputVolume:0];
}
}
// Update the speaker volume indicator.
- (void)_updateOutputVolume:(NSInteger)volume {
// `speakerVolumeMeter` is `NSLevelIndicator`.
self.speakerVolumeMeter.doubleValue = volume / 255.0 * 10;
}