以下是我当前代码的快速问题。它是在Android Studio中创建的:
CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true;
我有4个阵列,应该是声音。我希望能够通过数组更改下拉菜单/微调器的不同位置使用的声音。
我的问题是,当我在OnCreate中创建myMediaPlayer时,我不知道如何更改它。 int current_index被设置为与微调器的位置相同,所以我想根据我的微调器中的菜单编号将它添加到数组的位置。我已经尝试了自己的手,但我对android studio相当缺乏经验,我找到的样本,使用了代码
public class OptionActivity extends Activity {
Spinner spnr;
int songs[] = new int[] {R.raw.sound1,R.raw.sound1,R.raw.sound3,R.raw.sound4};
MediaPlayer myMediaPlayer;
int current_index = 0;
String[] mySelectSounds = {
"Robot",
"Manly man",
"Woman",
"Cat",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
myMediaPlayer = MediaPlayer.create(this, songs[0]);
spnr = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, mySelectSounds);
spnr.setAdapter(adapter);
spnr.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int position = spnr.getSelectedItemPosition();
Toast.makeText(getApplicationContext(), "You have selected " + mySelectSounds[+position], Toast.LENGTH_LONG).show();
current_index = (current_index + position);
if (position == 3) {
myMediaPlayer.start();
//What to put here, to make it change the array number in myMediaPlayer = MediaPlayer.create(this, songs[0]);?
}}
根据这里提出的其他问题似乎有效,但它对我不起作用。
答案 0 :(得分:0)
您需要在项目中单击
创建这样的新MediaPlayer MediaPlayer mp = MediaPlayer.create(OptionActivity.this, songs[current_index]);
答案 1 :(得分:0)
尝试
current_index = position;
if (current_index == 3) {
myMediaPlayer.start();
myMediaPlayer = MediaPlayer.create(this, songs[current_index]);
}}