如何使用媒体播放器在Android中获得声音淡入功能,在报警的应用 [英] how to get the sound fade in functionality in alarm application using media player in android

查看:97
本文介绍了如何使用媒体播放器在Android中获得声音淡入功能,在报警的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我正在开发具有报警功能的应用程序。在这里,我需要提供在功能上的声音淡入淡出。我使用媒体播放器来调用铃声报警。这里是我的code播放警报声

Hi i am new to android. I am developing a application with alarm functionality. Here i need to provide the sound fade in functionality. I am using media player to invoke the ringtone for alarm. here is my code for playing alarm sound

 try {
                        if(mp==null){
                System.out.println("-------mp is null now------------");
                Uri myUri = Uri.parse("android.resource://com.android.crazy/raw/airtel");
           mp=MediaPlayer.create(Alarm.this, myUri);
            }
mp.setDataSource(songName);
            mp.prepareAsync();
            mp.setLooping(true);

        } catch (Exception e) {
            e.printStackTrace();
        } 
      mp.start();

在这里,我需要给声音淡入功能。请告诉我如何把声音淡入的时间像5分钟一个特定的金额

Here i need to give the sound fadein functionality. please advise me how to put the sound fade in for a particular amount of time like 5 mins

在此先感谢

推荐答案

您可以使用此方法:

setVolume(float leftVolume, float rightVolume);

创建淡入的效果,你会刚开始在零,并保持它的设置有点偏高定期。有些东西像这样将5%的第二每隔半

to create "fade in" effect you'll just start at zero and keep setting it a little bit higher at regular intervals. Some thing like this will increast the volume by 5% every half of a second

            final Handler h = new Handler();
            float leftVol = 0f;
            float rightVol = 0f;
            Runnable increaseVol = new Runnable(){
                public void run(){
                    mp.setVolume(leftVol, rightVol);
                    if(leftVol < 1.0f){
                        leftVol += .05f;
                        rightVol += .05f;
                        h.postDelayed(increaseVol, 500);
                    }
                }
            };


            h.post(increaseVol);

您可以控制​​它得到多少改变您添加到leftVol和rightVol if语句里面有什么提高每一跳。您可以更改多久,每个刻度需要通过改变500的参数在postDelayed()方法。

you can control how much it gets raised each tick by changing what you add to leftVol and rightVol inside the if statement. And you can change how long each tick takes by changing the 500 parameter in the postDelayed() method.

这篇关于如何使用媒体播放器在Android中获得声音淡入功能,在报警的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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