Play a sound in android

How do I make an android app which just plays a sound on click of a button ? Suppose, a sound file a.mp3 is inside res\sound\ How should I play it ? Thanks in advance.

Answers


First of all you have to create a folder called raw then put there the music you want to play and then do something like this :

 public class TestSound extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btSound = (Button)this.findViewById(R.id.button1);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.musicTest);
        btSound .setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                mp.start();
            }
        });     
    }

Supported Media FormatsAndroid

Hope it works :)


Need Your Help

How to convert wikipedia url of other languages to english in PHP?

php string preg-replace preg-match

I have a wikipedia url say(of some language but not english),

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.