| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/voice/text_to_speech_player.h" | 5 #import "ios/chrome/browser/ui/voice/text_to_speech_player.h" |
| 6 | 6 |
| 7 #import <AVFoundation/AVFoundation.h> | 7 #import <AVFoundation/AVFoundation.h> |
| 8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #import "ios/chrome/browser/ui/voice/text_to_speech_player+subclassing.h" | 11 #import "ios/chrome/browser/ui/voice/text_to_speech_player+subclassing.h" |
| 12 #import "ios/chrome/browser/ui/voice/voice_search_notification_names.h" | 12 #import "ios/chrome/browser/ui/voice/voice_search_notification_names.h" |
| 13 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 14 @interface TextToSpeechPlayer ()<AVAudioPlayerDelegate> { | 18 @interface TextToSpeechPlayer ()<AVAudioPlayerDelegate> { |
| 15 // The audio data to be played. | 19 // The audio data to be played. |
| 16 base::scoped_nsobject<NSData> _audioData; | 20 base::scoped_nsobject<NSData> _audioData; |
| 17 // The AVAudioPlayer playing TTS audio data. | 21 // The AVAudioPlayer playing TTS audio data. |
| 18 base::scoped_nsobject<AVAudioPlayer> _player; | 22 base::scoped_nsobject<AVAudioPlayer> _player; |
| 19 // Whether playback has finished. | 23 // Whether playback has finished. |
| 20 BOOL _playbackFinished; | 24 BOOL _playbackFinished; |
| 21 } | 25 } |
| 22 | 26 |
| 23 // Cancels TTS audio playback, and sends a kTTSDidStopPlayingNotification | 27 // Cancels TTS audio playback, and sends a kTTSDidStopPlayingNotification |
| (...skipping 13 matching lines...) Expand all Loading... |
| 37 selector:handler | 41 selector:handler |
| 38 name:notificationName | 42 name:notificationName |
| 39 object:sender]; | 43 object:sender]; |
| 40 } | 44 } |
| 41 return self; | 45 return self; |
| 42 } | 46 } |
| 43 | 47 |
| 44 - (void)dealloc { | 48 - (void)dealloc { |
| 45 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 49 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 46 [self cancelPlayback]; | 50 [self cancelPlayback]; |
| 47 [super dealloc]; | |
| 48 } | 51 } |
| 49 | 52 |
| 50 #pragma mark - Accessors | 53 #pragma mark - Accessors |
| 51 | 54 |
| 52 - (BOOL)isReadyForPlayback { | 55 - (BOOL)isReadyForPlayback { |
| 53 return [_audioData length] > 0; | 56 return [_audioData length] > 0; |
| 54 } | 57 } |
| 55 | 58 |
| 56 - (BOOL)isPlayingAudio { | 59 - (BOOL)isPlayingAudio { |
| 57 return [_player isPlaying]; | 60 return [_player isPlaying]; |
| 58 } | 61 } |
| 59 | 62 |
| 60 - (AVAudioPlayer*)player { | 63 - (AVAudioPlayer*)player { |
| 61 return _player; | 64 return _player; |
| 62 } | 65 } |
| 63 | 66 |
| 64 #pragma mark - Public | 67 #pragma mark - Public |
| 65 | 68 |
| 66 - (void)prepareToPlayAudioData:(NSData*)audioData { | 69 - (void)prepareToPlayAudioData:(NSData*)audioData { |
| 67 if (self.playingAudio) | 70 if (self.playingAudio) |
| 68 [self cancelPlayback]; | 71 [self cancelPlayback]; |
| 69 _audioData.reset([audioData retain]); | 72 _audioData.reset(audioData); |
| 70 [[NSNotificationCenter defaultCenter] | 73 [[NSNotificationCenter defaultCenter] |
| 71 postNotificationName:kTTSAudioReadyForPlaybackNotification | 74 postNotificationName:kTTSAudioReadyForPlaybackNotification |
| 72 object:self]; | 75 object:self]; |
| 73 } | 76 } |
| 74 | 77 |
| 75 - (void)beginPlayback { | 78 - (void)beginPlayback { |
| 76 // no-op when audio is already playing. | 79 // no-op when audio is already playing. |
| 77 if (self.playingAudio || !self.readyForPlayback) | 80 if (self.playingAudio || !self.readyForPlayback) |
| 78 return; | 81 return; |
| 79 // Create the AVAudioPlayer and initiate playback. | 82 // Create the AVAudioPlayer and initiate playback. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 [_player setDelegate:nil]; | 125 [_player setDelegate:nil]; |
| 123 _player.reset(); | 126 _player.reset(); |
| 124 if (sendNotification) { | 127 if (sendNotification) { |
| 125 [[NSNotificationCenter defaultCenter] | 128 [[NSNotificationCenter defaultCenter] |
| 126 postNotificationName:kTTSDidStopPlayingNotification | 129 postNotificationName:kTTSDidStopPlayingNotification |
| 127 object:self]; | 130 object:self]; |
| 128 } | 131 } |
| 129 } | 132 } |
| 130 | 133 |
| 131 @end | 134 @end |
| OLD | NEW |