Android Development for Button Add audio instance

Source: Internet
Author: User

1.layout Layout file:

The code is as follows Copy Code

<relativelayout 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"
android:background= "@drawable/activity_background" >

<textview
Android:id= "@+id/title_bar"
Android:layout_width= "Fill_parent"
android:layout_height= "45dip"
Android:layout_alignparenttop= "true"
Android:gravity= "Center_vertical|center_horizontal"
android:background= "@drawable/theme_title_red"
Android:textsize= "25dip"
Android:textcolor= "#fff"
android:text= "Key Sound test"
Tools:context= ". Mainactivity "/>
<button
Android:id= "@+id/btn_test"
Android:layout_width= "80dip"
android:layout_height= "30dip"
android:layout_below= "@+id/title_bar"
android:layout_marginleft= "100dip"
android:layout_margintop= "20dip"
android:background= "@drawable/bg_btn_submit_selecter"
/>

</RelativeLayout>

2.Activity file:

The code is as follows Copy Code

Package com.jun.activity;

Import Android.media.AudioManager;
Import Android.media.SoundPool;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends activity {
Public Button Btn=null;
Private Soundpool sp;//declares a soundpool
private int music;//defines an integer with load (); To set Suondid

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
}

private void init () {
TODO auto-generated Method Stub
Btn= (Button) Findviewbyid (r.id.btn_test);
sp= New Soundpool (Ten, Audiomanager.stream_system, 5); The first parameter is the maximum number of simultaneous playback data streams, the second data stream type, and the third is sound quality
Music = Sp.load (This, r.raw.key_sound, 1); Put your sound material into the Res/raw, the 2nd argument is the resource file, and the 3rd is the priority of the music.

Btn.setonclicklistener (listener);
}
Private Onclicklistener Listener =new Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Sp.play (music, 1, 1, 0, 0, 1);

}};
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

3. Click on the Submit button, will automatically play,/src/raw folder under the Key_sound.mp3 music file!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.