带有开/关音乐的复选框

时间:2019-01-28 21:46:53

标签: android android-studio

目标:
经常重复播放的音乐。 应当有一个复选框,如果“关闭”音乐将被静音,如果“开启”则开始播放

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

信息:
*我是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 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 个答案:

答案 0 :(得分:1)

XML

中的复选框替换按钮
 <Checkbox
        android:id="@+id/button_sound1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sound1" />

然后在您的活动课程的 onCreate

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);
               }

       }});