gpt4 book ai didi

android - 从 onClick 和 onShake 事件中随机生成声音?

转载 作者:行者123 更新时间:2023-11-29 16:27:10 26 4
gpt4 key购买 nike

我在网上到处找,发现很少说明如何做到这一点。差不多,我在 array.xml 文件中放置了 8 个声音文件,我需要每次或 onClick 或 onShake 播放一次随机选择的文件。首先,我应该使用什么技术来实现这一目标?数组->随机-

STRING->PLAY? RANDOM INT->PLAY? RANDOM INT->STRING->PLAY? Any kind

方向将有很大帮助,因为我将近 3 周的时间对此进行研究。

*注意: MediaPlayer mp = MediaPlayer.create(JelloMan.this, R.原始声音)...我坚持认为您不能用字符串替换“R.raw”部分...

这是完整的代码。

package com.cyphasignals.jelloman;

import java.util.Random;

import android.app.Activity;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class JelloMan extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

private final int NUM_SOUND_FILES = 3;
//Modifier invalid here
private int mfile[] = new mfile[NUM_SOUND_FILES];
//Modifier invalid here and SECOND "mfile" is wanting to create a class
private Random rnd = new Random(3);
//Modifier invalid here
mfile[0] = R.raw.sound1;
mfile[1] = R.raw.sound2;
mfile[2] = R.raw.sound3;
int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);

ShakeListener MyShake = new ShakeListener((SensorManager)
getSystemService(SENSOR_SERVICE));
MyShake.setForceThreshHold(4.0);
MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {

MediaPlayer mp = MediaPlayer.create(JelloMan.this, mfile[sndToPlay]);
//[sndToPlay] wants me to change the modifier
public void onShake() {
mp.seekTo(0);
mp.start();
}
});
ImageButton mouthbutton = (ImageButton)findViewById(R.id.billmouth);
mouthbutton.setOnClickListener(new OnClickListener() {

MediaPlayer mp = MediaPlayer.create(JelloMan.this,
mfile[sndToPlay]);
//[sndToPlay] wants me to change the modifier
public void onClick(View v) {
mp.seekTo(0);
mp.start();
}
});

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
};
return false;
}

}

最佳答案

半伪代码:

private final int NUM_SOUND_FILES = 3;

private int mSndFiles[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random(); //import java.util.Random for this

mSndFiles[0] = R.raw.sound1;
mSndFiles[1] = R.raw.sound2;
mSndFiles[2] = R.raw.sound3;

int sndToPlay = rnd.nextInt(NUM_SOUND_FILES);

MediaPlayer mp = MediaPlayer.create(JelloMan.this, mSndFiles[sndToPlay]);

如果您拥有的所有声音文件都很小并且您想要低延迟,请考虑使用 SoundPool 而不是 MediaPlayer。

编辑:我并不是要您将上面的代码复制并粘贴到您的应用程序中,我假设您会把东西放在正确的位置。无论如何,试试这个,注意我在代码中的评论。我没有对此进行测试,并假设您还在其他地方定义了“ShakeListener”类,但这应该有效。

package com.cyphasignals.jelloman;

import java.util.Random;
import android.app.Activity;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.cyphasignals.R;

public class JelloMan extends Activity {

private final int NUM_SOUND_FILES = 3; //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****

private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
private MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mfile[0] = R.raw.sound1; //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
mfile[1] = R.raw.sound2; //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
mfile[2] = R.raw.sound3;

ShakeListener MyShake = new ShakeListener((SensorManager.getSystemService(SENSOR_SERVICE));
MyShake.setForceThreshHold(4.0);
MyShake.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
mp = MediaPlayer.create(JelloMan.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
mp.seekTo(0);
mp.start();
}});

ImageButton mouthbutton = (ImageButton)findViewById(R.id.billmouth);
mouthbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp = MediaPlayer.create(JelloMan.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
mp.seekTo(0);
mp.start();
}});

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return false;
}
}

从结构上讲,您需要考虑如果有人连续摇动设备,它是如何工作的。就像现在一样,它会不断跳回到声音的开头。

关于android - 从 onClick 和 onShake 事件中随机生成声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2807879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com