Android MP3 Play , Download and Delete
1. Main.java
- package android.test;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import android.app.Activity;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageButton;
- public class Main extends Activity {
- private ImageButton m_playBtn, m_pauseBtn, m_restBtn, m_stopBtn;
- private MediaPlayer m_player;
- private boolean m_bIsReleased = false;
- private boolean m_bIsPaused = false;
- private boolean m_bIsStreaming = true;
- private String m_strFileURL = "http://www.yootiao.com/images/15.mp3";
- private String m_strTempFilePath = "";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- m_playBtn = (ImageButton) findViewById(R.id.playBtn);
- m_pauseBtn = (ImageButton) findViewById(R.id.pauseBtn);
- m_restBtn = (ImageButton) findViewById(R.id.resetBtn);
- m_stopBtn = (ImageButton) findViewById(R.id.stopBtn);
- m_playBtn.setOnClickListener(new ImageButton.OnClickListener() {
- @Override
- public void onClick(View v) {
- Play(m_strFileURL);
- LogI("MediaPlayer start playing");
- }
- });
- m_pauseBtn.setOnClickListener(new ImageButton.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (m_player != null) {
- if (!m_bIsReleased) {
- if (!m_bIsPaused) {
- m_player.pause();
- m_bIsPaused = true;
- } else {
- m_player.start();
- m_bIsPaused = false;
- }
- }
- }
- }
- });
- m_restBtn.setOnClickListener(new ImageButton.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (m_player != null) {
- if (!m_bIsReleased) {
- m_player.seekTo(0);
- LogI("MediaPlayer reset");
- }
- }
- }
- });
- m_stopBtn.setOnClickListener(new ImageButton.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (m_player != null) {
- if (!m_bIsReleased) {
- m_player.seekTo(0);
- m_player.pause();
- LogI("MediaPlayer stop");
- }
- }
- }
- });
- }
- private void Play(final String strURL) {
- if (m_player != null) {
- m_player.start();
- return;
- }
- m_player = new MediaPlayer();
- m_player.setAudioStreamType(AudioManager.STREAM_RING);
- m_player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
- @Override
- public boolean onError(MediaPlayer mp, int what, int extra) {
- LogI("Error on Listener, what: " + what + "extra: " + extra);
- return false;
- }
- });
- m_player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
- @Override
- public void onBufferingUpdate(MediaPlayer mp, int percent) {
- LogI("MediaPlayer Update buffer: " + Integer.toString(percent) + "%");
- }
- });
- m_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- LogI("MediaPlayer Listener Completed");
- }
- });
- m_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
- @Override
- public void onPrepared(MediaPlayer mp) {
- LogI("MediaPlayer Prepared Listener");
- }
- });
- Runnable r = new Runnable() {
- @Override
- public void run() {
- try {
- if (m_bIsStreaming) {
- m_player.setDataSource(Main.this, Uri.parse(m_strFileURL));
- } else {
- SetDataSource(strURL);
- }
- m_player.prepare();
- LogI("Duration: " + m_player.getDuration());
- m_player.start();
- m_bIsReleased = false;
- } catch (Exception e) {
- LogE(e.getMessage(), e);
- }
- }
- };
- new Thread(r).start();
- }
- private void SetDataSource(String strURL) throws Exception {
- if (!m_bIsReleased) {
- URL url = new URL(strURL);
- URLConnection conn = url.openConnection();
- InputStream in = conn.getInputStream();
- if (in == null)
- throw new RuntimeException("stream is null");
- File file = File.createTempFile("player_mp3_cache", ".mp3", Main.this.getCacheDir());
- if (!file.exists())
- file.createNewFile();
- m_strTempFilePath = file.getAbsolutePath();
- FileOutputStream fos = new FileOutputStream(file);
- byte buffer[] = new byte[128];
- do {
- int nNumRead = in.read(buffer);
- if (nNumRead <= 0)
- break;
- fos.write(buffer);
- } while (true);
- m_player.setDataSource(m_strTempFilePath);
- in.close();
- fos.close();
- }
- }
- private void DeleteFile(String strPath) {
- File file = new File(strPath);
- if (file.exists())
- file.delete();
- }
- private void LogI(String str) {
- Log.i("Play_Web_Mp3", str);
- }
- private void LogE(String str, Throwable tr) {
- Log.e("Play_Web_Mp3", str, tr);
- }
- @Override
- protected void onPause() {
- DeleteFile(m_strTempFilePath);
- super.onPause();
- }
- }
2. AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="android.test"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="7" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Main"
- 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>
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- </manifest>
No comments:
Post a Comment