Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机声音onClick Android Medaplayer_Android_Audio_Onclick - Fatal编程技术网

随机声音onClick Android Medaplayer

随机声音onClick Android Medaplayer,android,audio,onclick,Android,Audio,Onclick,我有这个密码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

我有这个密码:

@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/文件夹播放随机声音。 是否有一个简单的方法来更改此代码

请帮帮我

  • 将声音放在assets(/assets/mysounds/)中的单独文件中
  • 获取声音列表:

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

    final MediaPlayer mpButtonClick = MediaPlayer.create(this, "mysounds/"+sounds[some_random_int]);
    

  • 我必须替换为:mContext?我有一个错误:无法解析McContext更改为“this”?好的,但现在我得到了:未处理的异常类型IOExceptionOni不能给你一个日志,因为我不能运行它。我必须在:[code]final MediaPlayer mpButtonClick=MediaPlayer.create中更改什么(这个“mysounds/”+声音[一些随机的]);[/code]这里例外:+听起来[有些随机];因为我有这些错误:声音无法解析为变量,一些随机整数无法解析为变量抱歉,我是新手。那你是怎么得到IOException的?!!你应该用random int替换
    some_random_int
    。我把它作为一个占位符。例如:final MediaPlayer mpButtonClick=MediaPlayer。创建这个“mysounds/”+sounds[sound1,sound2])??我在create中遇到了这个错误:MediaPlayer类型中的create(Context,Uri)方法不适用于参数(MainActivity,String)