Android multimedia playback API introduction, androidapi

Source: Internet
Author: User

Android multimedia playback API introduction, androidapi

This article calls the android Media Player to play some music.

Project Layout:

<LinearLayout 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: orientation = "vertical" tools: context = ". mainActivity "> <EditText android: id =" @ + id/et_path "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: hint = "Enter the music file path"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/bt_play" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "play"/> <Button android: id = "@ + id/bt_pause" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "pause"/> <Button android: id = "@ + id/bt_replay" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "replay"/> <Button android: id = "@ + id/bt_stop" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "stop"/> </LinearLayout>

You can view files in the local sdk and view related APIs.

File :///...... /Sdk/docs/guide/topics/media/mediaplayer.html

URL: http://www.cnblogs.com/wuyudong/p/5679191.html.

The logic code is as follows:

Package com.wuyudong.mp3 player; import java. io. file; import java. io. IOException; import android. media. audioManager; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android.net. uri; 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; import Android. widget. editText; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private EditText et_path; private Button bt_play, bt_replay, encrypted, bt_stop; private MediaPlayer mediaPlayer; @ Override protected void onCreate (Bundle listener. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_path = (EditText) fi NdViewById (R. id. et_path); bt_play = (Button) findViewById (R. id. bt_play); bt_replay = (Button) findViewById (R. id. bt_replay); bt_pause = (Button) findViewById (R. id. bt_pause); bt_stop = (Button) findViewById (R. id. bt_stop); listener (this); bt_play.setOnClickListener (this); bt_replay.setOnClickListener (this); bt_stop.setOnClickListener (this);} @ Override public void onClick (View v ){ Switch (v. getId () {case R. id. bt_play: play (); break; case R. id. bt_replay: replay (); break; case R. id. bt_stop: stop (); break; case R. id. bt_pause: pause (); break; default: break;}/*** pause music */private void pause () {if ("continue ". equals (bt_pause.getText (). toString (). trim () {mediaPlayer. start (); bt_pause.setText ("pause"); return;} if (mediaPlayer! = Null & mediaPlayer. isPlaying () {mediaPlayer. pause (); bt_pause.setText ("continue"); return ;}}/*** replay */private void replay () {if (mediaPlayer! = Null & mediaPlayer. isPlaying () {mediaPlayer. seekTo (0); return;} play () ;}/ *** stop playing music */private void stop () {if (mediaPlayer! = Null & mediaPlayer. isPlaying () {mediaPlayer. stop (); mediaPlayer. release (); // remember to release the resource mediaPlayer = null; bt_play.setEnabled (true);}/*** play music */private void play () {String path = et_path.getText (). toString (). trim (); File file = new File (path); if (file. exists () & file. length ()> 0) {try {mediaPlayer = new MediaPlayer (); mediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); mediaP Layer. setDataSource (path); mediaPlayer. prepare (); // might take long! (For buffering, etc) mediaPlayer. start (); mediaPlayer. setOnCompletionListener (new OnCompletionListener () {@ Override public void onCompletion (MediaPlayer mp) {bt_play.setEnabled (true) ;}}); bt_play.setEnabled (false);} catch (Exception e) {Toast. makeText (this, "playback failed", 0 ). show (); e. printStackTrace () ;}} else {Toast. makeText (this, "the file does not exist", 0 ). show ();}}}

Upload an audio file to sdcard before executing the code.

The final result of running the project is as follows:

Related Article

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.