fix: audio issue when sending app to background on Android and other fixes

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2023-05-11 16:25:57 -03:00
parent d2e38772b9
commit 3038ceeb82
No known key found for this signature in database
GPG Key ID: 59EB921E0706B48F
4 changed files with 38 additions and 14 deletions

View File

@ -173,6 +173,7 @@
reached-max-duration? (atom false) reached-max-duration? (atom false)
touch-timestamp (atom nil) touch-timestamp (atom nil)
disabled? (atom false) disabled? (atom false)
app-state-listener (atom nil)
rec-options rec-options
(merge (merge
audio/default-recorder-options audio/default-recorder-options
@ -512,7 +513,13 @@
(on-init reset-recorder)) (on-init reset-recorder))
(when audio-file (when audio-file
(let [filename (last (string/split audio-file "/"))] (let [filename (last (string/split audio-file "/"))]
(reload-player filename))))) (reload-player filename)))
(reset! app-state-listener
(.addEventListener rn/app-state
"change"
#(when (= % "background")
(reset! playing-audio? false))))
#(.remove @app-state-listener)))
[rn/view [rn/view
{:style style/bar-container {:style style/bar-container
:pointer-events :box-none} :pointer-events :box-none}

View File

@ -130,6 +130,11 @@
(when (and player (.-canPlay ^js player)) (when (and player (.-canPlay ^js player))
(.-currentTime ^js player))) (.-currentTime ^js player)))
(defn set-player-wake-lock
[player wake-lock?]
(when player
(set! (.-wakeLock player) wake-lock?)))
(defn toggle-playpause-player (defn toggle-playpause-player
[player on-play on-pause on-error] [player on-play on-pause on-error]
(when (and player (.-canPlay ^js player)) (when (and player (.-canPlay ^js player))

View File

@ -20,7 +20,8 @@
(h/describe "audio message" (h/describe "audio message"
(h/before-each (h/before-each
#(setup-subs {:mediaserver/port 1000})) #(setup-subs {:mediaserver/port 1000
:app-state "active"}))
(h/test "renders correctly" (h/test "renders correctly"
(h/render [audio-message/audio-message message context]) (h/render [audio-message/audio-message message context])

View File

@ -59,7 +59,7 @@
audio-url audio-url
(fn [base64-data] (fn [base64-data]
(let [player (audio/new-player (let [player (audio/new-player
(str "data:audio/acc;base64," base64-data) (str "data:audio/aac;base64," base64-data)
{:autoDestroy false {:autoDestroy false
:continuesToPlayInBackground false} :continuesToPlayInBackground false}
(fn [] (fn []
@ -68,6 +68,7 @@
(when (and @progress-timer (= @current-player-key player-key)) (when (and @progress-timer (= @current-player-key player-key))
(js/clearInterval @progress-timer) (js/clearInterval @progress-timer)
(reset! progress-timer nil))))] (reset! progress-timer nil))))]
(audio/set-player-wake-lock player true)
(swap! active-players assoc player-key player) (swap! active-players assoc player-key player)
(audio/prepare-player (audio/prepare-player
player player
@ -90,7 +91,8 @@
(when-not playing? (when-not playing?
(reset! current-player-key player-key)) (reset! current-player-key player-key))
(if (and player (if (and player
(= (@audio-uris player-key) audio-uri)) (= (@audio-uris player-key) audio-uri)
(not= (audio/get-state player) audio/IDLE))
(audio/toggle-playpause-player (audio/toggle-playpause-player
player player
(fn [] (fn []
@ -121,7 +123,7 @@
(reset! seeking-audio? false) (reset! seeking-audio? false)
(if (> @progress 0) (if (> @progress 0)
(let [seek-time (* audio-duration-ms @progress) (let [seek-time (* audio-duration-ms @progress)
checked-seek-time (min audio-duration-ms seek-time)] checked-seek-time (if (<= @progress 1) seek-time @progress)]
(seek-player (seek-player
player-key player-key
player-state player-state
@ -141,13 +143,20 @@
(if (or @seeking-audio? (#{:playing :seeking} @player-state)) (if (or @seeking-audio? (#{:playing :seeking} @player-state))
(if (<= @progress 1) (* duration @progress) @progress) (if (<= @progress 1) (* duration @progress) @progress)
duration) duration)
1000)] 1000)
paused? (= (audio/get-state player) audio/PAUSED)
app-state (rf/sub [:app-state])]
(rn/use-effect (fn [] #(destroy-player player-key))) (rn/use-effect (fn [] #(destroy-player player-key)))
(rn/use-effect (rn/use-effect
(fn [] (fn []
(when (and (some? @current-player-key) (when (or
(not= @current-player-key player-key) (and (some? @current-player-key)
(= @player-state :playing)) (not= @current-player-key player-key)
(= @player-state :playing))
(and platform/ios?
(= @current-player-key player-key)
(not= app-state "active")
(= @player-state :playing)))
(play-pause-player {:player-key player-key (play-pause-player {:player-key player-key
:player-state player-state :player-state player-state
:progress progress :progress progress
@ -155,7 +164,7 @@
:audio-duration-ms duration :audio-duration-ms duration
:seeking-audio? seeking-audio? :seeking-audio? seeking-audio?
:user-interaction? false}))) :user-interaction? false})))
[@current-player-key]) [@current-player-key app-state])
(if (= @player-state :error) (if (= @player-state :error)
[quo/text [quo/text
{:style style/error-label {:style style/error-label
@ -177,10 +186,12 @@
:user-interaction? true}) :user-interaction? true})
:style (style/play-pause-container)} :style (style/play-pause-container)}
[quo/icon [quo/icon
(case @player-state (cond
:preparing :i/loading (= @player-state :preparing)
:playing :i/pause-audio :i/loading
:i/play-audio) (and (= @player-state :playing) (not paused?))
:i/pause-audio
:else :i/play-audio)
{:size 20 {:size 20
:color colors/white}]] :color colors/white}]]
[:f> quo/soundtrack [:f> quo/soundtrack