【问题标题】:Save body of a streaming future object to disk将流式未来对象的主体保存到磁盘
【发布时间】:2021-09-26 06:53:03
【问题描述】:

我正在使用来自rusotoPollyClient,我正在尝试处理我认为来自方法synthesize_speech() 的某种类型的Future 响应。

这是我所拥有的按原样编译和运行的 sn-p:

extern crate rusoto_core;
extern crate rusoto_polly;

use std::default::Default;
use tokio::{io};

use rusoto_core::credential::{ProfileProvider, ProvideAwsCredentials};
use rusoto_core::Region;
use rusoto_core::ByteStream;
use rusoto_polly::{PollyClient, SynthesizeSpeechInput, Polly};
use std::fs::File;
use futures::AsyncBufRead;
use std::pin::Pin;
use futures::Future;

fn main() {

    let region = Region::UsEast1;
    let polly_client = PollyClient::new(region); // using default credenetials

    let speech_input = SynthesizeSpeechInput{
        engine: Option::from(String::from("neural")),
        language_code: None,
        lexicon_names: None,
        output_format: "mp3".to_string(),
        sample_rate: None,
        speech_mark_types: None,
        text: String::from("hello world!"),
        text_type: None,
        voice_id: String::from("Amy")
    };
    let mut response = polly_client.synthesize_speech(speech_input);

    println!("Now how to we handle this respones object...");

}

我的目标是在响应中保存应该是 MP3 二进制数据的内容。我没有分享任何编译器错误,因为我经历了很多不同的错误。我是 Rust 的新手,在处理非异步代码的过程中,这是我第一次遇到它。

我是否需要将 synthesize_speech() 方法包装在一个闭包中,以便我可以将它包装在某种 await 块中?

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: rust closures rust-tokio amazon-polly rusoto


    【解决方案1】:

    我意识到这只是一个玩具示例,但是在从 rusoto documentation 模仿 main() 上的一些 tokio async 模式之后,能够让它工作:

    extern crate rusoto_core;
    extern crate rusoto_polly;
    
    // use rusoto_core::credential::{ProfileProvider, ProvideAwsCredentials};
    use rusoto_core::{Region};
    use rusoto_polly::{PollyClient, SynthesizeSpeechInput, Polly};
    use std::fs;
    
    #[tokio::main] // https://docs.rs/tokio/0.2.2/tokio/attr.main.html
    async fn main() {
    
        let region = Region::UsEast1;
        let polly_client = PollyClient::new(region); // using default credentials
    
        let speech_input = SynthesizeSpeechInput{
            engine: Option::from(String::from("neural")),
            language_code: None,
            lexicon_names: None,
            output_format: "mp3".to_string(),
            sample_rate: None,
            speech_mark_types: None,
            text: String::from("Hello world, from Rust!"),
            text_type: None,
            voice_id: String::from("Amy")
        };
    
        match polly_client.synthesize_speech(speech_input).await {
            Ok(output) => match output.audio_stream{
                Some(audio_stream) => {
                    println!("{}", audio_stream.len());
                    fs::write("mp3/rstts.mp3", audio_stream).expect("Unable to write file");
                },
                None => println!("audio stream not found"),
            },
            Err(error) => {
                println!("error: {:?}", error);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多