代码之家  ›  专栏  ›  技术社区  ›  HelloWorld1

带有打开/关闭音乐的复选框

  •  0
  • HelloWorld1  · 技术社区  · 7 年前

    目标:
    总是重复播放的音乐。 应该有一个复选框,如果关闭,音乐将静音,如果开始播放

    问题:
    我应该使用什么方法和语法代码来创建它。 需要一些建议。

    信息:
    *我是安卓系统的新手

    package com.jfdimarzio.myapplication;
    
    import android.media.AudioAttributes;
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.os.Build;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        private SoundPool soundPool;
        private int ssound1, ssound2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .build();
    
                soundPool = new SoundPool.Builder()
                        .setMaxStreams(2)
                        .setAudioAttributes(audioAttributes)
                        .build();
            }
            else
            {
                soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
            }
    
            ssound1 = soundPool.load(this, R.raw.sound1, 1);
            ssound2 = soundPool.load(this, R.raw.sound2, 1);
        }
    
    
        public void playSound(View v)
        {
            switch(v.getId())
            {
                case R.id.button_sound1:
                    soundPool.play(ssound1, 1,1, 0, 0, 1);
                    break;
                case R.id.button_sound2:
                    soundPool.play(ssound2, 1,1, 0, 0, 1);
                    break;
            }
    
        }
    }
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button_sound1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="playSound"
            android:text="sound1" />
    
        <Button
            android:id="@+id/button_sound2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="playSound"
            android:text="sound2" />
    
    </LinearLayout>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Samir Dangal    7 年前

    将按钮替换为 XML

     <Checkbox
            android:id="@+id/button_sound1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sound1" />
    

    然后在里面 创建时的回调函数 你的 活动 班

    Checkbox btnSound1 = (CheckBox)findViewById(R.id.button_sound1);
    btnSound1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                   if(isChecked){
                     //write your play music code here
                    soundPool.play(ssound1, 1,1, 0, 0, 1);
    
                   }
    
                  if(!isChecked){
                  // write your music pause code here
                  //  soundPool.stop(ssound1);
                   }
    
           }});