Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/chrome/browser/ui/voice/text_to_speech_player.h" | |
| 6 | |
| 7 #import <AVFoundation/AVFoundation.h> | |
| 8 #import <UIKit/UIKit.h> | |
| 9 | |
| 10 #import "base/mac/scoped_nsobject.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" | |
| 13 | |
|
sdefresne
2016/10/25 13:20:30
Should we convert the file to ARC?
rohitrao (ping after 24h)
2016/10/25 15:23:12
I'll leave that to stk to decide =)
| |
| 14 @interface TextToSpeechPlayer ()<AVAudioPlayerDelegate> { | |
| 15 // The audio data to be played. | |
| 16 base::scoped_nsobject<NSData> _audioData; | |
| 17 // The AVAudioPlayer playing TTS audio data. | |
| 18 base::scoped_nsobject<AVAudioPlayer> _player; | |
| 19 // Whether playback has finished. | |
| 20 BOOL _playbackFinished; | |
| 21 } | |
| 22 | |
| 23 // Cancels TTS audio playback, and sends a kTTSDidStopPlayingNotification | |
| 24 // notification if |sendNotification| is YES. | |
| 25 - (void)cancelPlaybackAndSendNotification:(BOOL)sendNotification; | |
| 26 | |
| 27 @end | |
| 28 | |
| 29 @implementation TextToSpeechPlayer | |
| 30 | |
| 31 - (instancetype)init { | |
| 32 if ((self = [super init])) { | |
| 33 SEL handler = @selector(cancelPlayback); | |
| 34 NSString* notificationName = UIApplicationDidEnterBackgroundNotification; | |
| 35 id sender = [UIApplication sharedApplication]; | |
| 36 [[NSNotificationCenter defaultCenter] addObserver:self | |
| 37 selector:handler | |
| 38 name:notificationName | |
| 39 object:sender]; | |
| 40 } | |
| 41 return self; | |
| 42 } | |
| 43 | |
| 44 - (void)dealloc { | |
| 45 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 46 [self cancelPlayback]; | |
| 47 [super dealloc]; | |
| 48 } | |
| 49 | |
| 50 #pragma mark - Accessors | |
| 51 | |
| 52 - (BOOL)isReadyForPlayback { | |
| 53 return [_audioData length] > 0; | |
| 54 } | |
| 55 | |
| 56 - (BOOL)isPlayingAudio { | |
| 57 return [_player isPlaying]; | |
| 58 } | |
| 59 | |
| 60 - (AVAudioPlayer*)player { | |
| 61 return _player; | |
| 62 } | |
| 63 | |
| 64 #pragma mark - Public | |
| 65 | |
| 66 - (void)prepareToPlayAudioData:(NSData*)audioData { | |
| 67 if (self.playingAudio) | |
| 68 [self cancelPlayback]; | |
| 69 _audioData.reset([audioData retain]); | |
| 70 [[NSNotificationCenter defaultCenter] | |
| 71 postNotificationName:kTTSAudioReadyForPlaybackNotification | |
| 72 object:self]; | |
| 73 } | |
| 74 | |
| 75 - (void)beginPlayback { | |
| 76 // no-op when audio is already playing. | |
| 77 if (self.playingAudio || !self.readyForPlayback) | |
| 78 return; | |
| 79 // Create the AVAudioPlayer and initiate playback. | |
| 80 _player.reset([[AVAudioPlayer alloc] initWithData:_audioData error:nil]); | |
| 81 [_player setMeteringEnabled:YES]; | |
| 82 [_player setDelegate:self]; | |
| 83 [_player setNumberOfLoops:0]; | |
| 84 [_player setVolume:1.0]; | |
| 85 if ([_player prepareToPlay] && [_player play]) { | |
| 86 [[NSNotificationCenter defaultCenter] | |
| 87 postNotificationName:kTTSWillStartPlayingNotification | |
| 88 object:self]; | |
| 89 } else { | |
| 90 _player.reset(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 - (void)cancelPlayback { | |
| 95 [self cancelPlaybackAndSendNotification:[_player isPlaying]]; | |
| 96 } | |
| 97 | |
| 98 #pragma mark - AVAudioPlayerDelegate | |
| 99 | |
| 100 - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player | |
| 101 error:(NSError*)error { | |
| 102 [self cancelPlayback]; | |
| 103 } | |
| 104 | |
| 105 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player | |
| 106 successfully:(BOOL)flag { | |
| 107 [self cancelPlaybackAndSendNotification:flag]; | |
| 108 } | |
| 109 | |
| 110 - (void)audioPlayerBeginInterruption:(AVAudioPlayer*)player { | |
| 111 [self cancelPlayback]; | |
| 112 } | |
| 113 | |
| 114 #pragma mark - | |
| 115 | |
| 116 - (void)cancelPlaybackAndSendNotification:(BOOL)sendNotification { | |
| 117 if (_playbackFinished) | |
| 118 return; | |
| 119 _playbackFinished = YES; | |
| 120 [_player stop]; | |
| 121 _audioData.reset(); | |
| 122 [_player setDelegate:nil]; | |
| 123 _player.reset(); | |
| 124 if (sendNotification) { | |
| 125 [[NSNotificationCenter defaultCenter] | |
| 126 postNotificationName:kTTSDidStopPlayingNotification | |
| 127 object:self]; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 @end | |
| OLD | NEW |