How do i play sound on button click in android studio?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26538421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i play sound on button click in android studio?
提问by xManfred
I would like that when the button is pressed the sound file starts to play.I have already created the raw folder and put the .wav file there. The problem is that i don't know how to do this thing. I checked similar questions here but they don't seem to fix my problem. What should i do? Thank you in advance.
I would like that when the button is pressed the sound file starts to play.I have already created the raw folder and put the .wav file there. The problem is that i don't know how to do this thing. I checked similar questions here but they don't seem to fix my problem. What should i do? Thank you in advance.
Here is my code:
Here is my code:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.media.MediaPlayer;
import android.view.View.OnClickListener;
public class MyActivity extends ActionBarActivity {
int clicked= 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//start
//end
final TextView text = (TextView) findViewById(R.id.textView);
text.setText("Score: 0");
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicked++;
text.setText("Score: " + clicked);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here the xml codes :
Here the xml codes :
<ImageView
android:layout_width="100dp"
android:layout_height="173dp"
android:id="@+id/imageView"
android:background="@drawable/aidsv"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:adjustViewBounds="false" />
<Button
android:layout_width="200dp"
android:layout_height="29dp"
android:text="Click me!"
android:id="@+id/button"
android:background="@drawable/redbottone"
android:layout_marginTop="39dp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="120dp"
android:layout_height="42dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: "
android:id="@+id/textView"
android:textColor="#FF0000"
android:background="@drawable/sfondotempo"
android:autoText="false"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp" />
回答by Soheyl
Log.v(TAG, "Initializing sounds...");
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
Button play_button = (Button)this.findViewById(R.id.button);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
Place your sound in res/raw folder.
Place your sound in res/raw folder.