簡體   English   中英

如果單擊另一個單擊按鈕聲音開始,則設置聲音停止

[英]set sound stop if click another click button sound are start

首先,只想讓您知道我在這個網站上關注了很長時間,所以我總是喜歡在這里閱讀並在這里看到聰明的人:)。

我的新應用程序項目(為我的孩子們建造)有問題,我希望somone可以幫助我。

在我的應用程序上,我有9個不同的按鈕。 對於任何按鈕,我都有不同的聲音。

如果您按下“鳥按鈕”,那么您會聽到鳥叫聲,如果您按下“狗按鈕”,您將聽到狗叫聲……我為所有九個按鈕設置了聲音,當我按下其中一個按鈕時,我可以聽到我想要的聲音。

但是(!),如果我按下其中一個按鈕,然后又緊緊地按另一個按鈕,我會一起聽到聲音(例如,如果我按下狗按鈕,然后在按下貓按鈕之后一秒鍾,我會聽到狗還有貓的聲音。)

喬斯特·溫德林(Jost Windring)如果您能幫助我,請進行設置。.如果我按一下,我會聽到按鍵聲音。 但是,如果我按另一個按鈕,以前的聲音將停止(而不是暫停-停止),並且我只能聽到我按的最后一個語音按鈕。 公共類動物擴展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_animals );

    final MediaPlayer birdMP = MediaPlayer.create(this,R.raw.bird);
    final MediaPlayer catMP = MediaPlayer.create(this,R.raw.cat);
    final MediaPlayer chickenMP = MediaPlayer.create(this,R.raw.chicken);
    final MediaPlayer cowMP = MediaPlayer.create(this,R.raw.cow);
    final MediaPlayer dogMP = MediaPlayer.create(this,R.raw.dog);
    final MediaPlayer elephentMP = MediaPlayer.create(this,R.raw.elephent);
    final MediaPlayer horseMP = MediaPlayer.create(this,R.raw.horse);
    final MediaPlayer sheepMP = MediaPlayer.create(this,R.raw.sheep);
    final MediaPlayer wolfMP = MediaPlayer.create(this,R.raw.wolf);



    final Button Btnbird = (Button)this.findViewById( R.id.Btnbird );
    final Button Btncat = (Button)this.findViewById( R.id.Btncat );
    final Button Btnchicken = (Button)this.findViewById( R.id.Btnchicken );
    final Button Btncow = (Button)this.findViewById( R.id.Btncow );
    final Button Btndog = (Button)this.findViewById( R.id.Btndog );
    final Button Btnhelf = (Button)this.findViewById( R.id.Btnhelf );
    final Button Btnhurse = (Button)this.findViewById( R.id.Btnhurse );
    final Button Btnsheep = (Button)this.findViewById( R.id.Btnsheep );
    final Button Btnwolf = (Button)this.findViewById( R.id.Btnwolf );


    Btnbird.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            catMP.stop();
            chickenMP.stop();
            cowMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            birdMP.start();


        }

    } );

    Btncat.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            birdMP.stop();
            chickenMP.stop();
            cowMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            catMP.start();



        }
    } );

    Btnchicken.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            birdMP.stop();
            cowMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            chickenMP.start();

        }
    } );
    Btncow.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            cowMP.start();

        }
    } );
    Btndog.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            cowMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            dogMP.start();

        }
    } );
    Btnhelf.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            dogMP.stop();
            cowMP.stop();
            horseMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            elephentMP.start();

        }
    } );
    Btnhurse.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            dogMP.stop();
            elephentMP.stop();
            cowMP.stop();
            sheepMP.stop();
            wolfMP.stop();
            horseMP.start();

        }
    } );
    Btnsheep.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            cowMP.stop();
            wolfMP.stop();
            sheepMP.start();

        }
    } );
    Btnwolf.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            catMP.stop();
            chickenMP.stop();
            birdMP.stop();
            dogMP.stop();
            elephentMP.stop();
            horseMP.stop();
            sheepMP.stop();
            cowMP.stop();
            wolfMP.start();

        }
    } );


    findViewById( R.id.back ).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(animals.this,MainActivity.class  );
            startActivity( intent );

        }


    } );


}

}

您可以使用stopSound()類的函數,通過調用stop()停止所有聲音。

示例: wolfMP.stop()

要開始下一個聲音之前,請調用stopSound()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM