播放声音的Soundpool声音、Soundpool

2023-09-13 23:46:16 作者:唯你不可取代

我要在我的应用程序中播放一短声。我写了下面code,但我没有健全的和奇怪的振动出现在我的三星手机。但同时这code行之有效对我的Andr​​oid模拟器。我的code是:

I need to play a short sound in my application. I wrote the following code but I have no sound and strange vibration appeared on my Samsung phone. But in the same time this code works well on my android simulator. My code is:

package com.samplers;

import android.app.Activity;
import android.media.SoundPool;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FixVibroActivity extends Activity {
    /** Called when the activity is first created. */

    private Button white;
    private SoundPool spool;
    private int soundID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundID = spool.load(this, R.raw.error, 1);

        white = (Button)findViewById(R.id.whiteBtn);
        white.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Sound();
            }
        });
    }

    public void Sound(){
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        spool.play(soundID, volume, volume, 1, 0, 1f);
    };
}

帮我解决这个问题,拜托了!先感谢您! :)

Help me to solve this problem, please! Thank you in advance! :)

推荐答案

有可能要么是一个问题,你的音量控制或与您的声音文件播放正确:如果您更改声音()函数这个它有什么作用?如果你的手机没有正确处理 R.raw.error 文件格式,但仿真器进行正确的练习,这将是很奇怪的。

There might either be an issue with your volume control or with your sound file playing correctly: if you change the Sound() function to this what does it do? If your phone is not correctly handling the R.raw.error file format but the emulator is doing it correctly that would be VERY strange.

public void Sound(){
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        android.util.Log.v("SOUND","["+volume+"]["+spool.play(soundID, volume, volume, 1, 0, 1f)+"]");
    };