質問編集履歴
1
wavファイルを保存するソースコードを記載しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -72,3 +72,184 @@
|
|
72
72
|
<files-path name="share" path="."/>
|
73
73
|
</paths>
|
74
74
|
```
|
75
|
+
|
76
|
+
**RecordFragment**
|
77
|
+
```java
|
78
|
+
public class RecordFragment extends Fragment {
|
79
|
+
|
80
|
+
private Realm realm;
|
81
|
+
private MediaRecorder recorder;
|
82
|
+
private long startTime;
|
83
|
+
private long diff;
|
84
|
+
private final Handler handler = new Handler(Looper.getMainLooper());
|
85
|
+
public boolean loop;
|
86
|
+
private TextView timerText;
|
87
|
+
private String fileName;
|
88
|
+
private enum STATE{
|
89
|
+
initialize,
|
90
|
+
recording,
|
91
|
+
pause
|
92
|
+
}
|
93
|
+
private STATE state;
|
94
|
+
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddkkmmss");
|
95
|
+
private SimpleDateFormat playFormat = new SimpleDateFormat("mm:ss");
|
96
|
+
View.OnClickListener buttonListener = new View.OnClickListener() {
|
97
|
+
@Override
|
98
|
+
public void onClick(View v) {
|
99
|
+
if(v == cancelButton){
|
100
|
+
clearRecord();
|
101
|
+
}else if(v == recordButton){
|
102
|
+
startRecord();
|
103
|
+
}else if(v == checkButton){
|
104
|
+
stopRecord();
|
105
|
+
}
|
106
|
+
}
|
107
|
+
};
|
108
|
+
|
109
|
+
private ImageButton cancelButton;
|
110
|
+
private ImageButton recordButton;
|
111
|
+
private ImageButton checkButton;
|
112
|
+
@Nullable
|
113
|
+
@Override
|
114
|
+
|
115
|
+
public void onCreate(Bundle instance){
|
116
|
+
super.onCreate(instance);
|
117
|
+
realm = Realm.getDefaultInstance();
|
118
|
+
}
|
119
|
+
|
120
|
+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
121
|
+
//fragmentのレイアウトを指定する
|
122
|
+
return inflater.inflate(R.layout.record_fragment_layout, null);
|
123
|
+
}
|
124
|
+
|
125
|
+
public void onViewCreated(View view, Bundle savedInstanceState){
|
126
|
+
cancelButton = view.findViewById(R.id.cancelButton);
|
127
|
+
switchButton(cancelButton, View.GONE, false);
|
128
|
+
recordButton = view.findViewById(R.id.recordButton);
|
129
|
+
checkButton = view.findViewById(R.id.checkButton);
|
130
|
+
switchButton(checkButton, View.GONE, false);
|
131
|
+
cancelButton.setOnClickListener(buttonListener);
|
132
|
+
recordButton.setOnClickListener(buttonListener);
|
133
|
+
checkButton.setOnClickListener(buttonListener);
|
134
|
+
|
135
|
+
timerText = view.findViewById(R.id.recordTimeView);
|
136
|
+
recorder = new MediaRecorder();
|
137
|
+
state = STATE.initialize;
|
138
|
+
}
|
139
|
+
|
140
|
+
@Override
|
141
|
+
public void onStart() {
|
142
|
+
super.onStart();
|
143
|
+
}
|
144
|
+
|
145
|
+
public void onActivityCreated(Bundle savedInstanceState){
|
146
|
+
super.onActivityCreated(savedInstanceState);
|
147
|
+
}
|
148
|
+
|
149
|
+
public void startRecord(){
|
150
|
+
if(state == STATE.initialize) {
|
151
|
+
state = STATE.recording;
|
152
|
+
fileName = dateFormat.format(new Date()) + ".wav";
|
153
|
+
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
|
154
|
+
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
|
155
|
+
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
|
156
|
+
recorder.setOutputFile(getContext().getFilesDir() + "/audio/" + fileName);
|
157
|
+
try {
|
158
|
+
recorder.prepare();
|
159
|
+
} catch (Exception e) {
|
160
|
+
e.printStackTrace();
|
161
|
+
}
|
162
|
+
TimerThread thread = new TimerThread();
|
163
|
+
thread.start();
|
164
|
+
loop = true;
|
165
|
+
recorder.start();
|
166
|
+
switchButton(recordButton, View.GONE, false);
|
167
|
+
switchButton(cancelButton, View.VISIBLE, true);
|
168
|
+
switchButton(checkButton, View.VISIBLE, true);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
private void clearRecord(){
|
173
|
+
recorder.stop();
|
174
|
+
recorder.reset();
|
175
|
+
loop = false;
|
176
|
+
state = STATE.initialize;
|
177
|
+
File file = new File(getContext().getFilesDir()+"/audio/"+fileName);
|
178
|
+
file.delete();
|
179
|
+
if(!file.exists()){
|
180
|
+
Log.i("File", "DELETE");
|
181
|
+
}
|
182
|
+
switchButton(recordButton, View.VISIBLE, true);
|
183
|
+
switchButton(cancelButton, View.GONE, false);
|
184
|
+
switchButton(checkButton, View.GONE, false);
|
185
|
+
timerText.setText(playFormat.format(0));
|
186
|
+
}
|
187
|
+
|
188
|
+
private void switchButton(ImageButton button, int visibility, boolean enabled){
|
189
|
+
button.setVisibility(visibility);
|
190
|
+
button.setEnabled(enabled);
|
191
|
+
}
|
192
|
+
public void stopRecord(){
|
193
|
+
if(state == STATE.initialize) return;
|
194
|
+
|
195
|
+
state = STATE.initialize;
|
196
|
+
recorder.stop();
|
197
|
+
recorder.reset();
|
198
|
+
loop = false;
|
199
|
+
|
200
|
+
File[] list = MainFragment.getCountFile(getContext().getFilesDir()+"/audio/");
|
201
|
+
|
202
|
+
Bundle bundle = new Bundle();
|
203
|
+
bundle.putString("fileName", fileName);
|
204
|
+
bundle.putLong("playTime", diff);
|
205
|
+
realm.executeTransactionAsync(new Realm.Transaction() { //データベースにデータを追加
|
206
|
+
@Override
|
207
|
+
public void execute(Realm realm) {
|
208
|
+
Number maxId = realm.where(AudioInfo.class).max("id"); //データがない場合、nullが帰ってくるので、nullに対応した処理を行う
|
209
|
+
int nextId = (maxId == null) ? 0 : maxId.intValue()+1;
|
210
|
+
AudioInfo info = realm.createObject(AudioInfo.class, nextId); //createObjectでデータを追加し、追加したデータのモデルを返す
|
211
|
+
//追加されたモデルのプロパティに値をセットする
|
212
|
+
info.setPlayingTime(diff);
|
213
|
+
info.setName(fileName);
|
214
|
+
info.setFileSize(10.0);
|
215
|
+
}
|
216
|
+
});
|
217
|
+
//
|
218
|
+
// getParentFragmentManager().setFragmentResult("Record", bundle);
|
219
|
+
switchButton(recordButton, View.VISIBLE, true);
|
220
|
+
switchButton(cancelButton, View.GONE, false);
|
221
|
+
switchButton(checkButton, View.GONE, false);
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
public class TimerThread extends Thread{
|
228
|
+
@Override
|
229
|
+
public void run() {
|
230
|
+
startTime = System.currentTimeMillis();
|
231
|
+
while(loop){
|
232
|
+
try{
|
233
|
+
Thread.sleep(20);
|
234
|
+
}catch (InterruptedException e){
|
235
|
+
e.printStackTrace();
|
236
|
+
}
|
237
|
+
handler.post(new Runnable() {
|
238
|
+
@Override
|
239
|
+
public void run() {
|
240
|
+
diff = System.currentTimeMillis() - startTime;
|
241
|
+
timerText.setText(playFormat.format(diff));
|
242
|
+
}
|
243
|
+
});
|
244
|
+
}
|
245
|
+
handler.post(new Runnable() {
|
246
|
+
@Override
|
247
|
+
public void run() {
|
248
|
+
timerText.setText(playFormat.format(0));
|
249
|
+
}
|
250
|
+
});
|
251
|
+
}
|
252
|
+
}
|
253
|
+
}
|
254
|
+
|
255
|
+
```
|