我有一个录制音频笔记的类,我希望将其实现到现有的活动中,但是在这种情况下,录制和播放按钮最终出现在屏幕顶部并且搞砸了。
在哪里以及如何使用以下代码指定按钮的显示属性...
JAVA - AudioRecordTest.java
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mRecordButton = new RecordButton(this);
mPlayButton = new PlayButton(this);
setContentView(R.layout.activity_audio_record_test);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
XML - activity_audio_record_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inspection ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtName"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1" />
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:gravity="top|left"
android:lines="4"
android:maxLines="4"
android:minLines="4"
android:scrollbars="vertical"
android:singleLine="false"
android:width="0dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Ref"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtAge"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drop Down"
/>
<Spinner
android:id="@+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPhotoCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Take Image with Camera" />
<Button
android:id="@+id/btnPhotoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Get Image from Gallery" />
<TextView
android:id="@+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="below_this_text_image_will_be_displayed"
android:textSize="13dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/lblDisplayImage"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:gravity="bottom" >
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-->
<ImageView
android:id="@+id/imgDisplayImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="area_where_image_is_to_be_displayed" />
<!-- </ScrollView> -->
</RelativeLayout>
<------------------- RECORD AND PLAY BUTTONS TO GO HERE ------------->
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAddEmp_Click"
android:text="Save Inspection" />
<Button
android:id="@+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btnPhotoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Reset/Clear Form Data" />
<TextView
android:id="@+id/txtEmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Inspections on Device " />
</LinearLayout>
</ScrollView>
</LinearLayout>
谢谢,
亨利