首页 > 其他分享 >Android 触摸音的播放

Android 触摸音的播放

时间:2023-05-23 18:34:47浏览次数:35  
标签:SOUND 触摸 void volume int EFFECTS mediaPlayer Android 播放

1.源码路径  frameworks\base\services\core\java\com\android\server\audio\AudioService.java.

AudioService

    //==========================================================================================
    // Sound Effects
    //==========================================================================================
    private static final class LoadSoundEffectReply
            implements SoundEffectsHelper.OnEffectsLoadCompleteHandler {
        private static final int SOUND_EFFECTS_LOADING = 1;
        private static final int SOUND_EFFECTS_LOADED = 0;
        private static final int SOUND_EFFECTS_ERROR = -1;
        private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000;

        private int mStatus = SOUND_EFFECTS_LOADING;

        @Override
        public synchronized void run(boolean success) {
            mStatus = success ? SOUND_EFFECTS_LOADED : SOUND_EFFECTS_ERROR;
            notify();
        }

        public synchronized boolean waitForLoaded(int attempts) {
            while ((mStatus == SOUND_EFFECTS_LOADING) && (attempts-- > 0)) {
                try {
                    wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS);
                } catch (InterruptedException e) {
                    Log.w(TAG, "Interrupted while waiting sound pool loaded.");
                }
            }
            return mStatus == SOUND_EFFECTS_LOADED;
        }
    }

    /** @see AudioManager#playSoundEffect(int) */
    public void playSoundEffect(int effectType) {
        playSoundEffectVolume(effectType, -1.0f);
    }

    /** @see AudioManager#playSoundEffect(int, float) */
    public void playSoundEffectVolume(int effectType, float volume) {
        // do not try to play the sound effect if the system stream is muted
        if (isStreamMute(STREAM_SYSTEM)) {
            return;
        }

        if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) {
            Log.w(TAG, "AudioService effectType value " + effectType + " out of range");
            return;
        }

        sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE,
                effectType, (int) (volume * 1000), null, 0);
    }

发送msg交给handler处理

    case MSG_PLAY_SOUND_EFFECT:
                    mSfxHelper.playSoundEffect(msg.arg1, msg.arg2);
                    break;
mSfxHelper是SoundEffectsHelper 的实例

@NonNull
private SoundEffectsHelper mSfxHelper;

frameworks\base\services\core\java\com\android\server\audio\SoundEffectsHelper.java

    /*package*/ void playSoundEffect(int effect, int volume) {
        sendMsg(MSG_PLAY_EFFECT, effect, volume, null, 0);
    }
    private class SfxHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_EFFECTS:
                    onl oadSoundEffects((OnEffectsLoadCompleteHandler) msg.obj);
                    break;
                case MSG_UNLOAD_EFFECTS:
                    onUnloadSoundEffects();
                    break;
                case MSG_PLAY_EFFECT:
                    final int effect = msg.arg1, volume = msg.arg2;
                    onl oadSoundEffects(new OnEffectsLoadCompleteHandler() {
                        @Override
                        public void run(boolean success) {
                            if (success) {
                                onPlaySoundEffect(effect, volume);
                            }
                        }
                    });
                    break;
                case MSG_LOAD_EFFECTS_TIMEOUT:
                    if (mSoundPoolLoader != null) {
                        mSoundPoolLoader.onTimeout();
                    }
                    break;
            }
        }
    }

播放策略

    void onPlaySoundEffect(int effect, int volume) {
        float volFloat;
        // use default if volume is not specified by caller
        if (volume < 0) {
            volFloat = (float) Math.pow(10, (float) mSfxAttenuationDb / 20);
        } else {
            volFloat = volume / 1000.0f;
        }

        Resource res = mResources.get(mEffects[effect]);
        if (mSoundPool != null && res.mSampleId != EFFECT_NOT_IN_SOUND_POOL && res.mLoaded) {
            mSoundPool.play(res.mSampleId, volFloat, volFloat, 0, 0, 1.0f);
        } else {
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                String filePath = getResourceFilePath(res);
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM);
                mediaPlayer.prepare();
                mediaPlayer.setVolume(volFloat);
                mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        cleanupPlayer(mp);
                    }
                });
                mediaPlayer.setOnErrorListener(new one rrorListener() {
                    public boolean one rror(MediaPlayer mp, int what, int extra) {
                        cleanupPlayer(mp);
                        return true;
                    }
                });
                mediaPlayer.start();
            } catch (IOException ex) {
                Log.w(TAG, "MediaPlayer IOException: " + ex);
            } catch (IllegalArgumentException ex) {
                Log.w(TAG, "MediaPlayer IllegalArgumentException: " + ex);
            } catch (IllegalStateException ex) {
                Log.w(TAG, "MediaPlayer IllegalStateException: " + ex);
            }
        }
    }
    SoundEffectsHelper(Context context) {
        mContext = context;
        mSfxAttenuationDb = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_soundEffectVolumeDb);
        startWorker();
    }

frameworks\base\core\res\res\values\config.xml

<!-- The attenuation in dB applied to the sound effects played
         through AudioManager.playSoundEffect() when no volume is specified. -->
    <integer name="config_soundEffectVolumeDb">-6</integer>

 

   

标签:SOUND,触摸,void,volume,int,EFFECTS,mediaPlayer,Android,播放
From: https://www.cnblogs.com/wanglongjiang/p/17426063.html

相关文章

  • 代码混淆及android配置
    1什么是代码混淆百度百科解释:代码混淆(Obfuscatedcode)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为。代码混淆可以用于程序源代码,也可以用于程序编译而成的中间代码。执行代码混淆的程序被称作代码混淆器。已经存在许多种功能各异的代码......
  • AI智能融合平台EasyCVR接入RTSP流,视频无法播放的原因排查与解决
    EasyCVR视频融合平台基于云边端协同架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等。有用户反馈,现场内网环境,EasyCVR接入RTSP协议后,视......
  • APP测试 - 利用ABD命令将Android手机中已安装的APP导出为APK文件
    0.场景测试过程中部分迭代的APP与研发的版本不一致,又不知道具体是哪个版本。故需要导出测试出现问题的迭代APP用于复现问题 1.导出步骤1.1找到应用的包名方案A:查看手机安装的应用$adbshellpmlistpackage方案B:利用monitor监视要打开的APP$adbshellammonitor......
  • APlayer & MetingJS 音乐播放器使用指南
    1.介绍APlayer是一个简洁漂亮、功能强大的Html5音乐播放器,GitHub地址:https://github.com/DIYgod/APlayerMetingJS是为APlayer添加网易云、QQ音乐等支持的插件,GitHub地址: https://github.com/metowolf/MetingJS2.安装引用三个文件APlayer.min.js  APlayer.min.css ......
  • Android 单独Process 的 Service 触发 Application的构造
    今天在使用单独Process的Service(android:process,如果没有此attr就不会)时,发现该Serivce的启动会使得AndroidManifest中包裹此Service的Application再构造一个出来,想了想也合理,因为每个Application都对应一个Process,那么对于单独Process的service来说,是一个新进程,那么是需要构造出......
  • Android handler src track
    HandlerThread是真正干活的,本身就是一个Thread:HandlerThreadextendsThread主要比普通的Thread多了一个Looper,而hanlderThread的run函数其实很简单:@Overridepublicvoidrun(){mTid=Process.myTid();Looper.prepare();这一步就生了......
  • Android开发 UsageStatsManager应用使用情况管理
    前言  UsageStatsManager是用来知晓,设备中应用的使用情况的管理。它能给我们提供应用的进入前台动作与时间戳、进入后台的动作与时间戳、上次的使用时间、使用总时长等等信息。此功能在原生的设置-应用-使用统计中有所展示。所需权限<uses-permissionandroid:name="android.......
  • Android平台GB28181设备接入模块如何实现实时视频和本地录像双码流编码
    ​技术背景我们在做Android平台GB28181设备接入模块的时候,遇到这样的场景,比如执法记录仪或智慧工地等场景下,由于GB28181设备接入模块,注册到国标平台后,平时只是心跳保持,或还有实时位置订阅,查看视频的时候,是按需看,而且有时候,网络环境并不是太好,所以,催生了这样一个诉求:部分开发者希......
  • 修改Android 触摸提示音及音量大小
    一、修改代码提高系统的音量。代码路径:base/services/core/java/com/android/server/audio/AudioService.javaprivatestaticint[]MAX_STREAM_VOLUME=newint[]{5,//STREAM_VOICE_CALL7,//STREAM_SYSTEM7,//STREAM_RING15......
  • iOS ijkplayer 播放器 消息循环 详解
    iOSijkplayer 消息循环_mediaPlayer=ijkmp_ios_create(media_player_msg_loop);//开始消息循环intmedia_player_msg_loop(void*arg){@autoreleasepool{IjkMediaPlayer*mp=(IjkMediaPlayer*)arg;__weakIJKFFMoviePlayerController*ffpControll......