如何禁用和启用布局根据Android的播放声音 [英] How to disable and enable layout according to sound play in android

查看:235
本文介绍了如何禁用和启用布局根据Android的播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我打声若用​​户preSS上的布局。但问题是,如果用户preSS布局连连。声音一次又一次地重复。我想成为布局如果禁用播放声音,使声音的时候完成。使用户能够再次播放声音时,它的完成。

code -

 公共类请分享帮助扩展活动实现OnTouchListener {    私人的Soundpool的Soundpool;
    私人诠释soundID;
    布尔加载= FALSE;
    私人的LinearLayout布局1;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.boy);
        布局1 =(的LinearLayout)findViewById(R.id.layout1);
        layout1.setOnTouchListener(本);
        //设置硬件按钮来控制音乐
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        //加载声音
        的Soundpool =新的Soundpool(10,AudioManager.STREAM_MUSIC,0);
        soundPool.setOnLoadCompleteListener(新OnLoadCompleteListener(){
            @覆盖
            公共无效的onLoadComplete(的Soundpool的Soundpool,INT sampleId,
                    INT状态){
                加载=真实的;
            }
        });
        soundID = soundPool.load(在此,R.raw.test,1);    }    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        如果(event.getAction()== MotionEvent.ACTION_DOWN){
            //获取用户的声音设置
            AudioManager audioManager =(AudioManager)getSystemService(AUDIO_SERVICE);
            浮动actualVolume =(浮点)audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            浮动maxVolume =(浮点)audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            浮动量= actualVolume / maxVolume;
            //声音是装了吗?
            如果(装入){
                soundPool.play(soundID,体积,容量,1,0,1F);
                Log.e(测试,播放声音);
            }
        }
        返回false;
    }


解决方案

维持一个布尔作为播放声音时或不...,并基于该禁用或启用布局如下...

 如果(isSoundPlaying){
    layout1.setEnabled(假);
}其他{
    layout1.setEnabled(真);
}

更新:

在声音开始播放,然后写的 layout1.setEnabled(假); 将禁用布局。据我了解 soundPool.play(soundID,体积,容量,1,0,1F); 启动声音铺设然后写...

  soundPool.play(soundID,体积,容量,1,0,1F);
layout1.setEnabled(假);

而当声音播放停止然后写这将使布局...

  layout1.setEnabled(真);

In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish. so that user able to play sound again when its finish.

Code-

public class Boy1 extends Activity implements OnTouchListener {

    private SoundPool soundPool;
    private int soundID;
    boolean loaded = false;
    private LinearLayout layout1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boy);
        layout1 = (LinearLayout) findViewById (R.id.layout1);
        layout1.setOnTouchListener(this);
        // Set the hardware buttons to control the music
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        // Load the sound
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                loaded = true;
            }
        });
        soundID = soundPool.load(this, R.raw.test, 1);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // Getting the user sound settings
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float actualVolume = (float) audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            float maxVolume = (float) audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float volume = actualVolume / maxVolume;
            // Is the sound loaded already?
            if (loaded) {
                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                Log.e("Test", "Played sound");
            }
        }
        return false;
    }

解决方案

Maintain an boolean for sound is playing or not...and based on that disable or enable your Layout as follows...

if (isSoundPlaying) {
    layout1.setEnabled(false);
} else {
    layout1.setEnabled(true);
}

Update:

when sound start playing then write layout1.setEnabled(false); it will disable the layout. as I understand soundPool.play(soundID, volume, volume, 1, 0, 1f); start the sound laying then write...

soundPool.play(soundID, volume, volume, 1, 0, 1f);
layout1.setEnabled(false);

And when sound playing stops then write which will enable the layout...

layout1.setEnabled(true);

这篇关于如何禁用和启用布局根据Android的播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆