soundPool sound pool

soundPool can play multiple sounds at the same time, it is best not to file more than 1M, the sound can only play for a few seconds, mixing sound for games

public  class the MainActivity the extends AppCompatActivity { 


    @Override 
    protected  void the onCreate (the Bundle savedInstanceState) {
         Super .onCreate (savedInstanceState); 

        SoundPool Soundpool; 
        // instantiate SoundPool 

        // SDK version 21 is a watershed SoundPool 
        IF (Build.VERSION.SDK_INT> = 21 is ) { 
            SoundPool.Builder Builder = new new SoundPool.Builder ();
             // passed up to the number of audio playback, 
            builder.setMaxStreams (. 1 );
             // AudioAttributes various audio properties is a packaging method
            AttrBuilder = AudioAttributes.Builder new new AudioAttributes.Builder ();
             // set the appropriate audio stream attribute 
            attrBuilder.setLegacyStreamType (AudioManager.STREAM_MUSIC);
             // loading a AudioAttributes 
            builder.setAudioAttributes (attrBuilder.build ()); 
            Soundpool = Builder. Build (); 
        } the else {
             / ** 
             * the first parameter: int maxStreams: maximum number of concurrent streams SoundPool object 
             * the second parameter: int streamType: AudioManager audio stream type is described in 
             * the third parameter: int srcQuality : mass filter sample rate conversion. There is no effect. Use 0 as the default value. 
             * / 
            Soundpool = new newSoundPool (. 1, AudioManager.STREAM_MUSIC, 0 ); 
        } 

        // can be described in four ways an audio resource:
         // 1. AssetFileDescriptor a target
         // int Load (AssetFileDescriptor AFD, int priority)
         // 2. by a resource ID
         // int load (the context context, resId int, int priority)
         // 3. loaded by specifying a path
         // int load (String path, int priority)
         // 4. FileDescriptor by loading
         // int load (FD FileDescriptor , offset Long, Long length, int priority)
         // sound audio resource ID to load, here is the second, and the third parameter is the priority, the priority of voice * API pointed out, priority parameters currently no results, recommend setting 1. 
        Final  int= soundPool.load VoiceID ( the this , R.raw.qunianxiatian, 1 );
         // asynchronous need to wait for loading is complete, the audio can be played successfully 
        soundPool.setOnLoadCompleteListener ( new new SoundPool.OnLoadCompleteListener () { 
            @Override 
            public  void onLoadComplete (SoundPool Soundpool, int sampleId, int Status) {
                 IF (Status == 0 ) {
                     // first parameter SoundID
                     // second parameter is left leftVolume volume value (range = 0.0 to 1.0)
                     // third parameter is the right rightVolume volume value (range = 0.0 to 1.0)
                     //The fourth parameter priority for the stream of priority, the greater the value of high priority, the impact when the number of simultaneously playing out of the flow of processing SoundPool the maximum number of support
                     // The fifth parameter loop to repeat the audio frequency 0 a play value, -1 infinite loop, the other loop + 1 times play value
                     // sixth parameter playback rate of speed, the range 0.5-2.0 (half the rate of 0.5, 1.0 of the normal rate, twice the rate of 2.0 ) 
                    soundPool.play (VoiceID,. 1,. 1,. 1, 0,. 1 ); 
                } 
            } 
        }); 
    } 
    }

 

Guess you like

Origin www.cnblogs.com/Ocean123123/p/10991403.html