Android 예제 - Amazon Polly

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

Android 예제

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

여기에 표시된 코드는 주요 작업을 다루지만 오류를 처리하지는 않습니다. 전체 코드는 단원을 참조하십시오.안드로이드용 AWS SDK Amazon Polly 데모를 선택합니다.


// Cognito pool ID. Pool needs to be unauthenticated pool with // Amazon Polly permissions. String COGNITO_POOL_ID = "YourCognitoIdentityPoolId"; // Region of Amazon Polly. Regions MY_REGION = Regions.US_EAST_1;   // Initialize the Amazon Cognito credentials provider. CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(         getApplicationContext(),         COGNITO_POOL_ID,         MY_REGION ); // Create a client that supports generation of presigned URLs. AmazonPollyPresigningClient client = new AmazonPollyPresigningClient(credentialsProvider);

사용 가능한 음성 목록 가져오기

// Create describe voices request. DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest(); // Synchronously ask Amazon Polly to describe available TTS voices. DescribeVoicesResult describeVoicesResult = client.describeVoices(describeVoicesRequest); List<Voice> voices = describeVoicesResult.getVoices();

오디오 스트림에 대한 URL 가져 오기

// Create speech synthesis request. SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest =         new SynthesizeSpeechPresignRequest()         // Set the text to synthesize.         .withText("Hello world!")         // Select voice for synthesis.         .withVoiceId(voices.get(0).getId()) // "Joanna"         // Set format to MP3.         .withOutputFormat(OutputFormat.Mp3); // Get the presigned URL for synthesized speech audio stream. URL presignedSynthesizeSpeechUrl =         client.getPresignedSynthesizeSpeechUrl(synthesizeSpeechPresignRequest);

음성 합성

// Use MediaPlayer: // Create a media player to play the synthesized audio stream. MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try {     // Set media player's data source to previously obtained URL.     mediaPlayer.setDataSource(presignedSynthesizeSpeechUrl.toString()); } catch (IOException e) {     Log.e(TAG, "Unable to set data source for the media player! " + e.getMessage()); } // Prepare the MediaPlayer asynchronously (since the data source is a network stream). mediaPlayer.prepareAsync(); // Set the callback to start the MediaPlayer when it's prepared. mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {     @Override     public void onPrepared(MediaPlayer mp) {         mp.start();     } }); // Set the callback to release the MediaPlayer after playback is completed. mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } });