给按钮点击事件添加音效;
2017-02-10 15:44
537 查看
编写布局文件只添加一个button;
编写代码;
3.点击提交按钮,就会自动播放, /src/raw文件夹下的aaa.mp3音乐文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test4.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
编写代码;
package com.example.test4; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; public class MainActivity extends Activity { private Button button ; private SoundPool sPool;//声明一个sPool private int music;//定义一个整型用load();来设置suondID @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =(Button) findViewById(R.id.button1); sPool =new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量 music=sPool.load(this, R.raw.aaa, 1);//把你的声音素材放到res/raw里,第2个参数即为资源文件,第3个为音乐的优先级 button.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { sPool.play(music, 1, 1, 0, 0, 1); } }); // } }
3.点击提交按钮,就会自动播放, /src/raw文件夹下的aaa.mp3音乐文件
相关文章推荐
- ipad 添加到imageView中的按钮没有点击事件
- Android开发--为按钮添加点击事件
- MFC重绘关闭按钮,并给图片添加点击事件
- 添加按钮点击事件
- 为UIAlertView上的按钮添加方法,需要重写协议中对话框按钮点击事件方法
- swift按钮通过代码添加点击事件时出现unrecognized selector sent to instance 0x136ea5010错误
- tableView中添加按钮触发不了点击事件的解决办法
- android studio 添加按钮点击事件的三种方法
- winform中的datagridview有添加按钮列,响应按钮的点击事件
- 【Unity】UGUI怎么动态的添加按钮的OnClick点击事件
- ios 动态添加了按钮,绑定同一个点击事件,如何判断点击的是哪个?
- 练习题:获取登录按钮及整个登录面板,给登录按钮添加事件,点击时让登录层示,点击登录面板的关闭按钮,关闭整个登录层
- SDI里添加按钮并响应点击事件
- UIImageView添加UIButton时,按钮点击事件无效
- iOS--- 动态添加了按钮,绑定同一个点击事件,如何判断点击的是哪个?
- 【Android】利用Java代码布局,按钮添加点击事件
- Android学习笔记(九):按钮添加点击事件的4种方式
- IOS开发基础01(工程创建,添加视图,按钮,按钮点击事件)
- 高德地图显示在fragment中并添加按钮点击事件