MCPcopy Create free account
hub / github.com/bajrangCoder/setu / play

Method play

src/components/audio_player.rs:138–180  ·  view source on GitHub ↗
(&mut self, cx: &mut Context<Self>)

Source from the content-addressed store, hash-verified

136 self.error_message = None;
137
138 if self.has_reached_end() {
139 self.current_position = Duration::ZERO;
140 }
141
142 self.backend = None;
143 if !self.ensure_backend() {
144 cx.notify();
145 return;
146 }
147
148 let cursor = Cursor::new(self.audio_bytes.clone());
149 let Ok(source) = rodio::Decoder::try_from(cursor) else {
150 self.error_message = Some("Failed to decode audio format".to_string());
151 self.playback_state = PlaybackState::Error;
152 cx.notify();
153 return;
154 };
155
156 let start_position = self.current_position;
157
158 if let Some(ref backend) = self.backend
159 && let Ok(backend) = backend.lock()
160 {
161 backend.player.append(source);
162
163 if start_position > Duration::ZERO {
164 if let Err(error) = backend.player.try_seek(start_position) {
165 log::warn!("Failed to seek audio on play: {error:?}");
166 self.error_message =
167 Some("Seeking is not supported for this audio".to_string());
168 self.current_position = Duration::ZERO;
169 } else {
170 self.current_position = self.clamp_to_duration(backend.player.get_pos());
171 }
172 }
173 }
174
175 self.playback_state = PlaybackState::Playing;
176 cx.notify();
177 }
178
179 fn pause(&mut self, cx: &mut Context<Self>) {
180 if let Some(ref backend) = self.backend
181 && let Ok(backend) = backend.lock()
182 {
183 backend.player.pause();

Callers 2

toggle_playbackMethod · 0.80
resumeMethod · 0.80

Calls 3

has_reached_endMethod · 0.80
ensure_backendMethod · 0.80
clamp_to_durationMethod · 0.80

Tested by

no test coverage detected