停止并在Android中的单选按钮播放

时间:2015-06-05 07:19:03

标签: android radio-button

我在原始文件夹中有2个mp3文件。我试图在单选按钮中显示音频文件。如果我单击单选按钮我想播放音频。如果选择另一个单选按钮,旧音频应该停止,新音频应播放。这是我的代码。在我的代码音频正在播放,但当我选择另一个音频应用程序退出。

提前谢谢      options.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="10dp"
        android:text="Add Background" />

    <RadioButton
        android:id="@+id/music1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_below="@+id/radioButton1"
        android:layout_marginLeft="52dp"
        android:layout_marginTop="28dp"
        android:text="Music1"
        android:visibility="gone" />

    <RadioButton
        android:id="@+id/music2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/music1"
        android:layout_below="@+id/radioButton1"
        android:layout_marginLeft="52dp"
        android:text="Music2"
        android:visibility="gone" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_below="@+id/music1"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="16dp"
        android:text="Self Speach" />
</RadioGroup>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="47dp"
    android:text="Button" />

  public class OptionActivity extends Activity {

 RadioButton myOption1, myOption2, myOption3,myOption4;
 MediaPlayer mp,mp1;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.options);
      myOption1 = (RadioButton)findViewById(R.id.radioButton1);
      myOption2 = (RadioButton)findViewById(R.id.music1);
      myOption3 = (RadioButton)findViewById(R.id.music2);
      myOption4 = (RadioButton)findViewById(R.id.radioButton2);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      myOption1.setOnClickListener(myOptionOnClickListener);
      myOption2.setOnClickListener(myOptionOnClickListener);
      myOption3.setOnClickListener(myOptionOnClickListener);
      myOption4.setOnClickListener(myOptionOnClickListener);
      myOption1.setChecked(true);
      mp = MediaPlayer.create(this, R.raw.airtel);
      mp1 = MediaPlayer.create(this, R.raw.airtelbird);
  }
  RadioButton.OnClickListener myOptionOnClickListener =
   new RadioButton.OnClickListener()
  {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
      if(myOption1.isChecked())
      {

          myOption2.setVisibility(View.VISIBLE);
          myOption3.setVisibility(View.VISIBLE);


            /*  int resId;
              switch (v.getId()) {
              case R.id.music1:
                //resId = R.raw.airtel;

                break;
              case R.id.music2:
                 //resId = R.raw.airtelbird;
                  Toast.makeText(getApplicationContext(), "Music2", Toast.LENGTH_SHORT).show();
                break;
              default:
                //resId = R.raw.airtel;
                  Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_SHORT).show();
                break;
              }*/

      }
      if(myOption4.isChecked())
      {

          myOption2.setVisibility(View.GONE);
          myOption3.setVisibility(View.GONE);
      }

      if(myOption2.isChecked())
      {
          //Toast.makeText(getApplicationContext(), "Music1", Toast.LENGTH_SHORT).show();

         mp.start();
         mp1.release();

      }

      else if(myOption3.isChecked())
      {      
          mp1.start();
         mp.release();

      }


    }


  };
}

这是我的错误日志

    06-05 12:44:31.060: E/AndroidRuntime(9561): FATAL EXCEPTION: main
    06-05 12:44:31.060: E/AndroidRuntime(9561): Process:   com.example.videoapp, PID: 9561
    06-05 12:44:31.060: E/AndroidRuntime(9561):   java.lang.IllegalStateException
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.media.MediaPlayer._getAudioStreamType(Native Method)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.media.MediaPlayer.getAudioStreamType(MediaPlayer.java:1363)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.media.MediaPlayer.isRestricted(MediaPlayer.java:1347)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.media.MediaPlayer.start(MediaPlayer.java:1336)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at com.example.videoapp.OptionActivity$1.onClick(OptionActivity.java:89)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.view.View.performClick(View.java:5181)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.widget.CompoundButton.performClick(CompoundButton.java:128)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.view.View$PerformClick.run(View.java:20887)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.os.Handler.handleCallback(Handler.java:739)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.os.Handler.dispatchMessage(Handler.java:95)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.os.Looper.loop(Looper.java:145)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at android.app.ActivityThread.main(ActivityThread.java:5942)
    06-05 12:44:31.060: E/AndroidRuntime(9561):     at java.lang.reflect.Method.invoke(Native Method)
   06-05 12:44:31.060: E/AndroidRuntime(9561):  at java.lang.reflect.Method.invoke(Method.java:372)
   06-05 12:44:31.060: E/AndroidRuntime(9561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
   06-05 12:44:31.060: E/AndroidRuntime(9561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

1 个答案:

答案 0 :(得分:1)

您可以自定义单选按钮,在未选中时看起来像停止按钮,在选择时看起来像播放,并在按钮单击处理程序中处理功能。 您需要使用单选按钮组。 让我分享代码

&#13;
&#13;
 <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android">
        <RadioButton android:id="@+id/radio_1"

        android:background="@drawable/radio_button"
            android:onClick="onRadioButtonClicked"/>
        <RadioButton android:id="@+id/radio_2"

        android:background="@drawable/radio_button"/>
        <RadioButton android:id="@+id/radio_3"
            android:onClick="onRadioButtonClicked"

        android:background="@drawable/radio_button"

<!-- begin snippet: js hide: false -->
&#13;
&#13;
&#13;

我正在使用自定义radio_button.xml

&#13;
&#13;
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/selected_state"
        android:state_checked="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/regular_state"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/regular_state"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/selected_state" />
</selector>
&#13;
&#13;
&#13;

我的selected_state.xml请根据您的使用情况进行修改,添加图片进行播放

&#13;
&#13;
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid
        android:color="#ff0000" />
</shape>
&#13;
&#13;
&#13;

我的regular_state.xml

&#13;
&#13;
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid
        android:color="#880000" />
</shape>
&#13;
&#13;
&#13;