daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Mar 21st, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. final MediaPlayer mp = MediaPlayer.create(this,R.raw.boosto);
  2.  
  3.  
  4. Button button = (Button) this.findViewById(R.id.button);
  5.  
  6. button.setOnClickListener(new View.OnClickListener() {
  7.     @Override
  8.     public void onClick(View v) {
  9.  
  10.  
  11.             mp.start();
  12.  
  13.         }
  14.    
  15. SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
  16.  
  17.  /** soundId for Later handling of sound pool **/
  18.  int soundId = sp.load(context, R.raw.windows_8_notify, 1);
  19.  
  20.  sp.play(soundId, 1, 1, 0, 0, 1);
  21.    
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24.  
  25.    private SoundPool soundPool;
  26.  
  27.    private AudioManager audioManager;
  28.  
  29.    // Maximumn sound stream.
  30.    private static final int MAX_STREAMS = 5;
  31.  
  32.    // Stream type.
  33.    private static final int streamType = AudioManager.STREAM_MUSIC;
  34.  
  35.    private boolean loaded;
  36.  
  37.    private int soundIdDestroy;
  38.    private int soundIdGun;
  39.    private float volume;
  40.  
  41.    @Override
  42.    protected void onCreate(Bundle savedInstanceState) {
  43.        super.onCreate(savedInstanceState);
  44.        setContentView(R.layout.activity_main);
  45.  
  46.        // AudioManager audio settings for adjusting the volume
  47.        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  48.  
  49.        // Current volumn Index of particular stream type.
  50.        float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);
  51.  
  52.        // Get the maximum volume index for a particular stream type.
  53.        float maxVolumeIndex  = (float) audioManager.getStreamMaxVolume(streamType);
  54.  
  55.        // Volumn (0 --> 1)
  56.        this.volume = currentVolumeIndex / maxVolumeIndex;
  57.  
  58.        // Suggests an audio stream whose volume should be changed by
  59.        // the hardware volume controls.
  60.        this.setVolumeControlStream(streamType);
  61.  
  62.        // For Android SDK >= 21
  63.        if (Build.VERSION.SDK_INT >= 21 ) {
  64.  
  65.            AudioAttributes audioAttrib = new AudioAttributes.Builder()
  66.                    .setUsage(AudioAttributes.USAGE_GAME)
  67.                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  68.                    .build();
  69.  
  70.            SoundPool.Builder builder= new SoundPool.Builder();
  71.            builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);
  72.  
  73.            this.soundPool = builder.build();
  74.        }
  75.        // for Android SDK < 21
  76.        else {
  77.            // SoundPool(int maxStreams, int streamType, int srcQuality)
  78.            this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
  79.        }
  80.  
  81.        // When Sound Pool load complete.
  82.        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
  83.            @Override
  84.            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  85.                loaded = true;
  86.            }
  87.        });
  88.  
  89.        // Load sound file (destroy.wav) into SoundPool.
  90.        this.soundIdDestroy = this.soundPool.load(this, R.raw.destroy,1);
  91.  
  92.        // Load sound file (gun.wav) into SoundPool.
  93.        this.soundIdGun = this.soundPool.load(this, R.raw.gun,1);
  94.  
  95.    }
  96.  
  97.  
  98.  
  99.    // When users click on the button "Gun"
  100.    public void playSoundGun(View view)  {
  101.      if(loaded)  {
  102.          float leftVolumn = volume;
  103.          float rightVolumn = volume;
  104.          // Play sound of gunfire. Returns the ID of the new stream.
  105.          int streamId = this.soundPool.play(this.soundIdGun,leftVolumn, rightVolumn, 1, 0, 1f);
  106.      }
  107.    }
  108.  
  109.    // When users click on the button "Destroy"
  110.    public void playSoundDestroy(View view)  {
  111.        if(loaded)  {
  112.            float leftVolumn = volume;
  113.            float rightVolumn = volume;
  114.  
  115.            // Play sound objects destroyed. Returns the ID of the new stream.
  116.            int streamId = this.soundPool.play(this.soundIdDestroy,leftVolumn, rightVolumn, 1, 0, 1f);
  117.        }
  118.    }
  119.  
  120. }
  121.    
  122. public void onClick(View v) {
  123.     if(mp.isPlaying())
  124.         mp.seekTo(0L, MediaPlayer.SEEK_NEXT_SYNC); // continues playback from millisecond 0
  125.     else
  126.         mp.start();
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top