2017-08-14 5 views
0

J'ai déjà créé un bouton qui, lorsqu'il est enfoncé, se place dans un tableau de notes et en choisit un de manière aléatoire. Ce que je ne sais pas faire est d'avoir un bouton qui relance cette note aléatoire chaque fois que pressé. Je pensais utiliser l'entier rSound mais comme c'est dans un vide privé, je ne peux pas l'utiliser. Comment dois-je faire?Répétition d'une note de musique aléatoire dans Android Studio

code:

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(); 
      } 
     }); 


    } 
} 
+0

« rejoue cette note au hasard chaque fois pressé » Vous voulez que le clic initial à un note aléatoire, mais qu'assigner cette note à ce bouton pour tous les clics futurs? –

Répondre

1

Il suffit d'enregistrer le nombre entier aléatoire (qui est l'indice de la note aléatoire) dans une autre variable.
Ensuite, utilisez cette variable pour rejouer la dernière note aléatoire.

Mettez à jour votre fonction

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(); 
     } 

et ajoutez cette nouvelle fonction (appel avec le bouton nouveau)

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