码迷,mamicode.com
首页 > 移动开发 > 详细

Android MediaPlayer

时间:2015-03-06 15:35:31      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

import android.media.MediaPlayer;
import android.media.MediaPlayer.*;

private MediaPlayer mediaPlayer;

OnCreate:

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
}
});


mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
mediaPlayer.release();
return true;
}
});

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});

try {
String path = Environment.getExternalStorageDirectory().getPath().concat("/com.buzz.exhibition/audio/a.mp3");
//Log.i(TAG, path);
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
} catch (Exception ex) {
Log.i(TAG, ex.toString());
}

onDestroy:

mediaPlayer.release();

 

Android MediaPlayer

标签:

原文地址:http://www.cnblogs.com/ncore/p/4318049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!