WFPttViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // WFPttViewController.m
  3. // PttUIKit
  4. //
  5. // Created by Hao Jia on 2021/10/14.
  6. //
  7. #ifdef WFC_PTT
  8. #import "WFPttViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import <PttClient/WFPttClient.h>
  11. #import <WFChatUIKit/WFChatUIKit.h>
  12. #import "WFCUUtilities.h"
  13. @interface WFPttViewController ()
  14. @property(nonatomic, strong)UIButton *startButton;
  15. @end
  16. @implementation WFPttViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.view.backgroundColor = [UIColor whiteColor];
  20. #define BTN_HEIGHT 48
  21. self.startButton = [[UIButton alloc] initWithFrame:CGRectZero];
  22. self.startButton.layer.masksToBounds = YES;
  23. [self.startButton addTarget:self action:@selector(onStart:) forControlEvents:UIControlEventTouchDown];
  24. [self.startButton addTarget:self action:@selector(onEnd:) forControlEvents:UIControlEventTouchUpInside];
  25. [self.startButton addTarget:self action:@selector(onEnd:) forControlEvents:UIControlEventTouchUpOutside];
  26. [self.view addSubview:self.startButton];
  27. [self updateStartBtnStatus];
  28. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Close") style:UIBarButtonItemStylePlain target:self action:@selector(onClose:)];
  29. __weak typeof(self)ws = self;
  30. [[NSNotificationCenter defaultCenter] addObserverForName:kWFPttTalkingBeginNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  31. [ws updateStartBtnStatus];
  32. }];
  33. [[NSNotificationCenter defaultCenter] addObserverForName:kWFPttTalkingEndNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  34. [ws updateStartBtnStatus];
  35. }];
  36. }
  37. - (void)updateStartBtnStatus {
  38. self.startButton.hidden = NO;
  39. CGFloat btnSize = 100;
  40. NSString *title = WFCString(@"PressToTalk");
  41. UIColor *color = [UIColor redColor];
  42. if([[WFPttClient sharedClient] getTalkingConversation] == self.conversation) {
  43. btnSize = 150;
  44. title = WFCString(@"ReleaseEndTalk");
  45. color = [UIColor greenColor];
  46. }
  47. CGRect bound = self.view.bounds;
  48. self.startButton.frame = CGRectMake((bound.size.width - btnSize)/2, self.view.bounds.size.height - [WFCUUtilities wf_safeDistanceBottom] - btnSize/2 - 160, btnSize, btnSize);
  49. [self.startButton setTitle:title forState:UIControlStateNormal];
  50. self.startButton.backgroundColor = color;
  51. self.startButton.layer.cornerRadius = btnSize/2;
  52. }
  53. - (void)dismiss:(id)sender {
  54. if (self.navigationController.presentingViewController) {
  55. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  56. }else{
  57. [self.navigationController popViewControllerAnimated:YES];
  58. }
  59. }
  60. - (void)viewWillAppear:(BOOL)animated {
  61. [self updateMemberStatus];
  62. }
  63. - (void)updateMemberStatus {
  64. }
  65. - (void)onClose:(id)sender {
  66. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  67. }
  68. - (void)onStart:(id)sender {
  69. __weak typeof(self)ws = self;
  70. [[WFPttClient sharedClient] requestTalk:self.conversation startTalking:^(void) {
  71. NSLog(@"talking now...");
  72. [ws playPttRing:@"ptt_begin"];
  73. [ws updateStartBtnStatus];
  74. } onAmplitude:^(int averageAmp){
  75. NSLog(@"on amp %d", averageAmp);
  76. } requestFailure:^(int errorCode) {
  77. NSLog(@"request talking failure");
  78. [ws updateStartBtnStatus];
  79. } talkingEnd:^(PttEndReason reason) {
  80. NSLog(@"talking ended");
  81. [ws playPttRing:@"ptt_end"];
  82. [ws updateStartBtnStatus];
  83. }];
  84. }
  85. - (void)onEnd:(id)sender {
  86. self.startButton.backgroundColor = [UIColor blueColor];
  87. [[WFPttClient sharedClient] releaseTalking:self.conversation];
  88. }
  89. - (void)playPttRing:(NSString *)ring {
  90. if([[UIApplication sharedApplication].delegate respondsToSelector:@selector(playPttRing:)]) {
  91. [[UIApplication sharedApplication].delegate performSelector:@selector(playPttRing:) withObject:ring];
  92. }
  93. }
  94. -(void)dealloc {
  95. [[NSNotificationCenter defaultCenter] removeObserver:self];
  96. }
  97. @end
  98. #endif //WFC_PTT