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 7dfb80d..0554c2f 100644 --- a/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java +++ b/app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java @@ -1,13 +1,17 @@ package org.ntlab.amaryllis.client; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; +import android.os.Handler; +import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; +import android.widget.SeekBar; import android.widget.TextView; import java.io.File; @@ -19,6 +23,11 @@ private MediaPlayer mp; private MediaRecorder mr; static final String filePath = Environment.getExternalStorageDirectory() + "/app/res/raw"; + private int totalTime; + 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}; @@ -27,6 +36,7 @@ //再生 private void startPlay(){ mp = new MediaPlayer(); + StartChange = 1; //mp = MediaPlayer.create(this,R.raw.bgm); try { //mp = new MediaPlayer(); @@ -34,6 +44,37 @@ //OutputStream filePath = openFileOutput("a",MODE_PRIVATE); //mp.setDataSource(filePath); mp.prepare(); + mp.setLooping(false); + mp.seekTo(0); + //if(StartChange == 1){ + positionBar = findViewById(R.id.positionBar); + positionBar.setMax(totalTime); + positionBar.setOnSeekBarChangeListener( + new SeekBar.OnSeekBarChangeListener(){ + public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){ + if(fromUser){ + mp.seekTo(progress); + positionBar.setProgress(progress); + } + } + + public void onStartTrackingTouch(SeekBar seekBar){ + + } + + public void onStopTrackingTouch(SeekBar seekBar){ + + } + } + ); + totalTime = mp.getDuration(); + String S_totalTime = createTimeLabel(totalTime);//createTimeLabelにtotalTimeを渡す。 + TextView voiceTime = (TextView)findViewById(R.id.voiceTime); + voiceTime.setText(S_totalTime); + //voiceTime.setText("S_totalTime"); + + //} + mp.start(); }catch(Exception e){ e.printStackTrace(); @@ -52,6 +93,19 @@ } } + //totalTimeを分、秒に変換 + public String createTimeLabel(int time){ + String timeLabel = ""; + int min = time/1000/60; + int sec = time/1000%60; + + timeLabel = min + ":"; + if(sec < 10) timeLabel += "0"; + timeLabel += sec; + return timeLabel; + } + + //録音開始 private void startRecord(){ try{ mr = new MediaRecorder(); @@ -69,6 +123,7 @@ } } + //録音終了 private void stopRecord(){ try{ mr.stop(); @@ -86,6 +141,8 @@ setContentView(R.layout.activity_voice_test); file = new File(getFilesDir(),"Sample.3gp"); + + Button Rokuon = (Button)findViewById(R.id.button2); Rokuon.setOnClickListener(new View.OnClickListener(){ public void onClick(View x){ @@ -113,5 +170,25 @@ } }); } + + private Handler handler = new Handler(){ + @Override + public void handleMessage(Message msg){ + int currentPosition = msg.what; + + //再生位置を更新 + //positionBar.setProgress(currentPositon); + + //経過時間ラベル更新 + String elapsedTime = createTimeLabel(currentPosition); + //elapseTimeLabel.setText(elapsedTime); + + //残り時間ラベル更新 + String remainingTime = createTimeLabel(totalTime - currentPosition); + //remainingTimeLabel.setText("-" + remainingTime); + } + }; + + }