Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150505
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2015-01-29 16:49:24

一.使用MediaPlayer播放音乐的demo
1. 添加权限
因为音乐文件在sdcard中,所以需要添加外置sd卡的访问权限
AndroidManifest.xml
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2. demo代码如下
  1. package com.example.MediaPlay;

  2. import java.io.File;
  3. import com.example.MediaPlay.R;
  4. import android.app.Activity;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.media.AudioManager;
  8. import android.media.MediaPlayer;
  9. import android.media.MediaPlayer.OnCompletionListener;
  10. import android.media.MediaPlayer.OnPreparedListener;
  11. import android.os.Bundle;

  12. public class MediaPlay extends Activity
  13. {
  14.     private String TAG = "cong";
  15.     private Button btn_play, btn_pause, btn_stop;
  16.     private MediaPlayer mediaPlayer;
  17.     private String File_Path = "/mnt/sdcard/Music/letitgo.mp3";     //音乐文件的路径
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState)
  20.     {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.         btn_play = (Button) findViewById(R.id.btn_play);
  24.         btn_pause = (Button) findViewById(R.id.btn_pause);
  25.         btn_stop = (Button) findViewById(R.id.btn_stop);

  26.         btn_play.setOnClickListener(click);
  27.         btn_pause.setOnClickListener(click);
  28.         btn_stop.setOnClickListener(click);
  29.     }
  30.     private View.OnClickListener click = new View.OnClickListener() {
  31.         @Override
  32.         public void onClick(View v) {
  33.             switch (v.getId()) {
  34.             case R.id.btn_play:
  35.                 play();
  36.                 break;
  37.             case R.id.btn_pause:
  38.                 pause();
  39.                 break;
  40.             case R.id.btn_stop:
  41.                 stop();
  42.                 break;
  43.             default:
  44.                 break;
  45.             }
  46.         }
  47.     };
  48.     
  49.     protected void play() {
  50.         File file = new File(File_Path);    //确认音乐文件的存在
  51.         if(!file.exists())
  52.             return ;
  53.         try {
  54.             mediaPlayer = new MediaPlayer();
  55.             mediaPlayer.setDataSource(File_Path);
  56.             mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  57.             mediaPlayer.prepareAsync();       //异步的方式加载音乐文件
  58.             mediaPlayer.setOnPreparedListener(new OnPreparedListener() //异步加载完音乐文件后会回调此
  59.                 @Override
  60.                 public void onPrepared(MediaPlayer mp) {
  61.                     mediaPlayer.start();               //准备好之后才能start
  62.                     btn_play.setEnabled(false);
  63.                 }
  64.             });
  65.             mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
  66.                 @Override
  67.                 public void onCompletion(MediaPlayer mp) {
  68.                     btn_play.setEnabled(true);             //播放完成后btn_play可用
  69.                 }
  70.             });
  71.         } catch (Exception e) {
  72.             e.printStackTrace();
  73.         }        
  74.     }

  75.     protected void pause() {
  76.         if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
  77.             mediaPlayer.start();
  78.             btn_pause.setText("pause");
  79.             return;
  80.         }
  81.         if (mediaPlayer != null && mediaPlayer.isPlaying()) {
  82.             mediaPlayer.pause();
  83.             btn_pause.setText("continue");
  84.         }
  85.     }

  86.     protected void stop() {
  87.         if (mediaPlayer != null && mediaPlayer.isPlaying()) {
  88.             mediaPlayer.stop();
  89.             mediaPlayer.release();
  90.             mediaPlayer = null;
  91.             btn_play.setEnabled(true);
  92.         }
  93.     }

  94.     @Override
  95.     protected void onDestroy() {
  96.         if (mediaPlayer != null && mediaPlayer.isPlaying()) {
  97.             mediaPlayer.stop();
  98.             mediaPlayer.release();
  99.             mediaPlayer = null;
  100.         }
  101.         super.onDestroy();
  102.     }
  103. }
media.rar (下载后改名为media.tar.gz解压)
阅读(3429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~