summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/assets/www/media_session_dom1.html
blob: 8fa95844284224459c8eafe7d1fb0a71146d12c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<html>
  <head>
    <title>MediaSessionDOMTest1</title>
  </head>
  <body>
    <script>
      function updatePositionState(event) {
        if (event.target != active) {
          return;
        }
        navigator.mediaSession.setPositionState({
          duration: parseFloat(event.target.duration),
          position: parseFloat(event.target.currentTime),
          playbackRate: 1,
        });
      }

      function updateMetadata() {
        navigator.mediaSession.metadata = active.metadata;
      }

      function getTrack(offset) {
        console.log("" + active.id + " " + offset);
        const nextId = Math.min(
          tracks.length - 1,
          Math.max(0, parseInt(active.id) + offset)
        );
        return tracks[nextId];
      }

      navigator.mediaSession.setActionHandler("play", async () => {
        updateMetadata();
        await active.play();
      });

      navigator.mediaSession.setActionHandler("pause", () => {
        active.pause();
      });

      navigator.mediaSession.setActionHandler("previoustrack", () => {
        active = getTrack(-1);
      });

      navigator.mediaSession.setActionHandler("nexttrack", () => {
        active = getTrack(1);
      });

      const audio1 = document.createElement("audio");
      audio1.src = "audio/owl.mp3";
      audio1.addEventListener("timeupdate", updatePositionState);
      audio1.metadata = new window.MediaMetadata({
        title: "hoot",
        artist: "owl",
        album: "hoots",
        artwork: [
          {
            src: "images/test.gif",
            type: "image/gif",
            sizes: "265x199",
          },
        ],
      });
      audio1.id = 0;

      const audio2 = document.createElement("audio");
      audio2.src = "audio/owl.mp3";
      audio2.addEventListener("timeupdate", updatePositionState);
      audio2.metadata = new window.MediaMetadata({
        title: "hoot2",
        artist: "stillowl",
        album: "dahoots",
        artwork: [
          {
            src: "images/test.gif",
            type: "image/gif",
            sizes: "265x199",
          },
        ],
      });
      audio2.id = 1;

      const audio3 = document.createElement("audio");
      audio3.src = "audio/owl.mp3";
      audio3.addEventListener("timeupdate", updatePositionState);
      audio3.metadata = new window.MediaMetadata({
        title: "hoot3",
        artist: "immaowl",
        album: "mahoots",
        artwork: [
          {
            src: "images/test.gif",
            type: "image/gif",
            sizes: "265x199",
          },
        ],
      });
      audio3.id = 2;

      const tracks = [audio1, audio2, audio3];
      let active = audio1;

      window.onload = async () => {
        active = getTrack(0);
        updateMetadata();
        await active.play();
      };
    </script>
  </body>
</html>