java - Simple Audio Play Stop In Android -
I'm the first in Android.
I want to make an app
An MP3 file starts playing when clicking on an image (play button image) and another image (stop image button) and map 3 stops
How to create this app?
See a simple example of starting, pausing, and stopping audio play.
To start, drag three buttons from pallete, stop playing audio and stop Now the xml file will look like this:
File: activity_main.xml
< Code> & lt; RelativeLayout xmlns: androclass = "http://schemas.android.com/apk / res / Android" xmlns: device = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "Match_parent" Android: paddingBottom = "@ Dimen / activity_vertical_margin" Android: paddingLeft = "@ Dimen / activity_horizontal_margin" Android: paddingRight = "@ Dimen / activity_horizontal_margin" Android: paddingTop = "@ Dimen / activity_vertical_margin" tool: reference = "MainActivity "& Gt; & Lt; TextView android: id = "@ + id / textView1" Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: layout_alignParentTop = "true" Android: layout_marginTop = "30dp" android: text = "Audio Controller" / Gt; & Lt; Button Android "Android: attr / buttonStyleSmall" id = "@ + id / Button1" style = Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: layout_alignLeft = "@ + id / textView1" Android: Leaut_bele = "@ + Id / text view 1" Android: layout_margintop = "48dp" Android: text = "start" /> & Lt; Button Android "Android: attr / buttonStyleSmall" id = "@ + id / Button2" style = Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: layout_alignTop = "@ + id / Button1" Android: Layout_toRightOf = "@ + ID / Button 1" Android: Text = "Pause" /> & Lt; Button Android "Android: attr / buttonStyleSmall" id = "@ + id / Button3" style = Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: layout_alignTop = "@ + id / Button2" Android: Layout_toRightOf = "@ + ID / Button 2" Android: Text = "Stop" /> & Lt; / RelativeLayout & gt;
Activity class
Type the code to start, stop, and stop the audio player.
File: Main Activity. Java
Package com.example.audioplay; Import android Media. media Player; Importroid.os.Bundle; Import android.os environment; Import android App Import android.view.Menu; Import android.view.View; Import android.view.View.OnClickListener; Import android.widget.Button; Public class extends the main activity (button to start, stop, pause, @ Override protected void saved create (bundled Instansstet) {Suprknkret (saved Instensstet); SetContentView (Rklayoutkactivity_main); start = ( button) findViewById (R.id.button1); pause = (button) findViewById (R.id.button2); to stop = (button) findViewById (R.id.button3); // media Player last mediaplayer MP = new mediaplayer (); // try {you can change the path, the path here is the external directory / Music / Maine .mp3 mp.setDataSource (Environment.getExternalStorageDirectory () getPath () + '/ music / maine.mp3 ") (e.g., sdcard.); Mp.prepare ();} hold (exception e) {e.printStackTrace () ;} start.setOnClickListener (new OnClickListener () {@Override see public void onClick (v) {mp.start ();}}); Pause.setOnClickListener (new OnClickListener () {@Override on public void (see V ) {Mp.pause ();}}); Stop.setOnClickListener (New OnClickListener () {@Override Click Public Zero (see V) {mp.stop ();}}); }}
Comments
Post a Comment