Monday, April 30, 2012

Android MP3 Play , Download and Delete

Android MP3 Play , Download and Delete 


1. Main.java

  1. package android.test;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.InputStream;  
  5. import java.net.URL;  
  6. import java.net.URLConnection;  
  7. import android.app.Activity;  
  8. import android.media.AudioManager;  
  9. import android.media.MediaPlayer;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.widget.ImageButton;  
  15. public class Main extends Activity {  
  16.     private ImageButton m_playBtn, m_pauseBtn, m_restBtn, m_stopBtn;  
  17.     private MediaPlayer m_player;  
  18.     private boolean m_bIsReleased = false;  
  19.     private boolean m_bIsPaused = false;  
  20.     private boolean m_bIsStreaming = true;  
  21.   
  22.     private String m_strFileURL = "http://www.yootiao.com/images/15.mp3";  
  23.     private String m_strTempFilePath = "";  
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         m_playBtn = (ImageButton) findViewById(R.id.playBtn);  
  30.         m_pauseBtn = (ImageButton) findViewById(R.id.pauseBtn);  
  31.         m_restBtn = (ImageButton) findViewById(R.id.resetBtn);  
  32.         m_stopBtn = (ImageButton) findViewById(R.id.stopBtn);  
  33.         m_playBtn.setOnClickListener(new ImageButton.OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 Play(m_strFileURL);  
  37.                 LogI("MediaPlayer start playing");  
  38.             }  
  39.         });  
  40.         m_pauseBtn.setOnClickListener(new ImageButton.OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 if (m_player != null) {  
  44.                     if (!m_bIsReleased) {  
  45.                         if (!m_bIsPaused) {  
  46.                             m_player.pause();  
  47.                             m_bIsPaused = true;  
  48.                         } else {  
  49.                             m_player.start();  
  50.                             m_bIsPaused = false;  
  51.                         }  
  52.                     }  
  53.                 }  
  54.             }  
  55.         });  
  56.         m_restBtn.setOnClickListener(new ImageButton.OnClickListener() {  
  57.             @Override  
  58.             public void onClick(View v) {  
  59.                 if (m_player != null) {  
  60.                     if (!m_bIsReleased) {  
  61.                         m_player.seekTo(0);  
  62.                         LogI("MediaPlayer reset");  
  63.                     }  
  64.                 }  
  65.             }  
  66.         });  
  67.         m_stopBtn.setOnClickListener(new ImageButton.OnClickListener() {  
  68.             @Override  
  69.             public void onClick(View v) {  
  70.                 if (m_player != null) {  
  71.                     if (!m_bIsReleased) {  
  72.                         m_player.seekTo(0);  
  73.                         m_player.pause();  
  74.                         LogI("MediaPlayer stop");  
  75.                     }  
  76.                 }  
  77.             }  
  78.         });  
  79.     }  
  80.     private void Play(final String strURL) {  
  81.         if (m_player != null) {  
  82.             m_player.start();  
  83.             return;  
  84.         }  
  85.         m_player = new MediaPlayer();  
  86.         m_player.setAudioStreamType(AudioManager.STREAM_RING);  
  87.         
  88.         m_player.setOnErrorListener(new MediaPlayer.OnErrorListener() {  
  89.             @Override  
  90.             public boolean onError(MediaPlayer mp, int what, int extra) {  
  91.                 LogI("Error on Listener, what: " + what + "extra: " + extra);  
  92.                 return false;  
  93.             }  
  94.         });  
  95.      
  96.         m_player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {  
  97.             @Override  
  98.             public void onBufferingUpdate(MediaPlayer mp, int percent) {  
  99.                 LogI("MediaPlayer Update buffer: " + Integer.toString(percent) + "%");  
  100.             }  
  101.         });  
  102.  
  103.         m_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {  
  104.             @Override  
  105.             public void onCompletion(MediaPlayer mp) {  
  106.                 LogI("MediaPlayer Listener Completed");  
  107.             }  
  108.         });  

  109.         m_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {  
  110.             @Override  
  111.             public void onPrepared(MediaPlayer mp) {  
  112.                 LogI("MediaPlayer Prepared Listener");  
  113.             }  
  114.         });  
  115.         Runnable r = new Runnable() {  
  116.             @Override  
  117.             public void run() {  
  118.                 try {  
  119.                     if (m_bIsStreaming) {  
  120.                         m_player.setDataSource(Main.this, Uri.parse(m_strFileURL));  
  121.                     } else {  
  122.                         SetDataSource(strURL);  
  123.                     }  
  124.                     m_player.prepare();  
  125.                     LogI("Duration: " + m_player.getDuration());  
  126.                     m_player.start();  
  127.                     m_bIsReleased = false;  
  128.                 } catch (Exception e) {  
  129.                     LogE(e.getMessage(), e);  
  130.                 }  
  131.             }  
  132.         };  
  133.         new Thread(r).start();  
  134.     }  
  135.     private void SetDataSource(String strURL) throws Exception {  
  136.         if (!m_bIsReleased) {  
  137.             URL url = new URL(strURL);  
  138.             URLConnection conn = url.openConnection();  
  139.             InputStream in = conn.getInputStream();  
  140.             if (in == null)  
  141.                 throw new RuntimeException("stream is null");  
  142.             File file = File.createTempFile("player_mp3_cache"".mp3", Main.this.getCacheDir());  
  143.             if (!file.exists())  
  144.                 file.createNewFile();  
  145.             m_strTempFilePath = file.getAbsolutePath();  
  146.             FileOutputStream fos = new FileOutputStream(file);  
  147.             byte buffer[] = new byte[128];  
  148.             do {  
  149.                 int nNumRead = in.read(buffer);  
  150.                 if (nNumRead <= 0)  
  151.                     break;  
  152.                 fos.write(buffer);  
  153.             } while (true);  
  154.             m_player.setDataSource(m_strTempFilePath);  
  155.             in.close();  
  156.             fos.close();  
  157.         }  
  158.     }  
  159.     private void DeleteFile(String strPath) {  
  160.         File file = new File(strPath);  
  161.         if (file.exists())  
  162.             file.delete();  
  163.     }  
  164.     private void LogI(String str) {  
  165.         Log.i("Play_Web_Mp3", str);  
  166.     }  
  167.     private void LogE(String str, Throwable tr) {  
  168.         Log.e("Play_Web_Mp3", str, tr);  
  169.     }  
  170.     @Override  
  171.     protected void onPause() {  

  172.         DeleteFile(m_strTempFilePath);  
  173.         super.onPause();  
  174.     }  
  175. }  



2.  AndroidManifest.xml 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="android.test"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="7" />  
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.         <activity android:name=".Main"  
  9.                   android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.     </application>  
  16.     <uses-permission android:name="android.permission.INTERNET"/>  
  17.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  18.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>      
  19. </manifest>  





No comments: