android's Introduction
安卓学习 重写onKeyDown事件,对按键按下进行拦截(和系统键盘存在冲突) @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD) {//如果是虚拟键盘则不截获输入事件 return false; } InputDevice inputDevice = InputDevice.getDevice(event.getDeviceId()); Log.e("key", "onKeyDown: InputDevice:name=" + inputDevice.getName()+",productId="+inputDevice.getProductId()+",VendorId="+ inputDevice.getVendorId()); Log.e("key", "onKeyDown: keyCode=" + keyCode + "String=" + KeyEvent.keyCodeToString(keyCode)); if(keyCode==8){ }else if(keyCode==9){ if (tone==null){ }else{ tone.release(); } }else if(keyCode==10){ } //监听键盘以及二维码输入 return true;//截获事件 } //soundPool播放声音,多次播放需要释放资源。播放外部资源声音 Toast.makeText(this, "播放声音", Toast.LENGTH_SHORT).show(); //实例化SoundPool if(soundPool==null){ }else{ soundPool.release(); } //sdk版本21是SoundPool 的一个分水岭 if (Build.VERSION.SDK_INT >= 21) { SoundPool.Builder builder = new SoundPool.Builder(); //传入最多播放音频数量, builder.setMaxStreams(1); //AudioAttributes是一个封装音频各种属性的方法 AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder(); //设置音频流的合适的属性 attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC); //加载一个AudioAttributes builder.setAudioAttributes(attrBuilder.build()); soundPool = builder.build(); } else { /** * 第一个参数:int maxStreams:SoundPool对象的最大并发流数 * 第二个参数:int streamType:AudioManager中描述的音频流类型 *第三个参数:int srcQuality:采样率转换器的质量。 目前没有效果。 使用0作为默认值。 */ soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); } //可以通过四种途径来记载一个音频资源: //1.通过一个AssetFileDescriptor对象 //int load(AssetFileDescriptor afd, int priority) //2.通过一个资源ID //int load(Context context, int resId, int priority) //3.通过指定的路径加载 //int load(String path, int priority) //4.通过FileDescriptor加载 //int load(FileDescriptor fd, long offset, long length, int priority) //声音ID 加载音频资源,这里用的是第二种,第三个参数为priority,声音的优先级*API中指出,priority参数目前没有效果,建议设置为1。 final int voiceId = soundPool.load(this, R.raw.di, 1); //异步需要等待加载完成,音频才能播放成功 soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { if (status == 0) { //第一个参数soundID //第二个参数leftVolume为左侧音量值(范围= 0.0到1.0) //第三个参数rightVolume为右的音量值(范围= 0.0到1.0) //第四个参数priority 为流的优先级,值越大优先级高,影响当同时播放数量超出了最大支持数时SoundPool对该流的处理 //第五个参数loop 为音频重复播放次数,0为值播放一次,-1为无限循环,其他值为播放loop+1次 //第六个参数 rate为播放的速率,范围0.5-2.0(0.5为一半速率,1.0为正常速率,2.0为两倍速率) soundPool.play(voiceId, 1, 1, 1, 0, 1); } } }); // ToneGenerator 播放系统声音 tone = new ToneGenerator(AudioManager.STREAM_SYSTEM,ToneGenerator.MAX_VOLUME); Log.e("0000000000000","00000000000000000000"); // ToneGenerator tone = new ToneGenerator(AudioManager.STREAM_MUSIC,80); tone.startTone(ToneGenerator.TONE_PROP_BEEP);
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.