1

我有这个代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);



   final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.ingame_voice_42_);

Button bTutorial1 = (Button) findViewById(R.id.tutorial1); 

bTutorial1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         mpButtonClick.start();

    }
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

如何更改它,从 /raw/ 文件夹播放随机声音。为此更改此代码的简单方法是什么?

请帮帮我!

4

1 回答 1

0
  1. 将声音放在资产 (/assets/mysounds/) 中的单独文件中。
  2. 获取声音列表:

    AssetManager assetManager = this.getAssets();
    String[] sounds= assetManager.list("mysounds");
    
  3. 将您的 MediaPlayer 设置为随机声音:

    final MediaPlayer mpButtonClick = MediaPlayer.create(this, "mysounds/"+sounds[some_random_int]);
    
于 2013-05-17T10:54:21.513 回答