我正在制作一个播放传统乐器的应用程序。用户必须按照歌曲音符,并且在完成播放歌曲之后将计算标记。问题是如何计算标记,因为歌曲音符非常长。
我正在使用 soundPool 来制作声音。
这是我的应用和编码的屏幕截图:
帮我完成我的最后一年项目。
public class tutorial3扩展了AppCompatActivity {
public Button checkmark;
public static final int ROWS = 2;
public static final int COLS = 5;
public int count = 0;
public int correct = 0;
public boolean a,b,c,d,e;
private SoundPool soundPool;
private AudioManager audioManager;
// Maximumn sound stream.
private static final int MAX_STREAMS = 5;
// Stream type.
private static final int streamType = AudioManager.STREAM_MUSIC;
private boolean loaded;
private int soundId1k;
private int soundId2k;
private int soundId3k;
private int soundId5k;
private int soundId6k;
private int soundId1b;
private int soundId2b;
private int soundId3b;
private int soundId5b;
private int soundId6b;
private float volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial3);
//int count = 0;
// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// Current volumn Index of particular stream type.
float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);
// Get the maximum volume index for a particular stream type.
float maxVolumeIndex = (float) audioManager.getStreamMaxVolume(streamType);
// Volumn (0 --> 1)
this.volume = currentVolumeIndex / maxVolumeIndex;
// Suggests an audio stream whose volume should be changed by
// the hardware volume controls.
this.setVolumeControlStream(streamType);
// For Android SDK >= 21
if (Build.VERSION.SDK_INT >= 21) {
AudioAttributes audioAttrib = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
SoundPool.Builder builder = new SoundPool.Builder();
builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);
this.soundPool = builder.build();
}
// for Android SDK < 21
else {
// SoundPool(int maxStreams, int streamType, int srcQuality)
this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
}
// When Sound Pool load complete.
this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
// Load sound file (destroy.wav) into SoundPool.
this.soundId1k = this.soundPool.load(this, R.raw.one_kecik, 1);
// Load sound file (gun.wav) into SoundPool.
this.soundId2k = this.soundPool.load(this, R.raw.two_kecik, 1);
this.soundId3k = this.soundPool.load(this, R.raw.three_kecik, 1);
this.soundId5k = this.soundPool.load(this, R.raw.five_kecik, 1);
this.soundId6k = this.soundPool.load(this, R.raw.six_kecik, 1);
this.soundId6b = this.soundPool.load(this, R.raw.six_besar, 1);
this.soundId5b = this.soundPool.load(this, R.raw.five_besar, 1);
this.soundId3b = this.soundPool.load(this, R.raw.three_besar, 1);
this.soundId2b = this.soundPool.load(this, R.raw.two_besar, 1);
this.soundId1b = this.soundPool.load(this, R.raw.one_besar, 1);
ImageButton btnCells[][] = new ImageButton[ROWS][COLS];
btnCells[0][0] =(ImageButton)findViewById(R.id.btn00);
btnCells[0][1] =(ImageButton)findViewById(R.id.btn01);
btnCells[0][2] =(ImageButton)findViewById(R.id.btn02);
btnCells[0][3] =(ImageButton)findViewById(R.id.btn03);
btnCells[0][4] =(ImageButton)findViewById(R.id.btn04);
btnCells[1][0] =(ImageButton)findViewById(R.id.btn10);
btnCells[1][1] =(ImageButton)findViewById(R.id.btn11);
btnCells[1][2] =(ImageButton)findViewById(R.id.btn12);
btnCells[1][3] =(ImageButton)findViewById(R.id.btn13);
btnCells[1][4] =(ImageButton)findViewById(R.id.btn14);
boolean push[][] = new boolean[ROWS][COLS];
push[1][2] = true;
push[0][2] = true;
push[1][1] = true;
push[0][3] = true;
push[1][0] = true;
push[0][4] = true;
push[1][0] = true;
push[0][4] = true;
push[1][1] = true;
push[0][3] = true;
push[1][2] = true;
push[0][2] = true;
push[1][0] = true;
push[0][4] = true;
for (int i=0; i<2; i++)
{
for (int j=0; j<5; j++)
{
if (!push[i][j])
{
count++;
}
}
}
correct = 14 - count;
}
// When users click on the image button
public void playSound1k(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound objects destroyed. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId1k, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
// When users click on the button "Gun"
public void playSound2k(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId2k, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound3k(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId3k, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound5k(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId5k, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound6k(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId6k, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound6b(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId6b, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound5b(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId5b, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound3b(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId3b, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound2b(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId2b, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void playSound1b(View view) {
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
int streamId = this.soundPool.play(this.soundId1b, leftVolumn, rightVolumn, 1, 0, 1f);
}
}
public void chkmarks(View view) {
checkmark = (Button) findViewById(R.id.chkmark);
checkmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder altdial = new AlertDialog.Builder(tutorial3.this);
altdial.setMessage("you scored " + correct + " out of 14!").setCancelable(true)
.setNegativeButton("play now!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(tutorial3.this, play.class));
}
})
.setPositiveButton("home", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog.cancel();
startActivity(new Intent(tutorial3.this, menu.class));
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Marks");
alert.show();
}
});
}
}
答案 0 :(得分:0)
我找到了解决方案。这是
public Button checkmark;
public int count1;
public int sum1;
public int correct;
private SoundPool soundPool;
private AudioManager audioManager;
// Maximumn sound stream.
private static final int MAX_STREAMS = 5;
// Stream type.
private static final int streamType = AudioManager.STREAM_MUSIC;
private boolean loaded;
int streamIdd;
private int soundId1k;
private int soundId2k;
private int soundId3k;
private int soundId5k;
private int soundId6k;
private int soundId1b;
private int soundId2b;
private int soundId3b;
private int soundId5b;
private int soundId6b;
private float volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial3);
// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// Current volumn Index of particular stream type.
float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);
// Get the maximum volume index for a particular stream type.
float maxVolumeIndex = (float) audioManager.getStreamMaxVolume(streamType);
// Volumn (0 --> 1)
//this.volume = currentVolumeIndex / maxVolumeIndex;
volume = currentVolumeIndex / maxVolumeIndex;
// Suggests an audio stream whose volume should be changed by
// the hardware volume controls.
//this.setVolumeControlStream(streamType);
setVolumeControlStream(streamType);
// For Android SDK >= 21
if (Build.VERSION.SDK_INT >= 21) {
AudioAttributes audioAttrib = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
SoundPool.Builder builder = new SoundPool.Builder();
builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);
this.soundPool = builder.build();
}
// for Android SDK < 21
else {
// SoundPool(int maxStreams, int streamType, int srcQuality)
this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
}
// When Sound Pool load complete.
this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
// Load sound file (destroy.wav) into SoundPool.
soundId1k = this.soundPool.load(this, R.raw.one_kecik, 1);
// Load sound file (gun.wav) into SoundPool.
soundId2k = this.soundPool.load(this, R.raw.two_kecik, 1);
soundId3k = this.soundPool.load(this, R.raw.three_kecik, 1);
soundId5k = this.soundPool.load(this, R.raw.five_kecik, 1);
soundId6k = this.soundPool.load(this, R.raw.six_kecik, 1);
soundId6b = this.soundPool.load(this, R.raw.six_besar, 1);
soundId5b = this.soundPool.load(this, R.raw.five_besar, 1);
soundId3b = this.soundPool.load(this, R.raw.three_besar, 1);
soundId2b = this.soundPool.load(this, R.raw.two_besar, 1);
soundId1b = this.soundPool.load(this, R.raw.one_besar, 1);
final ImageButton btnCells[] = new ImageButton[14];
int refid[] = {R.id.btn12, R.id.btn02,R.id.btn11,R.id.btn03,R.id.btn10,R.id.btn04,R.id.btn10,R.id.btn04,R.id.btn11,R.id.btn03,R.id.btn12,R.id.btn02,R.id.btn10,R.id.btn04};
for (int j = 0; j < refid.length; j++) {
final int b = j;
btnCells[b] = (ImageButton) findViewById(refid[b]); // Fetch the view id from array
//final int finalJ = j;
//View.OnClickListener listener1 = new View.OnClickListener() {
btnCells[b].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(btnCells[0])) {
count1 = 1;
returnsound3k();
}
if (v.equals(btnCells[1])) {
count1 = 1;
returnsound3b();
}
if (v.equals(btnCells[2]))
{
count1 = 1;
returnsound2k();
}
if (v.equals(btnCells[3])){
count1 = 1;
returnsound2b();
}
if (v.equals(btnCells[4]))
{
count1 = 1;
returnsound1k();
}
if (v.equals(btnCells[5]))
{
count1 = 1;
returnsound1b();
}
if (v.equals(btnCells[6]))
{
count1 = 1;
returnsound1k();
}
if (v.equals(btnCells[7]))
{
count1 = 1;
returnsound1b();
}
if (v.equals(btnCells[8])) {
count1 = 1;
returnsound2k();
}
if (v.equals(btnCells[9])) {
count1 = 1;
returnsound2b();
}
if (v.equals(btnCells[10]))
{
count1 = 1;
returnsound3k();
}
if (v.equals(btnCells[11])){
count1 = 1;
returnsound3b();
}
if (v.equals(btnCells[12]))
{
count1 = 1;
returnsound1k();
}
if (v.equals(btnCells[13]))
{
count1 = 1;
returnsound1b();
}
sum1 += count1;
correct = sum1;
}
});
}
}
public int returnsound1k ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId1k, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound2k ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId2k, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound3k ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId3k, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound5k ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId5k, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound6k ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId6k, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound6b ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId6b, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound5b ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId5b, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound3b ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId3b, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound2b ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId2b, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public int returnsound1b ()
{
if (loaded) {
float leftVolumn = volume;
float rightVolumn = volume;
// Play sound of gunfire. Returns the ID of the new stream.
streamIdd = soundPool.play(soundId1b, leftVolumn, rightVolumn, 1, 0, 1f);
}
return streamIdd;
}
public void chkmarks(View view) {
checkmark = (Button) findViewById(R.id.chkmark);
checkmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder altdial = new AlertDialog.Builder(tutorial3.this);
altdial.setMessage("you scored " + correct + " out of 14!").setCancelable(true)
.setNegativeButton("play now!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(tutorial3.this, play.class));
}
})
.setPositiveButton("home", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog.cancel();
startActivity(new Intent(tutorial3.this, menu.class));
}
});
AlertDialog alert = altdial.create();
alert.setTitle("Marks");
alert.show();
}
});
}
}