Lagrange [work/v1.6]

Media: Pause other players when a new one starts

=> 86c28576cca47e5a7e23dc9116604880d684665b

diff --git a/src/media.c b/src/media.c
index c0d6d3de..dcda0664 100644
--- a/src/media.c
+++ b/src/media.c
@@ -24,6 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 #include "gmdocument.h"
 #include "ui/window.h"
 #include "audio/player.h"
+#include "app.h"
 
 #include 
 #include 
@@ -214,8 +215,9 @@ void setData_Media(iMedia *d, iGmLinkId linkId, const iString *mime, const iBloc
                 updateSourceData_Player(audio->player, NULL, NULL, complete_PlayerUpdate);
             }
             pushBack_PtrArray(&d->audio, audio);
-            /* TEST: Start playing right away. */
+            /* Start playing right away. */
             start_Player(audio->player);
+            postCommandf_App("media.player.started player:%p", audio->player);
         }
     }
 }
@@ -231,6 +233,10 @@ iMediaId findLinkImage_Media(const iMedia *d, iGmLinkId linkId) {
     return 0;
 }
 
+size_t numAudio_Media(const iMedia *d) {
+    return size_PtrArray(&d->audio);
+}
+
 iMediaId findLinkAudio_Media(const iMedia *d, iGmLinkId linkId) {
     /* TODO: use a hash */
     iConstForEach(PtrArray, i, &d->audio) {
diff --git a/src/media.h b/src/media.h
index c19ad8ae..9db6659d 100644
--- a/src/media.h
+++ b/src/media.h
@@ -60,6 +60,7 @@ iMediaId        findLinkImage_Media (const iMedia *, uint16_t linkId);
 iBool           imageInfo_Media     (const iMedia *, iMediaId imageId, iGmImageInfo *info_out);
 SDL_Texture *   imageTexture_Media  (const iMedia *, iMediaId imageId);
 
+size_t          numAudio_Media      (const iMedia *);
 iMediaId        findLinkAudio_Media (const iMedia *, uint16_t linkId);
 iBool           audioInfo_Media     (const iMedia *, iMediaId audioId, iGmAudioInfo *info_out);
 iPlayer *       audioPlayer_Media   (const iMedia *, iMediaId audioId);
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index b977e4d9..573cd3e3 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -1421,6 +1421,18 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
     else if (equal_Command(cmd, "media.updated") || equal_Command(cmd, "media.finished")) {
         return handleMediaCommand_DocumentWidget_(d, cmd);
     }
+    else if (equal_Command(cmd, "media.player.started")) {
+        /* When one media player starts, pause the others that may be playing. */
+        const iPlayer *startedPlr = pointerLabel_Command(cmd, "player");
+        const iMedia * media  = media_GmDocument(d->doc);
+        const size_t   num    = numAudio_Media(media);
+        for (size_t id = 1; id <= num; id++) {
+            iPlayer *plr = audioPlayer_Media(media, id);
+            if (plr != startedPlr) {
+                setPaused_Player(plr, iTrue);
+            }
+        }
+    }
     else if (equal_Command(cmd, "document.stop") && document_App() == d) {
         if (d->request) {
             postCommandf_App(
@@ -2064,6 +2076,8 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e
                                     if (!isFinished_GmRequest(req->req)) {
                                         cancel_GmRequest(req->req);
                                         removeMediaRequest_DocumentWidget_(d, linkId);
+                                        /* Note: Some of the audio IDs have changed now, layout must
+                                           be redone. */
                                     }
                                 }
                                 redoLayout_GmDocument(d->doc);
Proxy Information
Original URL
gemini://git.skyjake.fi/lagrange/work%2Fv1.6/cdiff/86c28576cca47e5a7e23dc9116604880d684665b
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
57.525989 milliseconds
Gemini-to-HTML Time
0.283655 milliseconds

This content has been proxied by September (ba2dc).