The path to the java siege lion (Android)-MP3, MP4, photography, internationalization, style themes, image movement and scaling, android-mp3

Source: Internet
Author: User

The path to the java siege lion (Android)-MP3, MP4, photography, internationalization, style themes, image movement and scaling, android-mp3

I. MP3 player

View the Android API documentation to see the MediaPlayer status transition diagram:

Exercise:

Package com.shellwaygateplayer; import java. io. file; import java. io. IOException; import android. support. v7.app. actionBarActivity; import android. telephony. phoneStateListener; import android. telephony. telephonyManager; import android. content. context; import android. media. mediaPlayer; import android. media. mediaPlayer. onPreparedListener; import android. OS. bundle; import android. OS. environment; import android. uti L. log; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends ActionBarActivity {// Media Player private MediaPlayer mp; private EditText songName; // whether private boolean pause has been suspended; private File file; private TelephonyManager tm; // pause the save point private int position = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); songName = (EditText) findViewById (R. id. song_name); tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); tm. listen (new MyPhoneStateListener (), PhoneStateListener. LISTEN_CALL_STATE);} private class MyPhoneStateListener extends PhoneStateListene R {@ Override public void onCallStateChanged (int state, String incomingNumber) {// TODO Auto-generated method stub super. onCallStateChanged (state, incomingNumber); switch (state) {case TelephonyManager. CALL_STATE_IDLE: // idle status if (position> 0 & mp! = Null) {// jump to the specified storage point location mp. seekTo (position); mp. start ();} break; case TelephonyManager. CALL_STATE_OFFHOOK: // Answer status if (mp! = Null) {if (mp. isPlaying () {position = mp. getCurrentPosition (); mp. pause () ;}} break; case TelephonyManager. CALL_STATE_RINGING: // if (mp. isPlaying () {position = mp. getCurrentPosition (); mp. pause () ;}break; default: break ;}/// the Activity life cycle method is used to listen for paused playback when calling. // @ Override // protected void onResume () {// TODO Auto-generated method stub // super. onResume (); // if (position> 0) {// jump to the specified position // Mp. seekTo (position); // mp. start (); //} // @ Override // protected void onPause () {// TODO Auto-generated method stub // super. onPause (); // position = mp. getCurrentPosition (); // mp. pause (); //} public void play (View view) {String name = songName. getText (). toString (); file = new File (Environment. getExternalStorageDirectory (), name + ". mp3 "); if (! File. exists () {Toast. makeText (this, "the file does not exist", Toast. LENGTH_SHORT). show (); mp = null;} else {if (mp! = Null) {if (mp. isPlaying () {Toast. makeText (this, "the song is playing", Toast. LENGTH_SHORT ). show () ;}else {play () ;}} else {play () ;}} public void play () {try {mp = new MediaPlayer (); // set it to the Initial State mp. reset (); // specifies the file mp to be played. setDataSource (file. getAbsolutePath (); // prepare (buffer) mp. prepare (); // set the buffer to complete the listener mp. setOnPreparedListener (new MyOnPreparedListener ();} catch (Exception e) {// TODO Auto-generated catch block E. printStackTrace () ;}} private class MyOnPreparedListener implements OnPreparedListener {@ Override public void onPrepared (MediaPlayer mp) {// TODO Auto-generated method stub // play mp. start () ;}} public void pause (View view) {if (mp. isPlaying () {// pause Playing mp. pause (); Button bt = (Button) view; bt. setText ("continue"); pause = true;} else {if (pause) {mp. start (); Button bt = (Button) view; bt. setText ("Stop Stop ") ;}} public void stop (View view) {if (mp! = Null) {// stop Playing mp. stop () ;}} public void reset (View view) {if (mp! = Null) {mp. start () ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); if (mp! = Null) {mp. release (); mp = null ;}}}MainActivity. java <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Enter the song name"/> <EditText android: id = "@ + id/song_name" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Complicated"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: onClick = "play" android: text = "play"/> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: onClick = "pause" android: text = "pause"/> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: onClick = "stop" android: text = "stop"/> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: onClick = "reset" android: text = "replay"/> </LinearLayout>Activity_main.xml <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com.shellwaygateplayer" android: versionCode = "1" android: versionName = "1.0"> <uses-sdk android: minSdkVersion = "8" android: targetSdkVersion = "21"/> <! -- The permission to read the call status is required to listen to the call status --> <uses-permission android: name = "android. permission. READ_PHONE_STATE "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>AndroidManifest. xml

Running result:

Ii. MP4 Player

 

 

 

 

 

Wait, try to update ....

 

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.