First, Introduction
1. When the click Entry is implemented in Onlistitemclick, jump to playeractivity,mp3info via intent to Playeractivity
2.PlayerActivity playback via Android.media.MediaPlayer, pause, stop
Second, the Code
1.xml
(1) Player.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"4android:orientation= "Horizontal" android:paddingleft= "10dip"5android:paddingright= "10dip" android:paddingtop= "1dip"6android:paddingbottom= "1dip" >7<imagebutton android:id= "@+id/begin" android:layout_width= "Wrap_content"8android:layout_height= "Wrap_content" android:src= "@drawable/begin"/>9<imagebutton android:id= "@+id/pause" android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" android:src= "@drawable/pause"/> One<imagebutton android:id= "@+id/stop" android:layout_width= "Wrap_content" Aandroid:layout_height= "Wrap_content" android:src= "@drawable/stop"/> -</LinearLayout>
(2) Androidmanifest.xml Registered Activity
1 <activity android:name= ". Playeractivity "android:label=" @string/app_name "/>
2.java
(1) Localmp3listactivity.java
When you click on a song, start playeractivity
1 @Override2 protected voidOnlistitemclick (ListView L, View V,intPositionLongID) {3Mp3info info =Infos.get (position);4Intent Intent =NewIntent ();5Intent.putextra ("Mp3info", info);6Intent.setclass ( This, Playeractivity.class);7 startactivity (intent);8}
(2) Playeractivity.java
1 PackageTony.mp3player;2 3 ImportJava.io.File;4 5 ImportTony.model.Mp3Info;6 Importandroid.app.Activity;7 Importandroid.content.Intent;8 ImportAndroid.media.MediaPlayer;9 ImportAndroid.net.Uri;Ten ImportAndroid.os.Bundle; One Importandroid.os.Environment; A ImportAndroid.view.View; - ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.ImageButton; the - Public classPlayeractivityextendsActivity { - - PrivateImageButton beginbtn =NULL; + PrivateImageButton pausebtn =NULL; - PrivateImageButton stopbtn =NULL; + A PrivateMediaPlayer MediaPlayer =NULL; at PrivateMp3info info =NULL; - - Private BooleanIsPlaying =false; - Private BooleanIspause =false; - Private BooleanIsrelease =false; - in @Override - protected voidonCreate (Bundle savedinstancestate) { to Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.player); -Intent Intent =getintent (); theinfo = (mp3info) Intent.getserializableextra ("Mp3info"); *BEGINBTN =(ImageButton) Findviewbyid (r.id.begin); $PAUSEBTN =(ImageButton) Findviewbyid (r.id.pause);Panax NotoginsengSTOPBTN =(ImageButton) Findviewbyid (r.id.stop); - theBeginbtn.setonclicklistener (NewBeginlistener ()); +Pausebtn.setonclicklistener (NewPauselistener ()); AStopbtn.setonclicklistener (NewStoplistener ()); the } + - classBeginlistenerImplementsOnclicklistener { $ @Override $ Public voidOnClick (View v) { - if(!isplaying) { -String Path =Getmp3path (Info.getmp3name ()); theMediaPlayer = Mediaplayer.create (playeractivity. This, Uri.parse ("file://" +path)); - Mediaplayer.start ();WuyiIsPlaying =true; theIsrelease =false; - } Wu } - } About $ classPauselistenerImplementsOnclicklistener { - @Override - Public voidOnClick (View v) { - if(MediaPlayer! =NULL) { A if(!isrelease) { + if(!ispause) { the mediaplayer.pause (); -Ispause =true; $}Else { the Mediaplayer.start (); theIspause =false; the } the } - } in } the } the About classStoplistenerImplementsOnclicklistener { the @Override the Public voidOnClick (View v) { the if(MediaPlayer! =NULL) { + if(isplaying) { - if(!isrelease) { the mediaplayer.stop ();Bayi mediaplayer.release (); theIsrelease =true; theIsPlaying =false; - } - } the } the } the } the - Privatestring Getmp3path (String mp3name) { theString Sdcartroot =environment.getexternalstoragedirectory (). GetAbsolutePath (); theString Path = sdcartroot + File.separator + "MP3" + File.separator +Mp3name; the returnpath;94 } the}
Android_mars Learning Note _s01 original version _023_mp3player003_ play mp3