当我按下快速播放按钮时,声音播放失败

Posted

技术标签:

【中文标题】当我按下快速播放按钮时,声音播放失败【英文标题】:When I pressed quickly play button, sound play has failure 【发布时间】:2016-09-21 17:55:51 【问题描述】:

我正在开发我的第一个 android 项目。所以我小新手。

我有 3 个用于播放声音的按钮。同时这声音有循环..当按下播放/暂停按钮 (iv) 时一切正常,但是当我快速按下播放/按下按钮时,名为 soundID、soundID2、soundID3 的声音不会暂停,要播放.. 有时所有的声音在同一时间播放.. 我的代码如下。我怎样才能清楚地和稳定地设置这些代码。

package com.bebekuyutucu;

import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity 

    private SoundPool soundPool, soundPool2, soundPool3;
    private int soundID, soundID2, soundID3;
    boolean plays,  loaded, loaded2, loaded3 = false;
    float actVolume, maxVolume, volume;
    AudioManager audioManager;
    int counter,counter2, counter3;
    String voice = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView iv, anne, muzik, supurge, fon, ninni;
        final SeekBar seekBar;
        final TextView tv = (TextView) findViewById(R.id.textView);


        // AudioManager audio settings for adjusting the volume
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        volume = actVolume / maxVolume;

        //Hardware buttons setting to adjust the media sound
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        // the counter will help us recognize the stream id of the sound played  now
        counter = 0;
        counter2 = 0;
        counter3 = 0;

        rekamYukle ();

        // Load the sounds
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
                loaded = true;
            
        );
        soundID = soundPool.load(this, R.raw.fon_sesi,1);

        soundPool2 = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
        soundPool2.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
                loaded2 = true;
            
        );
        soundID2 = soundPool2.load(this, R.raw.annekarni,1);

        soundPool3 = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
        soundPool3.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
                loaded3 = true;
            
        );
        soundID3 = soundPool3.load(this, R.raw.annekarni,1);

        Typeface cocoFont = Typeface.createFromAsset(getAssets(), "fonts/Cocogoose_trial.otf");
        tv.setTypeface(cocoFont);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        iv = (ImageView) findViewById(R.id.imageView3);
        ninni = (ImageView) findViewById(R.id.imageView4);
        anne = (ImageView) findViewById(R.id.imageView5);
        supurge = (ImageView) findViewById(R.id.imageView6);
        fon = (ImageView) findViewById(R.id.imageView7);
        muzik = (ImageView) findViewById(R.id.imageView8);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
                tv.setText(progress + " dk");
            

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) 

            

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) 

            
        );

        anne.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                anne.setImageResource(R.mipmap.annex);
                fon.setImageResource(R.mipmap.fon);
                muzik.setImageResource(R.mipmap.muzik);
                supurge.setImageResource(R.mipmap.supurge);
                ninni.setImageResource(R.mipmap.ninni);
                voice = "annekarni";

                if (voice != "annekarni") 
                    soundPool.stop(soundID);
                    soundPool2.stop(soundID2);
                    soundPool3.stop(soundID3);
                    iv.setImageResource(R.mipmap.butonbaslat);
                

            
        );

        muzik.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                anne.setImageResource(R.mipmap.anne);
                fon.setImageResource(R.mipmap.fon);
                muzik.setImageResource(R.mipmap.muzikx);
                supurge.setImageResource(R.mipmap.supurge);
                ninni.setImageResource(R.mipmap.ninni);
                voice = "muzikkutusu";

                if (voice != "muzikkutusu" ) 
                    soundPool.stop(soundID);
                    soundPool2.stop(soundID2);
                    soundPool3.stop(soundID3);
                    iv.setImageResource(R.mipmap.butonbaslat);
                
            
        );

        supurge.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                muzik.setImageResource(R.mipmap.muzik);
                fon.setImageResource(R.mipmap.fon);
                anne.setImageResource(R.mipmap.anne);
                supurge.setImageResource(R.mipmap.supurgex);
                ninni.setImageResource(R.mipmap.ninni);
                voice = "supurgesesi";

                if (voice != "supurgesesi") 
                    soundPool.stop(soundID);
                    soundPool2.stop(soundID2);
                    soundPool3.stop(soundID3);
                    iv.setImageResource(R.mipmap.butonbaslat);
                
            
        );
        fon.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                anne.setImageResource(R.mipmap.anne);
                fon.setImageResource(R.mipmap.fonx);
                muzik.setImageResource(R.mipmap.muzik);
                supurge.setImageResource(R.mipmap.supurge);
                ninni.setImageResource(R.mipmap.ninni);
                voice = "fonsesi";

                if (voice != "fonsesi") 
                    soundPool.stop(soundID);
                    soundPool2.stop(soundID2);
                    soundPool3.stop(soundID3);
                    iv.setImageResource(R.mipmap.butonbaslat);
                ;
            
        );
        ninni.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                muzik.setImageResource(R.mipmap.muzik);
                fon.setImageResource(R.mipmap.fon);
                anne.setImageResource(R.mipmap.anne);
                supurge.setImageResource(R.mipmap.supurge);
                ninni.setImageResource(R.mipmap.ninnix);
                voice = "ninnisesi";

                if (voice != "ninnisesi") 
                    soundPool.stop(soundID);
                    soundPool2.stop(soundID2);
                    soundPool3.stop(soundID3);
                    iv.setImageResource(R.mipmap.butonbaslat);
                
            
        );

        iv.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (tv.getText().toString().equals("0 dk")) 
                    Toast.makeText(MainActivity.this, "Lütfen sesi seçip, süreyi ayarlayınız.", Toast.LENGTH_SHORT).show();
                 else if (voice == null) 
                    Toast.makeText(MainActivity.this, "Lütfen sesi seçiniz.", Toast.LENGTH_SHORT).show();
                else 
                    try 
                        if (voice.equals("fonsesi"))
                            if (loaded && !plays) 
                                soundPool.play(soundID, volume, volume, 1, seekBar.getProgress() * 12, 1f);
                                counter = counter++;
                                plays = true;
                                iv.setImageResource(R.mipmap.butondurdur);
                            else if (plays) 
                                soundPool.stop(soundID);
                                soundPool2.stop(soundID2);
                                soundPool3.stop(soundID3);
                                soundPool.autoPause();
                                soundPool2.autoPause();
                                soundPool3.autoPause();

                                soundID = soundPool.load(getApplicationContext(), R.raw.fon_sesi, counter);
                                plays = false;
                                iv.setImageResource(R.mipmap.butonbaslat);
                            

                        else if (voice.equals("annekarni")) 
                            if (loaded2 && !plays) 

                                soundPool2.play(soundID2, volume, volume, 1, seekBar.getProgress() * 12, 1f);
                                counter2 = counter2++;
                                plays = true;
                                iv.setImageResource(R.mipmap.butondurdur);
                             else if (plays) 
                                soundPool.stop(soundID);
                                soundPool2.stop(soundID2);
                                soundPool3.stop(soundID3);
                                soundID2 = soundPool2.load(getApplicationContext(), R.raw.annekarni, counter2);
                                plays = false;
                                iv.setImageResource(R.mipmap.butonbaslat);
                            
                        else if (voice.equals("supurgesesi")) 
                            if (loaded3 && !plays) 
                                soundPool3.play(soundID3, volume, volume, 1, seekBar.getProgress() * 12, 1f);
                                counter3 = counter3++;
                                plays = true;
                                iv.setImageResource(R.mipmap.butondurdur);
                             else if (plays) 
                                soundPool.stop(soundID);
                                soundPool2.stop(soundID2);
                                soundPool3.stop(soundID3);
                                soundID3 = soundPool3.load(getApplicationContext(), R.raw.supurge, counter3);
                                plays = false;
                                iv.setImageResource(R.mipmap.butonbaslat);
                            
                        

                        catch (Exception hata)
                     Toast.makeText(MainActivity.this, "Hata :"+hata, Toast.LENGTH_SHORT).show();
                    
                
            
        );
    

    private void rekamYukle() 
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);
    

【问题讨论】:

【参考方案1】:

解决办法:我用soundPool.autoPause();代替soundpool.stop(soundID)

【讨论】:

以上是关于当我按下快速播放按钮时,声音播放失败的主要内容,如果未能解决你的问题,请参考以下文章

按下一个按钮它会发出声音。快速点击按钮和声音播放弹出和点击

按下按钮时循环播放声音

在android soundpool中播放新声音时如何停止所有其他以前的声音?

按下按钮时播放随机声音[重复]

Swift,如何通过按下按钮一次又一次地播放声音

当我按下键盘键时,Windows 可以播放声音吗?