android - 打开我的 Activity 3秒后,SoundPool无法播放

标签 android audio soundpool

我尝试在用户单击按钮时播放声音,如果我在 Activity 打开的前3-5秒内单击按钮,则效果很好,但是如果我再等待3-5秒,则没有声音。关于声音,我也没有任何错误。

任何想法都将适用!

更新:这仅在我的HTC上发生。如果我在三星Galaxy S 2上尝试此功能,效果很好!!!

在我的logcat中说:“AudioHardwareQSD:AudioHardware pcm播放将处于待机状态”对此的任何解决方法???

@Override
protected void onCreate(Bundle savedInstanceState)
{

    mSoundPoolMap = new HashMap<Integer, Integer>();
    mSoundPool = new SoundPool(16, AudioManager.STREAM_RING, 0);
    mAudioManager= (AudioManager) mcontext.getSystemService(Context.AUDIO_SERVICE);

    mSoundPoolMap.put(1, mSoundPool.load(mcontext, R.raw.tap, 1));}



       @Override
        public void onClick(View v)
        {   
            float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
            streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
            mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 0, 1);}

最佳答案

这是我如何使它发挥最佳效果但它却有效的方法...

检查该设备是否来自HTC,然后创建一个播放声音的虚拟通知。另外,您还必须确保您的文件是MP3而不是WAV或OGG。

public static void playSound(int index)
{

    String m_strManufacture = Build.MANUFACTURER;
    Log.i(TAG, "Device Name: " + m_strManufacture);
    if (m_strManufacture.equals("HTC"))
    {


        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification();
        Intent notificationIntent = new Intent(mContext, mContext.getClass());
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
        // mContext.getResources().getResourceName(R.raw.ringer);
        notification.sound = Uri.parse("android.resource://com.yourpackages/raw/" + sound);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);

    }
    else {//Do the soundpool work....} 

关于android - 打开我的 Activity 3秒后,SoundPool无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12261867/

相关文章:

Android 2.2 - SoundPool 示例 0 未就绪

java - RxJava android mvp 单元测试 NullPointerException

android - 使用 soundpool 示例播放声音

android - 在 Kotlin 中打开另一个 fragment

android - 如何在固定的时间内产生特定频率的音调

android - 将 Android 中的音频从 48kHz 重采样到 44.1kHz,反之亦然 - 纯 Java 还是 OpenSL ES?

c++ - 如何将 ogg 音频文件解码为原始音频数据?

android - 背景音乐长度和速度

Android FirebaseMessaging Service onMessageReceived 调用了两次

android - 如何更改通知操作按钮的图标?