Android mediaplayer: playing music and implementing progress bars

Source: Internet
Author: User

First, we read the file from the SD card to play the file !!

1 ,:

Work in advance and put music files into the SD card,

2. 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"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "song name:"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "blueflawer.pdf"/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "play" Android: id = "@ + ID/play_pause"/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "reset" Android: id = "@ + ID/RESET"/> <seekbar Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: id = "@ + ID/seekbar"/> </linearlayout>

3. Activity

Package CN. csdn. activity; import Java. io. file; import Java. io. ioexception; import Java. util. timer; import Java. util. timertask; import android. app. activity; import android. media. mediaplayer; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. seekbar; import android. widget. seekbar. onseekba Rchangelistener; public class myplayeractivity extends activity {private button play_pause, reset; private seekbar; private Boolean ifplay = false; private mediaplayer player = NULL; private string musicname = "blueflawer.mp3 "; private Boolean iffirst = false; private timer mtimer; private timertask mtimertask; private Boolean ischanging = false; // mutex variable to prevent degree conflict between the timer and seekbar when dragging. Public void oncreate (Bund Le savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); player = new mediaplayer (); findviews (); // each component} private void findviews () {play_pause = (button) findviewbyid (R. id. play_pause); reset = (button) findviewbyid (R. id. reset); play_pause.setonclicklistener (New myclick (); reset. setonclicklistener (New myclick (); seekbar = (seekbar) findviewbyid (R. id. seekbar); seekbar. seton Seekbarchangelistener (New myseekbar ();} class myclick implements onclicklistener {public void onclick (view v) {file = new file (environment. getexternalstoragedirectory (), musicname); // If (file. exists () {Switch (v. GETID () {case R. id. play_pause: If (player! = NULL &&! Ifplay) {play_pause.settext ("pause"); If (! Iffirst) {player. reset (); try {player. setdatasource (file. getabsolutepath (); player. prepare (); // prepare} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalstateexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} seekbar. setmax (player. getduration (); // set the progress bar // ---------- the timer records the playback progress --------- // mtimer = new timer (); mtimertask = new timertask () {@ override publi C void run () {If (ischanging = true) {return;} seekbar. setprogress (player. getcurrentposition () ;}}; mtimer. schedule (mtimertask, 0, 10); iffirst = true;} player. start (); // start ifplay = true;} else if (ifplay) {play_pause.settext ("continue"); player. pause (); ifplay = false;} break; case R. id. reset: If (ifplay) {player. seekto (0);} else {player. reset (); try {player. setdatasource (file. getabsolutepath (); player. prepa Re (); // prepare player. start (); // start} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalstateexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} break ;}}// progress bar processing class myseekbar implements onseekbarchangelistener {public void onprogresschanged (seekbar, int progress, Boolean fromuser) {} public void onstarttrackingtouch (seekbar) {ischanging = True;} public void onstoptrackingtouch (seekbar) {player. seekto (seekbar. getprogress (); ischanging = false ;}// call processing protected void ondestroy () {If (player! = NULL) {If (player. isplaying () {player. stop ();} player. release ();} super. ondestroy ();} protected void onpause () {If (player! = NULL) {If (player. isplaying () {player. Pause () ;}} super. onpause () ;} protected void onresume () {If (player! = NULL) {If (! Player. isplaying () {player. Start () ;}} super. onresume ();}}

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.