首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何访问Android的默认嘟嘟声?

如何访问Android的默认嘟嘟声?
EN

Stack Overflow用户
提问于 2011-06-24 08:24:25
回答 3查看 69K关注 0票数 56

我想让一个按钮发出嘟嘟声,表示它已被按下。我想知道如何使用默认的安卓嘟嘟声(就像当你调整铃声音量时),而不是导入我自己的mp3音乐文件或使用ToneGenerator?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-14 15:51:21

代码语言:javascript
复制
public void playSound(Context context) throws IllegalArgumentException, 
                                              SecurityException, 
                                              IllegalStateException,
                                              IOException {

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    MediaPlayer mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(context, soundUri);
    final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        // Uncomment the following line if you aim to play it repeatedly
        // mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }
}

我找到了另一个答案:

代码语言:javascript
复制
try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}

功劳归于https://stackoverflow.com/a/9622040/737925

票数 77
EN

Stack Overflow用户

发布于 2020-07-02 23:44:02

你可以通过ToneGenerator类访问Android默认的beeb声音。

代码语言:javascript
复制
import android.media.AudioManager;
import android.media.ToneGenerator;
代码语言:javascript
复制
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 200);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);

更多关于它们听起来如何的信息:https://developer.android.com/reference/android/media/ToneGeneratorhttps://www.youtube.com/watch?v=HVu7K9W1_BM

票数 3
EN

Stack Overflow用户

发布于 2013-02-22 08:33:11

最简单的方法是使用ToneGenerator类的实例:

代码语言:javascript
复制
    //declaration
    ToneGenerator toneG;
    //using any where`
    if(val>=taux_max)
    {
        taux_text.setTextColor(warnning_col);
        toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); //200 is duration in ms
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6462105

复制
相关文章

相似问题

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