iOS 예제 - Amazon Polly

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

iOS 예제

다음 예제에서는 Amazon Polly용 iOS SDK를 사용하여 음성 목록에서 선택한 음성으로 지정된 텍스트를 읽습니다.

여기에 표시된 코드는 주요 작업을 다루지만 오류는 처리하지 않습니다. 전체 코드는 AWS Mobile SDK for iOS Amazon Polly 데모를 참조하세요.

초기화

// Region of Amazon Polly. let AwsRegion = AWSRegionType.usEast1 // Cognito pool ID. Pool needs to be unauthenticated pool with // Amazon Polly permissions. let CognitoIdentityPoolId = "YourCognitoIdentityPoolId" // Initialize the Amazon Cognito credentials provider. let credentialProvider = AWSCognitoCredentialsProvider(regionType: AwsRegion, identityPoolId: CognitoIdentityPoolId) // Create an audio player var audioPlayer = AVPlayer()
사용 가능한 음성 목록 가져오기

// Use the configuration as default AWSServiceManager.default().defaultServiceConfiguration = configuration // Get all the voices (no parameters specified in input) from Amazon Polly // This creates an async task. let task = AWSPolly.default().describeVoices(AWSPollyDescribeVoicesInput()) // When the request is done, asynchronously do the following block // (we ignore all the errors, but in a real-world scenario they need // to be handled) task.continue(successBlock: { (awsTask: AWSTask) -> Any? in // awsTask.result is an instance of AWSPollyDescribeVoicesOutput in // case of the "describeVoices" method let voices = (awsTask.result! as AWSPollyDescribeVoicesOutput).voices return nil })
스피치 합성

// First, Amazon Polly requires an input, which we need to prepare. // Again, we ignore the errors, however this should be handled in // real applications. Here we are using the URL Builder Request, // since in order to make the synthesis quicker we will pass the // presigned URL to the system audio player. let input = AWSPollySynthesizeSpeechURLBuilderRequest() // Text to synthesize input.text = "Sample text" // We expect the output in MP3 format input.outputFormat = AWSPollyOutputFormat.mp3 // Choose the voice ID input.voiceId = AWSPollyVoiceId.joanna // Create an task to synthesize speech using the given synthesis input let builder = AWSPollySynthesizeSpeechURLBuilder.default().getPreSignedURL(input) // Request the URL for synthesis result builder.continueOnSuccessWith(block: { (awsTask: AWSTask<NSURL>) -> Any? in // The result of getPresignedURL task is NSURL. // Again, we ignore the errors in the example. let url = awsTask.result! // Try playing the data using the system AVAudioPlayer self.audioPlayer.replaceCurrentItem(with: AVPlayerItem(url: url as URL)) self.audioPlayer.play() return nil })