Android Learning 1. play audio using mediaplayer

Source: Internet
Author: User

To use mediaplayer to play audio and video, you must first understand the status of mediaplayer. The following figure shows the status of the mediaplayer provided by Google. You only need to understand the status of the mediaplayer. It won't go wrong.

For more information, see the APIS provided by Google.

To use the audio playing program written by mediaplayer to be robust, you must not only understand the status of the mediaplayer, but also set the various monitoring events and callback methods of the mediaplayer.

You can set different listening methods as needed.

The following is a copy of others' code, which is well written.

1. This is the layout file main. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "TOP"
Android: Orientation = "horizontal">

<Button
Android: Id = "@ + ID/btnplayurl"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "playing Network Audio"/>

<Button
Android: Id = "@ + ID/btnpause"
Android: layout_width = "80dip"
Android: layout_height = "wrap_content"
Android: text = "paused"/>

<Button
Android: Id = "@ + ID/btnstop"
Android: layout_width = "80dip"
Android: layout_height = "wrap_content"
Android: text = "stop"/>
</Linearlayout>

<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "TOP"
Android: Orientation = "horizontal">

<Seekbar
Android: Id = "@ + ID/skbprogress"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_vertical"
Android: layout_weight = "1.0"
Android: max = "100"
Android: paddingleft = "10dip"
Android: paddingright = "10dip"/>
</Linearlayout>

</Linearlayout>

2. Player. Java encapsulates mediaplayer playback.

Package com. Tao. test;

Import java. Io. ioexception;
Import java. util. timer;
Import java. util. timertask;

Import Android. Media. audiomanager;
Import Android. Media. mediaplayer;
Import Android. Media. mediaplayer. onbufferingupdatelistener;
Import Android. Media. mediaplayer. oncompletionlistener;
Import Android. Media. mediaplayer. onerrorlistener;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. widget. seekbar;

Public class player implements onbufferingupdatelistener, oncompletionlistener,
Mediaplayer. onpreparedlistener, onerrorlistener {
Public mediaplayer;
Private seekbar skbprogress;
Private timer mtimer = new timer ();

Public player (seekbar skbprogress ){
This. skbprogress = skbprogress;

Try {
Mediaplayer = new mediaplayer ();
Mediaplayer. setaudiostreamtype (audiomanager. stream_music );
Mediaplayer. setonbufferingupdatelistener (this );
Mediaplayer. setonpreparedlistener (this );
Mediaplayer. setoncompletionlistener (this );
} Catch (exception e ){
Log. E ("mediaplayer", "error", e );
}

Mtimer. Schedule (mtimerjob, 0, 1000 );
}

/**
* Update the progress bar using the timer and handler.
*/
Timertask mtimertask = new timertask (){
@ Override
Public void run (){
If (mediaplayer = NULL)
Return;
If (mediaplayer. isplaying () & skbprogress. ispressed () = false ){
Handleprogress. sendemptymessage (0 );
}
}
};

// Process the message
Handler handleprogress = new handler (){
Public void handlemessage (Message MSG ){

Int position = mediaplayer. getcurrentposition ();
Int duration = mediaplayer. getduration ();

If (duration> 0 ){
Int Pos = skbprogress. getmax () * position/duration;
Skbprogress. setprogress (POS );
}
};
};

Public void playurl (string videourl ){
Try {
Mediaplayer. Reset ();
Mediaplayer. setdatasource (videourl );
Mediaplayer. Prepare (); // play automatically after prepare
} Catch (illegalargumentexception e ){
E. printstacktrace ();
} Catch (illegalstateexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}

Public void pause (){
Mediaplayer. Pause ();
}

Public void play (){
Mediaplayer. Start ();
}
Public void stop (){
If (mediaplayer! = NULL ){
Mediaplayer. Stop ();
Mediaplayer. Release ();
Mediaplayer = NULL;
}
}

/**
* Playing Through onprepared
*/
Public void onprepared (mediaplayer arg0 ){
Arg0.start ();
Log. E ("mediaplayer", "onprepared ");
}

Public void oncompletion (mediaplayer arg0 ){
Log. E ("mediaplayer", "oncompletion ");
}

Public void onbufferingupdate (mediaplayer arg0, int bufferingprogress ){
Skbprogress. setsecondaryprogress (bufferingprogress );
Int currentprogress = skbprogress. getmax ()
* Mediaplayer. getcurrentposition ()/mediaplayer. getduration ();
Log. E (currentprogress + "% play", bufferingprogress + "% buffer ");
}

Public Boolean onerror (mediaplayer MP, int what, int extra ){
Return false;
}

}

3. Activity used for testmedia. Java Testing

Package com. Tao. test;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. seekbar;

Public class testmedia extends activity {
Private button btnpause, btnplayurl, btnstop;
Private seekbar skbprogress;
Private player;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
This. settitle ("online music playing --- hellogv writing ");


Btnplayurl = (button) This. findviewbyid (R. Id. btnplayurl );
Btnplayurl. setonclicklistener (New clickevent ());

Btnpause = (button) This. findviewbyid (R. Id. btnpause );
Btnpause. setonclicklistener (New clickevent ());

Btnstop = (button) This. findviewbyid (R. Id. btnstop );
Btnstop. setonclicklistener (New clickevent ());

Skbprogress = (seekbar) This. findviewbyid (R. Id. skbprogress );
Skbprogress. setonseekbarchangelistener (New seekbarchangeevent ());
Player = new player (skbprogress );

}

Class clickevent implements onclicklistener {

Public void onclick (view arg0 ){
If (arg0 = btnpause ){
Player. Pause ();
} Else if (arg0 = btnplayurl ){
// Search for any link in Baidu MP3. You can try another link.
String url = "http: // 219.138.125.22/myweb/MP3/cmp3/jh19.pdf ";
Player. playurl (URL );
} Else if (arg0 = btnstop ){
Player. Stop ();
}
}
}

Class seekbarchangeevent implements seekbar. onseekbarchangelistener {
Int progress;
Public void onprogresschanged (seekbar, int progress,
Boolean fromuser ){
// Originally (Progress/seekbar. getmax () * player. mediaplayer. getduration ()
This. Progress = progress * player. mediaplayer. getduration ()
/Seekbar. getmax ();
}

Public void onstarttrackingtouch (seekbar ){
}

Public void onstoptrackingtouch (seekbar ){
// Seekto () is a number relative to the video time, not a number relative to seekbar. getmax ().
Player. mediaplayer. seekto (Progress );
}
}

}

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.