实施哔哔声到Android应用程序 [英] Implementing beep sounds into android application

查看:104
本文介绍了实施哔哔声到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有期望功能的应用程序。

I have an application with the desired functionality.

Howerver,在某些时候会显示敬酒,我想两声提示到了祝酒词显示,以提醒用户所显示的消息同时播放。

Howerver, at certain times toasts are displayed and I want a double beep to play at the same time that the toasts are displayed to alert the user to the messages being displayed.

我不知道最好的方法是什么android系统播放声音,或如果有一些默认的声音,我可以访问使用的警报。

I'm not sure what the best approach is for playing sounds in android or if there is some default sounds that I could access to use for the alerts.

一些指导将大大AP preciated!

Some guidance would be greatly appreciated!

感谢

更新

我有以下的code在我的主要活动文件:

I have the following code in my main activity file:

  public void playAlertTone(final Context context){
            Thread t = new Thread(){
                    public void run(){
                        MediaPlayer player = null;
                        int countBeep = 0;
                        while(countBeep<2){
                        player = MediaPlayer.create(context,R.raw.beep);
                        player.start();
                        countBeep+=1;
                        try {


                            Thread.sleep(player.getDuration()+100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        }
                    }
                };
                t.start();   

            }

我有一个名为蜂鸣声音文件的 RES /原料

我怎样才能让2发生在同一时间调用此方法在显示举杯if语句?

How can I call this method in an if statement where a toast is displayed so the 2 occur at the same time?

更新2:

下面是code,其中我试图调用提醒方式:

Here is the code where I'm trying to call the alerting method:

  if (elapsedTime > hourAlert)
        {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout,
                                           (ViewGroup) findViewById(R.id.toast_layout_root));
            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText("HOUR PASSED");

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.BOTTOM, 0, 160);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
            playAlertTone(getApplicationContext()); // Edited here now call

任何想法?

推荐答案

您可以把您的音频文件中的 RES /原料项目的文件夹

you can put your audio file in res/raw folder of your Project

和在一个线程中播放音频

and play audio in a thread

public  void playAlertTone(final Context context){


    Thread t = new Thread(){
            public void run(){
                MediaPlayer player = null;
                int countBeep = 0;
                while(countBeep<2){
                player = MediaPlayer.create(context,R.raw.beep);
                player.start();
                countBeep+=1;
                try {

                                // 100 milisecond is duration gap between two beep
                    Thread.sleep(player.getDuration()+100);
                                       player.release();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                }
            }
        };

        t.start();   

    }

//call it like this from your activity' any method



    if(myCondition){

    Toast.makeText(getApplicationContext(), text, duration).show();

    playAlertTone(getApplicationContext());


    }

这篇关于实施哔哔声到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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