ボタンクリックイベントに音響効果を追加します.
4058 ワード
レイアウトファイルを作成するにはbuttonを1つだけ追加します.
コードを書く;
3.提出ボタンをクリックすると、自動的に再生されます./src/rawフォルダの下のaaa.mp 3音楽ファイル
<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.mp 3音楽ファイル