1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.mobileer.oboetester;
18 
19 import android.os.Bundle;
20 import android.view.View;
21 import android.widget.Button;
22 
23 import java.io.IOException;
24 
25 /**
26  * Activity to record and play back audio.
27  */
28 public class RecorderActivity extends TestInputActivity {
29 
30     private static final int STATE_RECORDING = 5;
31     private static final int STATE_PLAYING = 6;
32     private int mRecorderState = AUDIO_STATE_STOPPED;
33     private Button mRecordButton;
34     private Button mStopButton;
35     private Button mPlayButton;
36     private Button mShareButton;
37     private boolean mGotRecording;
38 
39     @Override
inflateActivity()40     protected void inflateActivity() {
41         setContentView(R.layout.activity_recorder);
42     }
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         mRecordButton = (Button) findViewById(R.id.button_start_recording);
49         mStopButton = (Button) findViewById(R.id.button_stop_record_play);
50         mPlayButton = (Button) findViewById(R.id.button_start_playback);
51         mShareButton = (Button) findViewById(R.id.button_share);
52         mRecorderState = AUDIO_STATE_STOPPED;
53         mGotRecording = false;
54         updateButtons();
55     }
56 
getActivityType()57     int getActivityType() {
58         return ACTIVITY_RECORD_PLAY;
59     }
60 
onStartRecording(View view)61     public void onStartRecording(View view) {
62         try {
63             openAudio();
64             startAudio();
65             mRecorderState = STATE_RECORDING;
66             mGotRecording = true;
67             updateButtons();
68         } catch (IOException e) {
69             showErrorToast(e.getMessage());
70         }
71     }
72 
onStopRecordPlay(View view)73     public void onStopRecordPlay(View view) {
74         stopAudio();
75         closeAudio();
76         mRecorderState = AUDIO_STATE_STOPPED;
77         updateButtons();
78     }
79 
onStartPlayback(View view)80     public void onStartPlayback(View view) {
81         startPlayback();
82         mRecorderState = STATE_PLAYING;
83         updateButtons();
84     }
85 
updateButtons()86     private void updateButtons() {
87         mRecordButton.setEnabled(mRecorderState == AUDIO_STATE_STOPPED);
88         mStopButton.setEnabled(mRecorderState != AUDIO_STATE_STOPPED);
89         mPlayButton.setEnabled(mRecorderState == AUDIO_STATE_STOPPED && mGotRecording);
90         mShareButton.setEnabled(mRecorderState == AUDIO_STATE_STOPPED && mGotRecording);
91     }
92 
startPlayback()93     public void startPlayback() {
94         try {
95             mAudioInputTester.startPlayback();
96             updateStreamConfigurationViews();
97             updateEnabledWidgets();
98         } catch (Exception e) {
99             e.printStackTrace();
100             showErrorToast(e.getMessage());
101         }
102     }
103 
104     @Override
getWaveTag()105     String getWaveTag() {
106         return "recording";
107     }
108 
109 }
110