回答編集履歴

5

プライベートな static メンバ変数の使い道を追加

2021/10/17 16:52

投稿

blendthink
blendthink

スコア25

test CHANGED
@@ -74,13 +74,17 @@
74
74
 
75
75
 
76
76
 
77
- ちなみに、プライベートな static メンバ変数の使い道は以下のように SoundPlayer の
77
+ ちなみに、プライベートな static メンバ変数の使い道はSoundPlayer のインスタンスを生成せずに SoundPool のインスタンスを使いたい時などです。
78
78
 
79
79
 
80
80
 
81
- ```
81
+ 以下がその例です。(今回は分かりやすいように hitSound と overSound を除外して固定値にしています)
82
82
 
83
83
 
84
+
85
+ ```java
86
+
87
+ package jp.codeforfun.catchtheball;
84
88
 
85
89
 
86
90
 

4

プライベートな static メンバ変数の使い道を追加

2021/10/17 16:52

投稿

blendthink
blendthink

スコア25

test CHANGED
@@ -71,3 +71,61 @@
71
71
  }
72
72
 
73
73
  ```
74
+
75
+
76
+
77
+ ちなみに、プライベートな static メンバ変数の使い道は以下のように SoundPlayer の
78
+
79
+
80
+
81
+ ```
82
+
83
+
84
+
85
+
86
+
87
+ import android.media.AudioManager;
88
+
89
+ import android.media.SoundPool;
90
+
91
+
92
+
93
+ public class SoundPlayer {
94
+
95
+
96
+
97
+ private static SoundPool soundPool;
98
+
99
+
100
+
101
+ private static SoundPool getSoundPool() {
102
+
103
+ if (soundPool == null) {
104
+
105
+ soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
106
+
107
+ }
108
+
109
+ return soundPool;
110
+
111
+ }
112
+
113
+
114
+
115
+ public static void playHitSound() {
116
+
117
+ getSoundPool().play(1, 1.0f, 1.0f, 1, 0, 1.0f);
118
+
119
+ }
120
+
121
+
122
+
123
+ public static void playOverSound() {
124
+
125
+ getSoundPool().play(2, 1.0f, 1.0f, 1, 0, 1.0f);
126
+
127
+ }
128
+
129
+ }
130
+
131
+ ```

3

訂正の訂正です。。深夜なので頭働いてないかもです。。

2021/10/17 16:49

投稿

blendthink
blendthink

スコア25

test CHANGED
@@ -2,6 +2,72 @@
2
2
 
3
3
 
4
4
 
5
- ~~リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。~~
5
+ リンク先を拝見いたしましたが、そもそも static 変数である必要がありません
6
6
 
7
+
8
+
9
+ 以下のようにできます。
10
+
11
+
12
+
13
+ ```java
14
+
15
+ package jp.codeforfun.catchtheball;
16
+
17
+
18
+
19
+ import android.content.Context;
20
+
21
+ import android.media.AudioAttributes;
22
+
23
+ import android.media.AudioManager;
24
+
25
+ import android.media.SoundPool;
26
+
27
+
28
+
29
+ public class SoundPlayer {
30
+
31
+
32
+
33
+ private final SoundPool soundPool;
34
+
35
+ private final int hitSound;
36
+
37
+ private final int overSound;
38
+
39
+
40
+
41
+ public SoundPlayer(Context context) {
42
+
43
+
44
+
45
+ soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
46
+
47
+
48
+
49
+ hitSound = soundPool.load(context, R.raw.hit, 1);
50
+
7
- インスタンス生成時に static 変数にインスタンスを入れているため必要ですね。失礼しました????
51
+ overSound = soundPool.load(context, R.raw.over, 1);
52
+
53
+ }
54
+
55
+
56
+
57
+ public void playHitSound() {
58
+
59
+ soundPool.play(hitSound, 1.0f, 1.0f, 1, 0, 1.0f);
60
+
61
+ }
62
+
63
+
64
+
65
+ public void playOverSound() {
66
+
67
+ soundPool.play(overSound, 1.0f, 1.0f, 1, 0, 1.0f);
68
+
69
+ }
70
+
71
+ }
72
+
73
+ ```

2

ミスを訂正

2021/10/17 15:39

投稿

blendthink
blendthink

スコア25

test CHANGED
@@ -2,4 +2,6 @@
2
2
 
3
3
 
4
4
 
5
- リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。。
5
+ ~~リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。。~~
6
+
7
+ → インスタンス生成時に static 変数にインスタンスを入れているため必要ですね。失礼しました????

1

文章の訂正

2021/10/17 15:12

投稿

blendthink
blendthink

スコア25

test CHANGED
@@ -1,4 +1,4 @@
1
- private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできないため、グローバル変数ではありません。
1
+ private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできません。よって、グローバル変数ではありません。
2
2
 
3
3
 
4
4