i have 2 mp3 files in the raw folder. I am trying to show the audio files in the radio button.if i click radio button i want to play the audio.and if choose another radio button old audio should stop,new audio should play. This is my code.in my code audio is playing but when i choose another audio app is quitted.
thanks in advance 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();
}
}
};
}
This is my error log
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)
You may customize radio button to look like stop button when not selected and look like play when selected and handle functionality in button click handler. You need to use radio button group for this. Let me share code
I am using custom radio_button.xml
My selected_state.xml Please modify according to your use case, add image for play
My regular_state.xml