同じxmlを共有する2つの別々のアクティビティ、recording.java
とaudio.java
があります。 1つのアクティビティでそれらを組み合わせるにはどうすればよいですか?それとも他の方法がありますか?私にお知らせください。ありがとうございました。Android:1つのレイアウトを共有する2つのアクティビティ
ここでは、コードです:あなたのコードで短い一見を与えた後
Recording.java
public class Recording extends Activity {
private static final String LOG_TAG = "Audio";
private static String FileName = null;
private RecordButton RecordButton = null;
private MediaRecorder Recorder = null;
private PlayButton PlayButton = null;
private MediaPlayer Player = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
Player = new MediaPlayer();
try {
Player.setDataSource(FileName);
Player.prepare();
Player.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
Player.release();
Player = null;
}
private void startRecording() {
Recorder = new MediaRecorder();
Recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
Recorder.setOutputFile(FileName);
Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
Recorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
Recorder.start();
}
private void stopRecording() {
Recorder.stop();
Recorder.release();
Recorder = null;
}
class RecordButton extends Button {
boolean StartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(StartRecording);
if (StartRecording) {
setText("Stop Record");
PlayButton.setEnabled(false);
} else {
setText("Start Record");
PlayButton.setEnabled(true);
}
StartRecording = !StartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start Record");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean StartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(StartPlaying);
if (StartPlaying) {
setText("Stop Playing");
RecordButton.setEnabled(false);
} else {
setText("Start Playing");
RecordButton.setEnabled(true);
}
StartPlaying = !StartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start Playing");
setOnClickListener(clicker);
}
}
public Recording() {
FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName += "/audiorecording.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main_layout);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LinearLayout audio = new LinearLayout(this);
audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);
RecordButton = new RecordButton(this);
audio.addView(RecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
PlayButton = new PlayButton(this);
audio.addView(PlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER | Gravity.BOTTOM));
container.addView(audio);
}
@Override
public void onPause() {
super.onPause();
if (Recorder != null) {
Recorder.release();
Recorder = null;
}
if (Player != null) {
Player.release();
Player = null;
}
}
}
Audio.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
final MediaPlayer sound = MediaPlayer.create(Audio.this, R.raw.audio1);
imageButton1.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v){
sound.start();
}
});
imageButton2.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v){
sound.pause();
}
});
}
}
audio.xml
<Button
android:id="@+id/Play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />
<Button
android:id="@+id/Pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />
はい、アクティビティを記録し、オーディオアクティビティを再生しています。私はそれを調べます。ありがとう@パッシオンドロイド –