I'm having an issue with MediaPlayer code in fragments. Logcat is suggesting it's down to the OnClickListener in this passage of code within the SoundsFragment.java class.
Can anyone tell me what I'm doing wrong?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_sounds);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button1=(Button)findViewById(R.id.button_1);
Button button2=(Button)findViewById(R.id.button_2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
SoundsFragment.java file which hosts the mediaplayer code that was recently inserted and has causes these issues.
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SoundsFragment extends Fragment implements OnClickListener{
public SoundsFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sounds, container, false);
return rootView;
}
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_sounds);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button1=(Button)findViewById(R.id.button_1);
Button button2=(Button)findViewById(R.id.button_2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
private Button findViewById(int button1) {
// TODO Auto-generated method stub
return null;
}
private void setVolumeControlStream(int streamMusic) {
// TODO Auto-generated method stub
}
private void setContentView(int activityMain) {
// TODO Auto-generated method stub
}
public void onClick(View v) {
int resId;
switch (v.getId()) {
case R.id.button_1:
resId = R.raw.a;
break;
case R.id.button_2:
resId = R.raw.b;
break;
default:
resId = R.raw.a;
break;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(getActivity(), resId);
mp.start();
}
@Override
public void onDestroy() {
if(null!=mp){
mp.release();
}
super.onDestroy();
}
}
fragment_sound.xml This is the xml file the manages the layout of the sounds fragment and is hosting the buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/sounds_fragment">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:layout_marginTop="36dp"
android:text="Play audio a.mp3" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button_1"
android:layout_below="@+id/button_1"
android:text="Play audio b.mp3" />
</RelativeLayout>