play button music code example

Example 1: play audio button

<script>
var audio = new Audio("http://music.ogg" ) ;

audio.oncanplaythrough = function(){
audio.play();
}

audio.loop = true;

audio.onended = function(){
audio.play();
}

</script>

<input type="image" src="http://button.png" onclick="audio.play()">

Example 2: android play music on button click

public class MainActivity extends AppCompatActivity{
	Button bt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt = (Button)findViewById(R.id.clickme);
		final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample);
		bt.setOnClickListener(new View.OnClickListener(){
			@Overridepublic void onClick(View view){
				np.start();
			}
		}
	}
}

Tags:

Misc Example