gpt4 book ai didi

java - 在 Android Studio 中重复随机音符

转载 作者:行者123 更新时间:2023-12-02 12:22:20 25 4
gpt4 key购买 nike

我已经制作了一个按钮,按下该按钮后会进入一系列音符并随机选择一个音符,我不知道如何做的是有一个按钮可以在按下时重播该随机音符。我正在考虑使用 rSound 整数,但由于它位于私有(private)空间中,我无法使用它..我应该如何处理这个问题?

代码:

    package com.stefanawesome.piano;

import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

//Declaring variables and array:
ImageButton NoteNew;
ImageButton Repeat;
Button c, d, e, f, g, a, b, high;
List<Integer> soundList = new ArrayList<Integer>();
private SoundPool soundpool;
private int sound_c, sound_d, sound_e, sound_f, sound_g, sound_a, sound_b, sound_high;

//Random Note Function:
private void playRandomSound() {
int randomInt = (new Random().nextInt(soundList.size()));
int rSound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(this, rSound);
mp.start();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

//Find View by ID all here:
soundList.add(R.raw.c);
soundList.add(R.raw.d);
soundList.add(R.raw.e);
soundList.add(R.raw.f);
soundList.add(R.raw.g);
soundList.add(R.raw.a);
soundList.add(R.raw.b);
soundList.add(R.raw.chigh);
NoteNew = (ImageButton) findViewById(R.id.Newnote);
Repeat = (ImageButton) findViewById(R.id.Repeat);
c = (Button) findViewById(R.id.C);
d = (Button) findViewById(R.id.D);
e = (Button) findViewById(R.id.E);
f = (Button) findViewById(R.id.F);
g = (Button) findViewById(R.id.G);
a = (Button) findViewById(R.id.A);
b = (Button) findViewById(R.id.B);
high = (Button) findViewById(R.id.high);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundpool = new SoundPool.Builder().setMaxStreams(5).build();
} else {
soundpool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

}

//Soundpools over here:
sound_c = soundpool.load(this, R.raw.c, 1);
sound_d = soundpool.load(this, R.raw.d, 1);
sound_e = soundpool.load(this, R.raw.e, 1);
sound_f = soundpool.load(this, R.raw.f, 1);
sound_g = soundpool.load(this, R.raw.g, 1);
sound_a = soundpool.load(this, R.raw.a, 1);
sound_b = soundpool.load(this, R.raw.b, 1);
sound_high = soundpool.load(this, R.raw.chigh, 1);


//When button gets clicked do something:
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_c, 1, 1, 0, 0, 1);
}
});

d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_d, 1, 1, 0, 0, 1);
}
});

e.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_e, 1, 1, 0, 0, 1);
}
});

f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_f, 1, 1, 0, 0, 1);
}
});

g.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_g, 1, 1, 0, 0, 1);
}
});

a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_a, 1, 1, 0, 0, 1);
}
});

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_b, 1, 1, 0, 0, 1);
}
});

high.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
soundpool.play(sound_high, 1, 1, 0, 0, 1);
}
});
NoteNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playRandomSound();
}
});


}
}

最佳答案

只需将随机整数(即随机音符的索引)保存在其他变量中即可。
然后,使用该变量重播最后一个随机音符。

将您的函数更新为

int last;

private void playRandomSound() {
int randomInt = (new Random().nextInt(soundList.size()));
last=randomInt;
int rSound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(this, rSound);
mp.start();
}

并添加这个新功能(使用新按钮调用它)

private void playlastRandomSound() {
int rSound = soundList.get(last);
MediaPlayer mp = MediaPlayer.create(this, rSound);
mp.start();
}

关于java - 在 Android Studio 中重复随机音符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674751/

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