随机声音onClick Android Media Player

时间:2013-05-17 10:47:17

标签: android audio onclick

我有这段代码:

@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 /文件夹中播放随机声音。 是一个简单的方法来改变这个代码吗?

请帮帮我!

1 个答案:

答案 0 :(得分: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]);