import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
MediaPlayer[] mediaPlayer = new MediaPlayer[3];
private Button sound1StartButton;
private Button sound2StartButton;
private Button sound3StartButton;
private Button sound1StopButton;
private Button sound2StopButton;
private Button sound3StopButton;
private Button playpauseButton;
private boolean isPlaying;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sound1StartButton = (Button) findViewById(R.id.sound1StartButton);
sound2StartButton = (Button) findViewById(R.id.sound2StartButton);
sound3StartButton = (Button) findViewById(R.id.sound3StartButton);
sound1StopButton = (Button) findViewById(R.id.sound1StopButton);
sound2StopButton = (Button) findViewById(R.id.sound2StopButton);
sound3StopButton = (Button) findViewById(R.id.sound3StopButton);
playpauseButton = (Button) findViewById(R.id.PlayPauseButton) ;
sound1StopButton.setVisibility(View.INVISIBLE);
sound2StopButton.setVisibility(View.INVISIBLE);
sound3StopButton.setVisibility(View.INVISIBLE);
}
public void onPlaySound(View v) {
switch (v.getId()) {
case R.id.sound1StartButton:
mediaPlayer[0] = MediaPlayer.create(getApplicationContext(), R.raw.book);
mediaPlayer[0].start();
sound1StartButton.setVisibility(View.INVISIBLE);
sound1StopButton.setVisibility(View.VISIBLE);
playpauseButton.setText("Pause");
isPlaying = true;
break;
case R.id.sound2StartButton:
mediaPlayer[1] = MediaPlayer.create(getApplicationContext(), R.raw.silhum);
mediaPlayer[1].start();
sound2StartButton.setVisibility(View.INVISIBLE);
sound2StopButton.setVisibility(View.VISIBLE);
playpauseButton.setText("Pause");
isPlaying = true;
break;
case R.id.sound3StartButton:
mediaPlayer[2] = MediaPlayer.create(getApplicationContext(), R.raw.sunburst);
mediaPlayer[2].start();
sound3StartButton.setVisibility(View.INVISIBLE);
sound3StopButton.setVisibility(View.VISIBLE);
playpauseButton.setText("Pause");
isPlaying = true;
break;
}
}
public void onStopSound(View v) {
switch (v.getId()) {
case R.id.sound1StopButton:
//mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.book);
mediaPlayer[0].stop();
//mediaPlayer.release();
sound1StartButton.setVisibility(View.VISIBLE);
sound1StopButton.setVisibility(View.INVISIBLE);
playpauseButton.setText("Play");
isPlaying = false;
break;
case R.id.sound2StopButton:
//mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.silhum);
mediaPlayer[1].stop();
//mediaPlayer.release();
sound2StartButton.setVisibility(View.VISIBLE);
sound2StopButton.setVisibility(View.INVISIBLE);
playpauseButton.setText("Play");
isPlaying = false;
break;
case R.id.sound3StopButton:
//mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.sunburst);
mediaPlayer[2].stop();
//mediaPlayer.release();
sound3StartButton.setVisibility(View.VISIBLE);
sound3StopButton.setVisibility(View.INVISIBLE);
playpauseButton.setText("Play");
isPlaying = false;
break;
}
}
public void onPlayPause(View v) {
if (mediaPlayer == null) {
Toast.makeText(this, "재생 할 것이 없습니다", Toast.LENGTH_SHORT).show();
} else {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
playpauseButton.setText("Play");
} else {
mediaPlayer.start();
playpauseButton.setText("Pause");
}
}
}
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
playpauseButton.setText("Play");
} else {
mediaPlayer.start();
playpauseButton.setText("Pause"); <<이 조건문을 어떻게 해야 트랙이 동시에 일시정지되고 일시정지된트랙만 재생시킬수 있을까요??