Iman Yasmin
Iman Yasmin

Reputation: 447

Android : Two activities that shared one layout

I've two separated activities, recording.java and audio.java that shared the same xml. What should I do to combine them together in one activity? Or is there any other approach? Please let me know. Thank you.

Here's the code :

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" />

Upvotes: 0

Views: 738

Answers (2)

Qian Sijianhao
Qian Sijianhao

Reputation: 564

I think Fragment will be the best choice in this occasion.Change both of your activities into fragments and make another activity to contains them.

Upvotes: 1

Passiondroid
Passiondroid

Reputation: 1589

After giving a short look at your code it seems you are recording some audio in one activity and playing the audio in another activity. And if you want to achieve both the function in one activity then you can make both activities as fragments and show it into one single activity.

Upvotes: 1

Related Questions