Skip to content

Commit ec54dba

Browse files
committed
Expand example (still contains hardcoded paths!)
1 parent 0f72497 commit ec54dba

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

examples/async.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use winrt::*;
1616
use winrt::windows::foundation::*;
1717
use winrt::windows::devices::enumeration::*;
1818
use winrt::windows::devices::midi::*;
19+
use winrt::windows::media::*;
20+
use winrt::windows::storage::StorageFile;
1921

2022
fn main() {
2123
let rt = RuntimeContext::init();
@@ -78,4 +80,48 @@ async fn run() {
7880
let device_name = current.get_name().unwrap();
7981
println!("Device Name ({}): {}", i, device_name);
8082
}
83+
84+
print!("Transcoding media: ");
85+
let source = StorageFile::get_file_from_path_async(&*FastHString::new("D:\\Desktop\\test-transcode\\test.mp3")).unwrap().await.expect("get_file_from_path_async failed").unwrap();
86+
let dest = StorageFile::get_file_from_path_async(&*FastHString::new("D:\\Desktop\\test-transcode\\test.flac")).unwrap().await.expect("get_file_from_path_async failed").unwrap();
87+
let profile = mediaproperties::MediaEncodingProfile::create_flac(mediaproperties::AudioEncodingQuality::Medium).unwrap().unwrap();
88+
let transcoder = transcoding::MediaTranscoder::new();
89+
let prepared = transcoder.prepare_file_transcode_async(&source, &dest, &profile).unwrap().await.unwrap().unwrap();
90+
assert!(prepared.get_can_transcode().unwrap());
91+
92+
let mut interval = Interval::new(Duration::from_millis(100));
93+
let async_op = prepared.transcode_async().unwrap();
94+
// TODO: there should be a way to await the created AsyncActionProgressHandler (and any other delegate) as an async stream
95+
async_op.set_progress(&AsyncActionProgressHandler::new(|_info, progress| {
96+
print_progress(progress);
97+
Ok(())
98+
})).unwrap();
99+
let mut async_op = async_op.fuse();
100+
101+
let work = async {
102+
let mut result = None;
103+
loop {
104+
select! {
105+
_ = interval.next() => {
106+
use std::io::Write;
107+
print!(".");
108+
std::io::stdout().flush().unwrap();
109+
},
110+
res = async_op => {
111+
result = Some(res);
112+
println!("");
113+
break;
114+
}
115+
};
116+
}
117+
result.unwrap()
118+
};
119+
120+
work.await
121+
}
122+
123+
fn print_progress(progress: f64) {
124+
use std::io::Write;
125+
print!("{:.0}%", progress);
126+
std::io::stdout().flush().unwrap();
81127
}

0 commit comments

Comments
 (0)