首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在打开我的活动的3秒后SoundPool没有播放

在打开我的活动的3秒后SoundPool没有播放
EN

Stack Overflow用户
提问于 2012-09-04 18:45:42
回答 1查看 778关注 0票数 1

我试图在用户点击按钮时播放声音,如果我在活动打开的前3-5秒内点击按钮,效果很好,但是如果我等待超过3-5秒就没有声音了。我也没有收到任何关于声音的错误...

任何想法都会得到重视!

更新:这只发生在我的HTC上。如果我在三星Galaxy S 2上试一下,它工作正常!

在我的日志中写着:"AudioHardwareQSD: AudioHardware pcm播放将进入待机状态“有没有解决这个问题的办法?

代码语言:javascript
复制
@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);}
EN

回答 1

Stack Overflow用户

发布于 2012-09-05 17:41:17

以下是我是如何让它工作的,虽然不是最好的,但它确实有效。

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

代码语言:javascript
复制
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....} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12261867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档