Как воспроизвести другой звук с помощью кнопки переключения? - PullRequest
0 голосов
/ 19 марта 2019

У меня есть приложение для изучения арабских букв, которое я опубликовал в магазине Play, я хочу обновить его произношение, я хочу два разных произношения для одной и той же буквы, поэтому я хочу использовать кнопку переключения, которая в его состояние по умолчанию воспроизводит одно произношение, а когда отмечено, воспроизводит второе произношение. Ниже мой пробный код. Пожалуйста, помогите, как справка, если кнопка переключателя проверена или нет в MainActivity.class

public class MainActivity extends AppCompatActivity {
    private Button previousButton;
    private Button nextbutton;
    private ImageView imageView;
    private AudioManager audioManager;
    float actVolume, maxVolume, volume;
    private SoundPool soundPool;
    int hurufID;
    private int[] huruf;
    private int[] hurufsound;
    private int[] misriSound;
    int i = 0;
    // Global variable initiated to set the counter for the images to rotate
    int counter = 0;

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

    //audio manager settings for volume
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    volume = actVolume / maxVolume;

    AudioAttributes aa = null;
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
        aa = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .setUsage(USAGE_MEDIA)
                .build();
    }

    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(aa)
                .build();
    }
    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            soundPool.play(hurufID,1,1,1,0,1f);
        }
    });

    huruf = new int[28];
    huruf[0] = R.drawable.alizabar;
    huruf[1] = R.drawable.baezabar;
    huruf[2] = R.drawable.taezabar;
    huruf[3] = R.drawable.saezabar;
    huruf[4] = R.drawable.jeemzabar;
    huruf[5] = R.drawable.haizabar;
    huruf[6] = R.drawable.khaizabar;
    huruf[7] = R.drawable.daalzabar;
    huruf[8] = R.drawable.zaalzabar;
    huruf[9] = R.drawable.raezabar;
    huruf[10] = R.drawable.zaezabar;
    huruf[11] = R.drawable.seenzabar;
    huruf[12] = R.drawable.sheenzabar;
    huruf[13] = R.drawable.suwadzabar;
    huruf[14] = R.drawable.duwadzabar;
    huruf[15] = R.drawable.toinzabar;
    huruf[16] = R.drawable.zoinzabar;
    huruf[17] = R.drawable.aainzabar;
    huruf[18] = R.drawable.gainzabar;
    huruf[19] = R.drawable.faezabar;
    huruf[20] = R.drawable.khaafzabar;
    huruf[21] = R.drawable.kaafzabar;
    huruf[22] = R.drawable.laamzabar;
    huruf[23] = R.drawable.meemzabar;
    huruf[24] = R.drawable.noonzabar;
    huruf[25] = R.drawable.waawzabar;
    huruf[26] = R.drawable.chasmihaizabar;
    huruf[27] = R.drawable.yaazabar;

    hurufsound = new int [28];
    hurufsound[0] = R.raw.alifzabar;
    hurufsound[1] = R.raw.baezabar;
    hurufsound[2] = R.raw.taezabar;
    hurufsound[3] = R.raw.saezabar;
    hurufsound[4] = R.raw.jeemzabar;
    hurufsound[5] = R.raw.haizabar;
    hurufsound[6] = R.raw.khaizabar;
    hurufsound[7] = R.raw.daalzabar;
    hurufsound[8] = R.raw.zaalzabar;
    hurufsound[9] = R.raw.raezabar;
    hurufsound[10] = R.raw.zaezabar;
    hurufsound[11] = R.raw.seenzabar;
    hurufsound[12] = R.raw.sheenzabar;
    hurufsound[13] = R.raw.suwadzabar;
    hurufsound[14] = R.raw.duwadzabar;
    hurufsound[15] = R.raw.toinzabar;
    hurufsound[16] = R.raw.zoinzabar;
    hurufsound[17] = R.raw.aainzabar;
    hurufsound[18] = R.raw.gainzabar;
    hurufsound[19] = R.raw.faezabar;
    hurufsound[20] = R.raw.khaafzabar;
    hurufsound[21] = R.raw.kaafzabar;
    hurufsound[22] = R.raw.laamzabar;
    hurufsound[23] = R.raw.meemzabar;
    hurufsound[24] = R.raw.noonzabar;
    hurufsound[25] = R.raw.wawzabar;
    hurufsound[26] = R.raw.chaizabar;
    hurufsound[27] = R.raw.yaazabar;

    misriSound = new int[7];
    misriSound[0] = R.raw.malif;
    misriSound[1] = R.raw.baa;
    misriSound[2] = R.raw.taa;
    misriSound[3] = R.raw.saa;
    misriSound[4] = R.raw.haa;
    misriSound[5] = R.raw.khaa;
    misriSound[6] = R.raw.mjeem;


    imageView = findViewById(R.id.sequenceImageView);

    nextbutton = findViewById(R.id.next_button);
    nextbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            // counter ++ goes forward each image and
            counter++;
            // if counter reaches to the last it will set to the first image.
            if ( counter == huruf.length ) counter = 0;

            i = counter;
            audio(i);

            imageView.setImageResource(huruf[counter]);

        }
    });

    previousButton = findViewById(R.id.previous_button);
    previousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            // counter -- is going backwards
            counter--;
            // if counter goes to 0 lenght it will set back to last of the image
            if ( counter < 0 ) counter = huruf.length - 1;

            i = counter;
            audio(i);
            imageView.setImageResource(huruf[counter]);
        }
    });



}
// I know I have to do something here but not sure what
public void audio(int i) {
    hurufID = soundPool.load(this,hurufsound[i],1);
}


@Override
protected void onPause () {
    super.onPause();
    if (soundPool == null) {
        soundPool.release();
    }

}
@Override
protected void onResume (){
    super.onResume();
    if ( soundPool != null ) {
        soundPool.play(hurufID, 1, 1, 1, 0, 1);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mainmenu, menu);
    MenuItem menuItem = menu.findItem(R.id.switchID);
    menuItem.setActionView(R.layout.swtich_layout);
    Switch mSwitch = menuItem.getActionView().findViewById(R.id.swtichAb);
    mSwitch.setChecked(false);
    mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if ( isChecked ){
                Toast.makeText(MainActivity.this, "ON", Toast.LENGTH_SHORT).show();
                hurufID = soundPool.load(getApplicationContext(),misriSound[0],1);

            }else
                Toast.makeText(MainActivity.this, "OFF", Toast.LENGTH_SHORT).show();
                hurufID = soundPool.load(getApplicationContext(),hurufsound[0],1);

        }
    });
    return true;
}

`

...