Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有打开/关闭音乐的复选框_Android_Android Studio - Fatal编程技术网

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

Android 带有打开/关闭音乐的复选框,android,android-studio,Android,Android Studio,目标: 总是重复播放的音乐。 应该有一个复选框,如果关闭音乐将静音,如果打开开始播放 问题: 我应该使用什么方法和语法代码来创建它。 我需要一些建议 信息: *我是android的新手 package com.jfdimarzio.myapplication; import android.media.AudioAttributes; import android.media.AudioManager; import android.media.SoundPool; import androi

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

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

信息:
*我是android的新手

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中的复选框替换按钮

 <Checkbox
        android:id="@+id/button_sound1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sound1" />
谢谢你的帮助!
 <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);
               }

       }});