使用 SoundPool 播放声音 [英] Play sound with SoundPool

查看:25
本文介绍了使用 SoundPool 播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中播放一段简短的声音.我编写了以下代码,但我的三星手机上没有声音并且出现奇怪的振动.但同时这段代码在我的 android 模拟器上运行良好.我的代码是:

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! :)

推荐答案

您的音量控制或声音文件播放可能存在问题:如果您更改了 Sound() 函数这有什么作用?如果您的手机没有正确处理 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)+"]");
    };

这篇关于使用 SoundPool 播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆