質問編集履歴
2
タイトルの一部を変更しました。(「メモリ解放したい」→「closeしたい」)
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
使い終えたjavax.sound.sampled.Clipを自動で
|
1
|
+
使い終えたjavax.sound.sampled.Clipを自動でcloseしたい。
|
body
CHANGED
File without changes
|
1
MusicPlayerクラスを呼び出すMusicPlayerManagerクラスのコードを追記しました。また、助言を受けて試したことの結果を追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -92,13 +92,127 @@
|
|
92
92
|
}
|
93
93
|
```
|
94
94
|
|
95
|
-
MusicPlayerのインスタンスは
|
95
|
+
MusicPlayerのインスタンスはMusicPlayerManagerクラスで生成され、このクラスのArrayListに格納・保持されています。
|
96
96
|
|
97
|
+
MusicPlayerManager.java
|
98
|
+
```Java
|
99
|
+
import java.util.ArrayList;
|
100
|
+
import java.util.Queue;
|
101
|
+
import java.util.ArrayDeque;
|
102
|
+
import javax.sound.sampled.Clip;
|
103
|
+
|
104
|
+
public class MusicPlayerManager {
|
105
|
+
|
106
|
+
private static ArrayList<String> filePathList;
|
107
|
+
private static ArrayList<MusicPlayer> musicPlayers;
|
108
|
+
private static RhythmManager rhythmManager;
|
109
|
+
|
110
|
+
// 何番にPlay指示が出たかを一時保管し、リズムに合わせて処理するためのバッファ
|
111
|
+
private static Queue<Integer> orderBuffer;
|
112
|
+
|
113
|
+
public MusicPlayerManager() {
|
114
|
+
|
115
|
+
filePathList = new ArrayList<String>();
|
116
|
+
musicPlayers = new ArrayList<MusicPlayer>();
|
117
|
+
rhythmManager = new RhythmManager(150);
|
118
|
+
orderBuffer = new ArrayDeque<>();
|
119
|
+
}
|
120
|
+
|
121
|
+
public MusicPlayerManager(String[] filePath) {
|
122
|
+
|
123
|
+
this();
|
124
|
+
for(String filePath_ : filePath) {
|
125
|
+
filePathList.add(filePath_);
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
public MusicPlayerManager(ArrayList<String> filePath) {
|
130
|
+
|
131
|
+
this();
|
132
|
+
for(String filePath_ : filePath) {
|
133
|
+
filePathList.add(filePath_);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
public void Play(int fileNum) {
|
138
|
+
|
139
|
+
// 処理待機バッファ内のfileNumの重複を避ける
|
140
|
+
if(!orderBuffer.contains(fileNum))
|
141
|
+
orderBuffer.add(fileNum);
|
142
|
+
}
|
143
|
+
|
144
|
+
public void AddFile(String filePath) {
|
145
|
+
|
146
|
+
filePathList.add(filePath);
|
147
|
+
}
|
148
|
+
|
149
|
+
public int GetFileTotalNum() {
|
150
|
+
|
151
|
+
return filePathList.size();
|
152
|
+
}
|
153
|
+
|
154
|
+
// RhythmManagerに呼び出されるメソッド
|
155
|
+
// リズムに合わせて一定間隔で呼び出される
|
156
|
+
public static void PlayAllInOrderBuffer()
|
157
|
+
{
|
158
|
+
// キューの先頭を取得し削除しながらループする
|
159
|
+
while(orderBuffer.peek() != null)
|
160
|
+
{
|
161
|
+
int buf = orderBuffer.poll();
|
162
|
+
musicPlayers.add(new MusicPlayer(filePathList.get(buf)));
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
```
|
167
|
+
|
97
168
|
### 試したこと
|
98
169
|
|
99
170
|
- `timer.schedule(timerTask, (int)(p.getMicrosecondLength() / 1000))`の部分を`timer.scheduleAtFixedRate(timerTask, (int)(p.getMicrosecondLength() / 1000), 1000)`(第3引数の1000は適当)としてみましたが、動作は大差ありませんでした。
|
100
171
|
- javax.swing.TimerとActionListenerを用いて同様のプログラムを作成しましたが、動作は大差ありませんでした。
|
172
|
+
- m-oguraさんの助言を受け、try-with-resourcesを用いてMusicPlayer.javaを下のように修正しましたが、タスクマネージャーでOpenJDK Platform binaryアプリ(起動中のアプリ)のメモリを監視したところ、音が鳴るたびにメモリ量は増え続けており、ストリームはclose出来ていないようでした。
|
173
|
+
```Java
|
174
|
+
import java.io.File;
|
175
|
+
import javax.sound.sampled.AudioFormat;
|
176
|
+
import javax.sound.sampled.AudioInputStream;
|
177
|
+
import javax.sound.sampled.AudioSystem;
|
178
|
+
import javax.sound.sampled.DataLine;
|
179
|
+
import javax.sound.sampled.Clip;
|
101
180
|
|
181
|
+
// Exceptions
|
182
|
+
import java.io.IOException;
|
183
|
+
import javax.sound.sampled.LineUnavailableException;
|
184
|
+
import javax.sound.sampled.UnsupportedAudioFileException;
|
185
|
+
|
186
|
+
public class MusicPlayer
|
187
|
+
{
|
188
|
+
public MusicPlayer(String filePath)
|
189
|
+
{
|
190
|
+
LoadClipPlayer(filePath);
|
191
|
+
}
|
192
|
+
|
193
|
+
private static void LoadClipPlayer(String filePath) {
|
194
|
+
|
195
|
+
try(AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filePath));)
|
196
|
+
{
|
197
|
+
AudioFormat format = stream.getFormat();
|
198
|
+
DataLine.Info dataLine = new DataLine.Info(Clip.class, format);
|
199
|
+
Clip clip = (Clip)AudioSystem.getLine(dataLine);
|
200
|
+
clip.open(stream);
|
201
|
+
clip.start();
|
202
|
+
}
|
203
|
+
catch(UnsupportedAudioFileException e) {
|
204
|
+
e.printStackTrace();
|
205
|
+
}
|
206
|
+
catch(LineUnavailableException e) {
|
207
|
+
e.printStackTrace();
|
208
|
+
}
|
209
|
+
catch(IOException e) {
|
210
|
+
e.printStackTrace();
|
211
|
+
}
|
212
|
+
}
|
213
|
+
}
|
214
|
+
```
|
215
|
+
|
102
216
|
### 補足情報(FW/ツールのバージョンなど)
|
103
217
|
|
104
218
|
バージョンは以下の通りです。
|