MCPcopy Create free account
hub / github.com/Moosync/Moosync / initialize

Method initialize

src-tauri/rodio-player/src/lib.rs:112–222  ·  view source on GitHub ↗
(events_tx: Sender<PlayerEvents>, cache_dir: PathBuf)

Source from the content-addressed store, hash-verified

110 }
111
112 fn initialize(events_tx: Sender<PlayerEvents>, cache_dir: PathBuf) -> Sender<RodioCommand> {
113 let (tx, rx) = channel::<RodioCommand>();
114 let ret = tx.clone();
115
116 thread::spawn(move || {
117 let (_stream, stream_handle) = OutputStream::try_default().unwrap();
118 let sink = Arc::new(rodio::Sink::try_new(&stream_handle).unwrap());
119
120 let runtime = tokio::runtime::Builder::new_multi_thread()
121 .enable_all()
122 .build()
123 .unwrap();
124
125 let events_tx = events_tx.clone();
126 runtime.block_on(async move {
127 let last_src = Arc::new(Mutex::new(None));
128 while let Ok(command) = rx.recv() {
129 let sink = sink.clone();
130
131 match command {
132 RodioCommand::SetSrc(src) => {
133 let last_src = last_src.clone();
134 {
135 let mut last_src = last_src.lock().unwrap();
136 *last_src = Some(src.clone());
137 }
138
139 sink.clear();
140 Self::send_event(events_tx.clone(), PlayerEvents::TimeUpdate(0f64));
141 Self::send_event(events_tx.clone(), PlayerEvents::Loading);
142
143 if let Err(err) =
144 Self::set_src(cache_dir.clone(), src.clone(), &sink).await
145 {
146 error!("Failed to set src: {:?}", err);
147 Self::send_event(events_tx.clone(), PlayerEvents::Error(err))
148 } else {
149 debug!("Set src");
150 let src_clone = src.clone();
151
152 let events_tx = events_tx.clone();
153 let sink = sink.clone();
154
155 // Send ended event only if song hasn't changed yet
156 thread::spawn(move || {
157 sink.sleep_until_end();
158 let last_src = last_src.clone();
159 let last_src = last_src.lock().unwrap();
160 if let Some(last_src) = last_src.clone() {
161 info!("last src={}, current src={}", last_src, src_clone);
162 if last_src == src_clone {
163 Self::send_event(
164 events_tx.clone(),
165 PlayerEvents::Ended,
166 );
167 }
168 }
169 });

Callers

nothing calls this directly

Calls 7

cloneMethod · 0.80
clearMethod · 0.80
sendMethod · 0.80
playMethod · 0.45
pauseMethod · 0.45
stopMethod · 0.45
set_volumeMethod · 0.45

Tested by

no test coverage detected