Я пытался проигрывать музыку через спиннер, и я сделал это.Всякий раз, когда я выбираю пункт на спиннере, должна воспроизводиться выбранная мелодия.Это работает нормально ... Но у меня есть одно большое исключение в моем коде ниже.т. е. во время воспроизведения одной мелодии для выбранного элемента и если я выберу другой элемент, который также воспроизводится с первой мелодией одновременно.как я могу это исправить?Любая идея?
Помощь всегда ценится ....!
Это мой код ...
import java.util.concurrent.Callable;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class SettingsActivity extends Activity {
//FrameLayout maininclude;
RelativeLayout settingspage;
RelativeLayout loginpage;
RelativeLayout registerpage;
RelativeLayout forgotpasswordpage;
MediaPlayer mp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settingspage);
final Button login = (Button) findViewById(R.id.setloginbutton);
final Button register = (Button) findViewById(R.id.setregisterbutton);
final Button forgotpassword1 = (Button) findViewById(R.id.setforgotpasswordbutton);
final Spinner tunes = (Spinner) findViewById(R.id.tunesspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.tunes, R.layout.spinnerlayout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tunes.setAdapter(adapter);
tunes.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
if(position == 0) {
//First Entry
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.dumy);
mp.start();
}
if(position == 1) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.akko_echo);
mp.start();
}
if(position == 2) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.crystal_cling);
mp.start();
}
if(position == 3) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.glocken_jing);
reset();
mp.start();
}
if(position == 4) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.bang_drum);
reset();
mp.start();
}
if(position == 5) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.grand_piano);
reset();
mp.start();
}
if(position == 6) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.hard_rock);
reset();
mp.start();
}
if(position == 7) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.sonic_bloom);
mp.start();
}
if(position == 8) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.bass_boost);
reset();
mp.start();
}
if(position == 9) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.spooky_freaky);
reset();
mp.start();
}
if(position == 10) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.akko_echo);
reset();
mp.start();
}
if(position == 11) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.shan_zung);
reset();
mp.start();
}
if(position == 12) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.yo_drum);
reset();
mp.start();
}
}
private void reset() {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//maininclude = (FrameLayout) findViewById(R.id.mainlayout);
//settingspage = (RelativeLayout) findViewById(R.id.settingsxml);
// loginpage = (RelativeLayout) findViewById(R.id.loginxml);
//registerpage = (RelativeLayout) findViewById(R.id.registrationxml);
//forgotpasswordpage = (RelativeLayout) findViewById(R.id.forgotpasswordxml);
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(view.getContext(),
LoginActivity.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.fadein, 0);
// settingspage.setVisibility(View.GONE);
//registerpage.setVisibility(View.GONE);
//forgotpasswordpage.setVisibility(View.GONE);
//loginpage.setVisibility(View.VISIBLE);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(view.getContext(),
RegisterActivity.class);
startActivityForResult(myIntent, 1);
overridePendingTransition(R.anim.fadein, 0);
}
});
forgotpassword1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(view.getContext(),
ForgotPasswordActivity.class);
startActivityForResult(myIntent, 2);
overridePendingTransition(R.anim.fadein, 0);
}
});
}
}