VoiceTest内を修正しました。
1 parent c05bd2a commit 7b771ed9d1afa2d73cf9b8e17a355d89cff98dad
Ninja Team authored on 30 Jun 2020
Showing 2 changed files
View
231
app/src/main/java/org/ntlab/amaryllis/client/VoiceTest.java
package org.ntlab.amaryllis.client;
 
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 {
 
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};
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);
//positionBar.setOnSeekBarChangeListener(
 
//}
//}
//);
//totalTime = mp.getDuration();
//String S_totalTime = createTimeLabel(totalTime);//createTimeLabelにtotalTimeを渡す。
//TextView voiceTime = (TextView)findViewById(R.id.voiceTime);
//voiceTime.setText(S_totalTime);
 
//}
 
//mp.start();
 
} 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);
}
}
 
//totalTimeを分、秒に変換
timeLabel += sec;
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();
 
}catch(Exception e){
e.printStackTrace();
 
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);
 
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) {
super.onCreate(savedInstanceState);
}
});
 
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();
}
});
 
}
});
}
 
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();
//}
//}
 
}
 
View
87
app/src/main/res/layout/activity_voice_test.xml
tools:context=".VoiceTest">
 
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="419dp"
android:layout_height="525dp"
android:layout_width="411dp"
android:layout_height="384dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.077"
tools:layout_editor_absoluteX="-4dp">
tools:layout_editor_absoluteX="5dp">
 
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_width="290dp"
android:layout_height="49dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:layout_constraintVertical_bias="0.896" />
 
<ImageView
android:id="@+id/Stoping"
android:layout_width="123dp"
android:layout_height="61dp"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:srcCompat="@android:drawable/ic_media_play"
tools:layout_editor_absoluteX="25dp" />
 
<ImageView
android:id="@+id/Playing"
android:layout_width="125dp"
android:layout_height="65dp"
android:layout_marginTop="100dp"
android:alpha="0"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:srcCompat="@android:drawable/ic_media_pause"
tools:layout_editor_absoluteX="25dp" />
 
<ImageView
android:id="@+id/imageView3"
android:layout_width="79dp"
android:layout_height="103dp"
android:layout_marginStart="88dp"
android:layout_marginTop="40dp"
app:layout_constraintStart_toEndOf="@+id/Stoping"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:srcCompat="@android:drawable/stat_notify_call_mute" />
 
<ImageView
android:id="@+id/imageView4"
android:layout_width="83dp"
android:layout_height="114dp"
android:layout_marginStart="88dp"
android:layout_marginTop="176dp"
app:layout_constraintStart_toEndOf="@+id/Stoping"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:srcCompat="@android:drawable/ic_btn_speak_now" />
 
</androidx.constraintlayout.widget.ConstraintLayout>