当点击Android上的ImageButton时播放随机声音。

huangapple go评论79阅读模式
英文:

Playing random sound when ImageButton is clicked on Android

问题

我是安卓新手,正在尝试在点击“ImageButton”时播放随机声音。

但每次我按按钮时仍然得到相同的音频。按钮每次被按下时应该播放不同的声音。以后我还想添加一个停止按钮。

这是我的代码:

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;
    ImageButton soundbutton;

    int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
    Random r = new Random();
    int rndm = r.nextInt();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        soundbutton = (ImageButton)this.findViewById(R.id.playButton);
        mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
        soundbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (mp.isPlaying()) {
                        mp.stop();
                        mp.release();
                        rndm = r.nextInt(sounds.length);
                        mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
                    }
                    mp.start();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

谢谢您的帮助。

英文:

I'm new in android and I'm trying to play random sound when ImageButton is clicked.

I'm still getting the same audio when I press the button. It should play different sounds every time the button is pressed. Also I would like to add a stop button later.

Here is my code:

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import java.util.Random;
public
class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton soundbutton;
int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
Random r = new Random();
int rndm = r.nextInt();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//media player
soundbutton = (ImageButton)this.findViewById(R.id.playButton);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt();
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
}
mp.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Thank you for your help.

答案1

得分: 1

你需要充分发挥你的随机函数。你有10个声音,所以你想要一个在0-9之间的数字。

rndm = r.nextInt(10);

这将给你一个在0-9之间的数字。

英文:

You need to give a maximum to your Random. You have 10 sounds so you want a number between 0-9.

rndm = r.nextInt(10); 

This will give you a number between 0-9.

huangapple
  • 本文由 发表于 2020年9月16日 23:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63923420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定