Google Glass GDK:生成通知声音

时间:2014-06-02 19:15:25

标签: google-glass google-gdk

我想在Google Glass上的特定事件后生成通知声音。这就是我试过的

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                                .setSmallIcon(R.drawable.ic_action_done)
                                                .setContentTitle("Message Receied")
                                                .setContentText("New message received")
                                                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    //alert
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1, builder.build());

以及

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();

第一个代码段不会返回任何内容,也不会执行任何操作。考虑到缺少通知中心,我不确定Glass如何处理通知。第二个代码段在logcat

中引发以下错误
06-02 15:05:30.248: D/MediaPlayer(32271): Couldn't open file on client side, trying server side
06-02 15:05:30.279: E/MediaPlayer(32271): Unable to create media player
06-02 15:05:30.279: D/Ringtone(32271): Problem opening; delegating to remote player

有没有人知道如何产生声音?

谢谢

2 个答案:

答案 0 :(得分:2)

不确定是否可以解决您的问题,但您可以使用AudioManager生成玻璃声音,如:

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

然后,

mAudioManager.playSoundEffect(Sounds.TAP);

mAudioManager.playSoundEffect(Sounds.SUCCESS);

这不是通知声音,但也许您只需要生成声音?我不确定您的活动发生在哪里......

答案 1 :(得分:0)

似乎我最终回答了我自己的问题,但如果上述情况不起作用,这就是答案:

private SoundPool mSoundPool;
private int mAlertReceived;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
         mAlertReceived = mSoundPool.load(context, R.raw.tinkerbell, 1);

protected void playSound(int soundId) {
        mSoundPool.play(soundId,
                        1 /* leftVolume */,
                        1 /* rightVolume */,
                        1,
                        0 /* loop */,
                        1 /* rate */);  
    }

我从GDK秒表样本中获取了此代码。 res / raw /

中有一个.ogg文件