回答編集履歴

2

Page Visibility API

2016/05/12 22:07

投稿

think49
think49

スコア18162

test CHANGED
@@ -40,6 +40,62 @@
40
40
 
41
41
 
42
42
 
43
+ ### Page Visibility API
44
+
45
+
46
+
47
+ iOS Safari では `window.onfocus` が動作しないようなので Page Visibility API を併用します。
48
+
49
+
50
+
51
+ - [Page Visibility - Can I use...](http://caniuse.com/#feat=pagevisibility)
52
+
53
+
54
+
55
+ ```javascript
56
+
57
+ 'use strict';
58
+
59
+ function handleVisibilitychange (event) {
60
+
61
+ var doc = event.target;
62
+
63
+
64
+
65
+ if (doc.visibilityState === 'visible') {
66
+
67
+ alert(event.target.visibilityState);
68
+
69
+ doc.getElementById('play-sound').play();
70
+
71
+ }
72
+
73
+ }
74
+
75
+
76
+
77
+ function handleFocus (event) {
78
+
79
+ alert(event.type);
80
+
81
+ event.currentTarget.document.getElementById('play-sound').play();
82
+
83
+ }
84
+
85
+
86
+
87
+ if ('visibilityState' in document) {
88
+
89
+ document.addEventListener('visibilitychange', handleVisibilitychange, false);
90
+
91
+ } else if ('onfocus' in this) {
92
+
93
+ addEventListener('focus', handleFocus, false);
94
+
95
+ }
96
+
97
+ ```
98
+
43
99
  Re: kouji20 さん
44
100
 
45
101
 

1

iOS Safari で Audio API を利用する

2016/05/12 22:07

投稿

think49
think49

スコア18162

test CHANGED
@@ -1,3 +1,5 @@
1
+ ### focus イベントハンドラ
2
+
1
3
  `window.onfocus` はいかがでしょうか。
2
4
 
3
5
  (`addEventListener`を使うと上書きされる心配がなくなります。)
@@ -10,4 +12,34 @@
10
12
 
11
13
 
12
14
 
15
+ ### audio要素の API サポート状況
16
+
17
+
18
+
19
+ iPhone で動作しないとのことですが、`onfocus` を確認する前にaudio要素のAPIサポート状況を確認する必要があると思います。
20
+
21
+ `HTMLMediaElement#play` が存在するのか。
22
+
23
+
24
+
25
+ ```JavaScript
26
+
27
+ alert('play' in document.getElementById("play-sound"));
28
+
29
+ ```
30
+
31
+
32
+
33
+ iOS Safari で再生可能な音楽フォーマットを使っているのか。
34
+
35
+
36
+
37
+ - [HTMLMediaElement - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement#Browser_compatibility)
38
+
39
+ - [Media formats supported by the HTML audio and video elements - HTML | MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats#Browser_compatibility)
40
+
41
+
42
+
13
43
  Re: kouji20 さん
44
+
45
+