lundi 9 février 2015

MediaPlayer in Fragments with two buttons of sounds

We are having an issue with MediaPlayer code in fragments with two buttons in the xml file the manages the layout of the sounds fragment and is hosting the buttons. Can anyone tell me what I'm doing wrong? Thanks


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();
}


}`


Aucun commentaire:

Enregistrer un commentaire