我使用Android Studio制作了一个音板,播放了AoE II的39首mp3曲目。一切都很好,除了其中只有30个玩的事实。在构建之间,不工作的9个不同。我创建了太多的MediaPlayers吗?或者还有别的错误吗?
---------- MainActiviy -------
package com."package_name_here";
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySound1;
MediaPlayer mySound2;
MediaPlayer mySound3;
MediaPlayer mySound4;
MediaPlayer mySound5;
MediaPlayer mySound6;
MediaPlayer mySound7;
MediaPlayer mySound8;
MediaPlayer mySound9;
MediaPlayer mySound10;
MediaPlayer mySound11;
MediaPlayer mySound12;
MediaPlayer mySound13;
MediaPlayer mySound14;
MediaPlayer mySound15;
MediaPlayer mySound16;
MediaPlayer mySound17;
MediaPlayer mySound18;
MediaPlayer mySound19;
MediaPlayer mySound20;
MediaPlayer mySound21;
MediaPlayer mySound22;
MediaPlayer mySound23;
MediaPlayer mySound24;
MediaPlayer mySound25;
MediaPlayer mySound26;
MediaPlayer mySound27;
MediaPlayer mySound28;
MediaPlayer mySound29;
MediaPlayer mySound30;
MediaPlayer mySound31;
MediaPlayer mySound32;
MediaPlayer mySound33;
MediaPlayer mySound34;
MediaPlayer mySound35;
MediaPlayer mySound36;
MediaPlayer mySound37;
MediaPlayer mySound38;
MediaPlayer mySound39;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound1 = MediaPlayer.create(this,R.raw.ahh);
mySound2 = MediaPlayer.create(this,R.raw.all_hail);
mySound3 = MediaPlayer.create(this,R.raw.attack_an_enemy_now);
mySound4 = MediaPlayer.create(this,R.raw.back_to_age_1);
mySound5 = MediaPlayer.create(this,R.raw.being_rushed);
mySound6 = MediaPlayer.create(this,R.raw.blame_your_isp);
mySound7 = MediaPlayer.create(this,R.raw.build_a_navy);
mySound8 = MediaPlayer.create(this,R.raw.build_a_wonder);
mySound9 = MediaPlayer.create(this,R.raw.cease_creating_extra_villagers);
mySound10 = MediaPlayer.create(this,R.raw.create_extra_villagers);
mySound11 = MediaPlayer.create(this,R.raw.dadgum);
mySound12 = MediaPlayer.create(this,R.raw.dont_point_that_thing);
mySound13 = MediaPlayer.create(this,R.raw.enemy_sighted);
mySound14 = MediaPlayer.create(this,R.raw.food_please);
mySound15 = MediaPlayer.create(this,R.raw.give_me_your_extra_resources);
mySound16 = MediaPlayer.create(this,R.raw.gold_please);
mySound17 = MediaPlayer.create(this,R.raw.herb_laugh);
mySound18 = MediaPlayer.create(this,R.raw.i_need_a_monk);
mySound19 = MediaPlayer.create(this,R.raw.it_is_good);
mySound20 = MediaPlayer.create(this,R.raw.long_time_no_siege);
mySound21 = MediaPlayer.create(this,R.raw.my_granny);
mySound22 = MediaPlayer.create(this,R.raw.nice_town_ill_take_it);
mySound23 = MediaPlayer.create(this,R.raw.no);
mySound24 = MediaPlayer.create(this,R.raw.oooh);
mySound25 = MediaPlayer.create(this,R.raw.quit_touchin);
mySound26 = MediaPlayer.create(this,R.raw.raiding_party);
mySound27 = MediaPlayer.create(this,R.raw.roggan);
mySound28 = MediaPlayer.create(this,R.raw.smite_me);
mySound29 = MediaPlayer.create(this,R.raw.start_the_game);
mySound30 = MediaPlayer.create(this,R.raw.stone_please);
mySound31 = MediaPlayer.create(this,R.raw.stop_building_a_navy);
mySound32 = MediaPlayer.create(this,R.raw.the_wonder);
mySound33 = MediaPlayer.create(this,R.raw.wait_for_my_signal);
mySound34 = MediaPlayer.create(this,R.raw.what_age_are_you_in);
mySound35 = MediaPlayer.create(this,R.raw.wololo);
mySound36 = MediaPlayer.create(this,R.raw.wood_please);
mySound37 = MediaPlayer.create(this,R.raw.yes);
mySound38 = MediaPlayer.create(this,R.raw.you_play_2_hours);
mySound39 = MediaPlayer.create(this,R.raw.you_should_see);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void playahh(View view) {mySound1.start();}
public void playall_hail(View view) {mySound2.start();}
public void playattack_an_enemy_now(View view) {mySound3.start();}
public void playback_to_age_1(View view) {mySound4.start();}
public void playbeing_rushed(View view) {mySound5.start();}
public void playblame_your_isp(View view) {mySound6.start();}
public void playbuild_a_navy(View view) {mySound7.start();}
public void playbuild_a_wonder(View view) {mySound8.start();}
public void playcease_creating_extra_villagers(View view) {mySound9.start();}
public void playcreate_extra_villagers(View view) {mySound10.start();}
public void playdadgum(View view) {mySound11.start();}
public void playdont_point_that_thing(View view) {mySound12.start();}
public void playenemy_sighted(View view) {mySound13.start();}
public void playfood_please(View view) {mySound14.start();}
public void playgive_me_your_extra_resources(View view) {mySound15.start();}
public void playgold_please(View view) {mySound16.start();}
public void playherb_laugh(View view) {mySound17.start();}
public void playi_need_a_monk(View view) {mySound18.start();}
public void playit_is_good(View view) {mySound19.start();}
public void playlong_time_no_siege(View view) {mySound20.start();}
public void playmy_granny(View view) {mySound21.start();}
public void playnice_town_ill_take_it(View view) {mySound22.start();}
public void playno(View view) {mySound23.start();}
public void playoooh(View view) {mySound24.start();}
public void playquit_touchin(View view) {mySound25.start();}
public void playraiding_party(View view) {mySound26.start();}
public void playroggan(View view) {mySound27.start();}
public void playsmite_me(View view) {mySound28.start();}
public void playstart_the_game(View view) {mySound29.start();}
public void playstone_please(View view) {mySound30.start();}
public void playstop_building_a_navy(View view) {mySound31.start();}
public void playthe_wonder(View view) {mySound32.start();}
public void playwait_for_my_signal(View view) {mySound33.start();}
public void playwhat_age_are_you_in(View view) {mySound34.start();}
public void playwololo(View view) {mySound35.start();}
public void playwood_please(View view) {mySound36.start();}
public void playyes(View view) {mySound37.start();}
public void playyou_play_2_hours(View view) {mySound38.start();}
public void playyou_should_see(View view) {mySound39.start();}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
--------- XML -------------
我有39个按钮,但它们不是我认为的问题。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ahh"
android:onClick="playahh">
</Button>
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="playall_hail"
android:text="@string/all_hail">
</Button>
</LinearLayout>
上面的内容,重复20次,共40个按钮,1个为空白。