Android 学习笔记(十四): MediaPlayer 播放音频

MediaPlayer类提供了播放、暂停、停止、和重复播放等方法,每次只能播放一个音频文件,适合播放较大文件,

用法:

1、从资源文件中播放,

[java] view plaincopy
MediaPlayer  player1  =   new MediaPlayer().create(this,R.raw.message);  
player1.start();  

2、从文件系统播放,

[java] view plaincopy
     MediaPlayer   player  =   new MediaPlayer();  
     String  path   =  "/sdcard/message.mp3";  
     try {  
player.setDataSource(path);  
player.prepare();  
 catch (IOException e) {  
e.printStackTrace();  
  
     player.start();  

 3、从网络播放,

[java] view plaincopy
String path="http://*************.mp3";  
Uri  uri  =  Uri.parse(path);  
MediaPlayer  player  =   new MediaPlayer().create(this,uri);  
player.start();  
posted @ 2012-04-13 23:56  生活不是用来挥霍的  阅读(421)  评论(0编辑  收藏  举报