diff --git a/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java b/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java index e253d89..bfb6914 100644 --- a/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java +++ b/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java @@ -2,19 +2,24 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; + +import android.app.Activity; import android.os.Bundle; import android.media.MediaPlayer; import android.media.MediaRecorder; +import android.media.MediaPlayer.OnCompletionListener; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import java.io.File; +import java.io.IOException; import java.io.OutputStream; public class VoiceTest extends AppCompatActivity { @@ -27,31 +32,45 @@ private SeekBar positionBar; private TextView elapsedTimeLabel; private TextView remainingTimeLabel; - public int StartChange = 0; - //String[] txt = {"録音", "停止", "再生"}; - //boolean[] bl1 = {true, false, true}; - //boolean[] bl2 = {false, true, false}; + private int StartChange = 1; File file; - //再生 + //public class mp extends Activity implements OnCompletionListener{ + + //}; + + //再生開始&再生中止 private void startPlay(){ + ImageView Stoping = findViewById(R.id.Stoping); + ImageView Playing = findViewById(R.id.Playing); + float Tmin = 0; + float Tmax = 255; //mp = new MediaPlayer(); - StartChange = 1; //mp = MediaPlayer.create(this,R.raw.bgm); if(mp.isPlaying()){ - mp.stop(); + mp.stop();//再生を中断 + //mp.pause();//途中から再生を再開したい時はこっちを使う必要があるかも + Stoping.setAlpha(1.0f);//画像が見えるようにする処理 + Playing.setAlpha(0.0f);//画像が見えないようにする処理 + //try{ + //mp.prepare(); + //}catch(IllegalStateException e){ + //e.printStackTrace(); + //}catch(IOException e){ + //e.printStackTrace(); + //} }else { try { //mp = new MediaPlayer(); - mp.setDataSource(file.getAbsolutePath()); + mp.setDataSource(file.getAbsolutePath());//再生するファイルを持ってくる + //OutputStream filePath = openFileOutput("a",MODE_PRIVATE); //mp.setDataSource(filePath); mp.prepare(); }catch(IllegalStateException e){ e.printStackTrace(); - //mp.setLooping(false); - //mp.seekTo(0); + //if(StartChange == 1){ //positionBar = findViewById(R.id.positionBar); //positionBar.setMax(totalTime); @@ -73,10 +92,6 @@ //} //} //); - //totalTime = mp.getDuration(); - //String S_totalTime = createTimeLabel(totalTime);//createTimeLabelにtotalTimeを渡す。 - //TextView voiceTime = (TextView)findViewById(R.id.voiceTime); - //voiceTime.setText(S_totalTime); //} @@ -86,21 +101,14 @@ } catch (Exception e) { e.printStackTrace(); } + totalTime = mp.getDuration(); + String S_totalTime = createTimeLabel(totalTime);//createTimeLabelにtotalTimeを渡す。 + TextView voiceTime = (TextView)findViewById(R.id.voiceTime); + voiceTime.setText(S_totalTime); mp.seekTo(0); - mp.start(); - } - } - - //再生停止 - private void stopPlay(){ - try{ - mp.stop(); - mp.prepare(); - mp.release(); - }catch(IllegalStateException e){ - e.printStackTrace(); - }catch(Exception e){ - e.printStackTrace(); + mp.start();//再生スタート + Playing.setAlpha(1.0f); + Stoping.setAlpha(0.0f); } } @@ -116,35 +124,36 @@ return timeLabel; } - //録音開始 + //録音開始&録音終了 private void startRecord(){ - try{ - mr = new MediaRecorder(); - mr.setAudioSource(MediaRecorder.AudioSource.MIC); - mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); - mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); - mr.setOutputFile(file.getAbsolutePath()); - //mr.setOutputFile(filePath); - mr.prepare(); - mr.start(); + if(StartChange == 0) { + try { + mr.stop(); + mr.reset(); + mr.release(); + StartChange = 1; + } catch (Exception e) { + e.printStackTrace(); + } + }else { + try { + mr = new MediaRecorder(); + mr.setAudioSource(MediaRecorder.AudioSource.MIC); + mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); + mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); + mr.setOutputFile(file.getAbsolutePath()); + //mr.setOutputFile(filePath); - }catch(Exception e){ - e.printStackTrace(); + mr.prepare(); + mr.start(); + StartChange = 0; + } catch (Exception e) { + e.printStackTrace(); + } } } - //録音終了 - private void stopRecord(){ - try{ - mr.stop(); - mr.reset(); - mr.release(); - - }catch(Exception e){ - e.printStackTrace(); - } - } @Override protected void onCreate(Bundle savedInstanceState) { @@ -174,10 +183,12 @@ }); Button Saisei = (Button)findViewById(R.id.button4); + //Saisei.setAlpha(0); Saisei.setOnClickListener(new View.OnClickListener(){ public void onClick(View x){ TextView textView1 = (TextView)findViewById(R.id.text); textView1.setText("Saisei Start!"); + startPlay(); } }); @@ -192,24 +203,51 @@ }); } - private Handler handler = new Handler(){ - @Override - public void handleMessage(Message msg){ - int currentPosition = msg.what; + //private Handler handler = new Handler(){ + //@Override + //public void handleMessage(Message msg){ + //int currentPosition = msg.what; //再生位置を更新 //positionBar.setProgress(currentPositon); //経過時間ラベル更新 - String elapsedTime = createTimeLabel(currentPosition); + //String elapsedTime = createTimeLabel(currentPosition); //elapseTimeLabel.setText(elapsedTime); //残り時間ラベル更新 - String remainingTime = createTimeLabel(totalTime - currentPosition); + //String remainingTime = createTimeLabel(totalTime - currentPosition); //remainingTimeLabel.setText("-" + remainingTime); - } - }; + //} + //}; + + //再生停止 + //private void stopPlay(){ + //try{ + //mp.stop(); + //mp.prepare(); + //mp.release(); + //}catch(IllegalStateException e){ + //e.printStackTrace(); + //}catch(Exception e){ + //e.printStackTrace(); + //} + //} + + + //録音終了 + //private void stopRecord(){ + //try{ + //mr.stop(); + //mr.reset(); + //mr.release(); + + //}catch(Exception e){ + //e.printStackTrace(); + //} + //} + }