안드로이드 개발 질문/답변
(글 수 45,052)
재생중인 음악을 정지버튼을 누를시 정지시키고 싶은데요..
일딴 제가 원하는것이 재생버튼을 누르면 바로 정지버튼이 재생버튼 자리에서 뜨는건데요....[보통 음악플레이 버튼처럼...]
stop 버튼에 Visibility 에 gone으로 해놓았는데..그다음 소스는 어떻게 건들어야 할지...
이거 어떻게 해야 할지 모르겠네요....
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:orientation="vertical"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView1" android:gravity="center" android:layout_gravity="center" android:textSize="30dp" android:text="Sounds"></TextView> <RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:gravity="right" android:paddingBottom="5dp"> <Button android:id="@+id/btn_comic1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:background="@drawable/play" android:layout_centerVertical="true"></Button> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:onClick="onClickHandler" android:id="@+id/s_checkBox1"></CheckBox> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:text="Na Ra Ku" android:layout_centerVertical="true" android:layout_alignTop="@+id/s_checkBox1" android:layout_toRightOf="@+id/s_checkBox1"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/stop" android:visibility="gone" android:id="@+id/btn_comic1_stop"></Button> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout2" android:layout_height="wrap_content" android:gravity="right" android:paddingBottom="5dp"> <Button android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_comic2" android:onClick="onClick" android:background="@drawable/play" android:layout_centerVertical="true"></Button> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:onClick="onClickHandler" android:id="@+id/s_checkBox2"></CheckBox> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:text="Ba Bo" android:layout_centerVertical="true" android:layout_alignTop="@+id/s_checkBox2" android:layout_toRightOf="@+id/s_checkBox2"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/stop" android:visibility="gone" android:id="@+id/btn_comic2_stop"></Button> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout3" android:layout_height="wrap_content" android:gravity="right" android:paddingBottom="5dp"> <Button android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_comic3" android:onClick="onClick" android:background="@drawable/play" android:layout_centerVertical="true"></Button> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:onClick="onClickHandler" android:id="@+id/s_checkBox3"></CheckBox> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:text="Go Ja" android:layout_centerVertical="true" android:layout_alignTop="@+id/s_checkBox3" android:layout_toRightOf="@+id/s_checkBox3"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/stop" android:visibility="gone" android:id="@+id/btn_comic3_stop"></Button> </RelativeLayout> </LinearLayout>
package com.androidsample.Action_Sound;
import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton;
public class Sounds extends Activity implements CompoundButton.OnCheckedChangeListener { CheckBox cb; private MediaPlayer mPlayer = null; private MediaPlayer mPlayer2 = null; private MediaPlayer mPlayer3 = null;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // TODO Auto-generated method stub setContentView(R.layout.sounds); cb=(CheckBox)findViewById(R.id.s_checkBox1); cb.setOnCheckedChangeListener(this);
mPlayer = new MediaPlayer(); mPlayer2 = new MediaPlayer(); mPlayer3 = new MediaPlayer(); mPlayer = MediaPlayer.create(this, R.raw.naraku); mPlayer2 = MediaPlayer.create(this, R.raw.babo); mPlayer3 = MediaPlayer.create(this, R.raw.goja); } public void onCheckedChanged1(CompoundButton buttonView, boolean isChecked) { if (isChecked) { } } public void onClick(View v) { switch (v.getId()) { case R.id.btn_comic1: mPlayer.seekTo(0); mPlayer.start(); break; case R.id.btn_comic1_stop: if(mPlayer.isPlaying()) mPlayer.pause(); else mPlayer.start(); break; case R.id.btn_comic2: mPlayer2.seekTo(0); mPlayer2.start(); break; case R.id.btn_comic2_stop: if(mPlayer.isPlaying()) mPlayer2.pause(); else mPlayer2.start(); break; case R.id.btn_comic3: mPlayer3.seekTo(0); mPlayer3.start(); break; case R.id.btn_comic3_stop: if(mPlayer.isPlaying()) mPlayer3.pause(); else mPlayer3.start(); break; } } @Override protected void onDestroy() { super.onDestroy();
mPlayer.stop(); mPlayer.release(); mPlayer2.stop(); mPlayer.release(); mPlayer3.stop(); mPlayer.release(); }
public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub } }
2011.07.10 12:13:51
체커블을 관리할수있는 체크박스나 뭐 관련으로 컨트롤러를 변경하고 그냥 클럽님 말대로 셀렉터 정의해서 재생 정지버튼을 체커블에 따라 셋해주시면 한버튼으로 두기능 수행하실수 있습니다.
2011.07.10 21:46:17
1. 버튼 위치에 버튼크기만큼 FrameLayout으로 두고 그 안에 start, stop버튼 둘다 풀로 채워두고 하나를 gone으로 놓습니다. start버튼 누를때 stop버튼을 visible로 하고 start버튼을 gone으로 하고 제 역할을 합니다. stop은 반대로요.
2. 버튼 하나만 놓고 flag를 하나 두어서 true일때는 start역할을 하게, false일때는 stop역할을 하게 두어도 되겠지요. 물론 이때도 누를때마다 플레그 뒤집어줘야되고 버튼 이미지(또는 텍스트)도 바꾸어 줘야겠지요
그냥 버튼 눌렀을때 MediaPlayer.isPlaying() 으로 재생중인지 확인하고
재생중이면 정지하고 버튼 이미지를 시작 모양으로 변경,
재생중이 아니면 시작하고 버튼 이미지를 정지 모양으로 변경하시면 됩니다.