Introduction to the Android multimedia playback API

Source: Internet
Author: User

This article calls Android's media Player to implement some music playback operations

Project layout:

<LinearLayoutxmlns: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 " >    <EditTextAndroid:id= "@+id/et_path"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the path to the music file" />    <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >        <ButtonAndroid:id= "@+id/bt_play"Android:layout_width= "0dip"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Play" />        <ButtonAndroid:id= "@+id/bt_pause"Android:layout_width= "0dip"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Paused" />        <ButtonAndroid:id= "@+id/bt_replay"Android:layout_width= "0dip"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Replay" />        <ButtonAndroid:id= "@+id/bt_stop"Android:layout_width= "0dip"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Stop" />    </LinearLayout></LinearLayout>

You can view the files in the local SDK to see the relevant APIs

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

This article address: http://www.cnblogs.com/wuyudong/p/5679191.html, reprint please indicate source address.

The relevant Logic section code is as follows:

 PackageCom.wuyudong.mp3player;ImportJava.io.File;Importjava.io.IOException;ImportAndroid.media.AudioManager;ImportAndroid.media.MediaPlayer;ImportAndroid.media.MediaPlayer.OnCompletionListener;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateEditText Et_path; PrivateButton Bt_play, Bt_replay, Bt_pause, bt_stop; PrivateMediaPlayer MediaPlayer; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Et_path=(EditText) Findviewbyid (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); Bt_pause.setonclicklistener ( This); Bt_play.setonclicklistener ( This); Bt_replay.setonclicklistener ( This); Bt_stop.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt_play:play ();  Break;  CaseR.id.bt_replay:replay ();  Break;  CaseR.id.bt_stop:stop ();  Break;  CaseR.id.bt_pause:pause ();  Break; default:             Break; }    }    /*** Pause Music*/    Private voidpause () {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 to); return; }    }    /*** Re-play*/    Private voidReplay () {if(MediaPlayer! =NULL&&mediaplayer.isplaying ()) {Mediaplayer.seekto (0); return;    } play (); }    /*** Stop playing music*/    Private voidStop () {if(MediaPlayer! =NULL&&mediaplayer.isplaying ())            {mediaplayer.stop (); Mediaplayer.release (); //Remember to release resourcesMediaPlayer =NULL; Bt_play.setenabled (true); }    }    /*** Play Music*/    Private voidPlay () {String path=Et_path.gettext (). toString (). Trim (); File File=NewFile (path); if(File.exists () && file.length () > 0) {            Try{MediaPlayer=NewMediaPlayer ();                Mediaplayer.setaudiostreamtype (Audiomanager.stream_music);                Mediaplayer.setdatasource (path); Mediaplayer.prepare (); //might take long! (for buffering, etc)Mediaplayer.start (); Mediaplayer.setoncompletionlistener (NewOncompletionlistener () {@Override Public voidOncompletion (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, "file does not exist", 0). Show (); }    }}

Upload a copy of the audio file to SDcard before executing the code

The final result of running the project implementation is as follows:

Introduction to the Android multimedia playback API

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.