1、音频采集
你可以使用手机进行现场录音,实现步骤如下:
第一步:在功能清单文件AndroidManifest.xml中添加音频刻录权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
第二步:编写音频刻录代码:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//从麦克风采集声音
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//内容输出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码方式
recorder.setOutputFile("/mnt/sdcard/itcast.amr");
recorder.prepare();//预期准备
recorder.start(); //开始刻录
...
recorder.stop();//停止刻录
recorder.reset(); //重设
recorder.release(); //刻录完成一定要释放资源
2、音乐播放
MediaPlayer mediaPlayer = new MediaPlayer();
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();//重置为初始状态
}
mediaPlayer.setDataSource("/mnt/sdcard/god.mp3");
mediaPlayer.prepare();
mediaPlayer.start();//开始或恢复播放
mediaPlayer.pause();//暂停播放
mediaPlayer.start();//恢复播放
mediaPlayer.stop();//停止播放
mediaPlayer.release();//释放资源
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//播出完毕事件
@Override public void onCompletion(MediaPlayer arg0) {
mediaPlayer.release();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {// 错误处理事件
@Override public boolean onError(MediaPlayer player, int arg1, int arg2) {
mediaPlayer.release();
return false;
}
});
3、使用SoundPool播放音效
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的游戏开发中。
在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。
SoundPool(android.media.SoundPool),顾名思义是声音池的意思,主要用于播放一些较短的声音片段,支持从程序的资源或文件系统加载。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、播放比率等参数,支持通过ID对多个音频流进行管理。
就现在已知的资料来说,SoundPool有一些设计上的BUG,从固件版本1.0开始有些还没有修复,我们在使用中应该小心再小心。相信将来Google会修复这些问题,但我们最好还是列出来:
1. SoundPool最大只能申请1M的内存空间,这就意味着我们只能用一些很短的声音片段,而不是用它来播放歌曲或者做游戏背景音乐。
2. SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用,因为有些时候它们可能会使你的程序莫名其妙的终止。建议使用这两个方法的时候尽可能多做测试工作,还有些朋友反映它们不会立即中止播放声音,而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。
3. SoundPool的效率问题。其实SoundPool的效率在这些播放类中算是很好的了,但是有的朋友在G1中测试它还是有100ms左右的延迟,这可能会影响用户体验。也许这不能管SoundPool本身,因为到了性能比较好的Droid中这个延迟就可以让人接受了。
在现阶段SoundPool有这些缺陷,但也有着它不可替代的优点,基于这些我们建议大在如下情况中多使用SoundPool:1.应用程序中的声效(按键提示音,消息等)2.游戏中密集而短暂的声音(如多个飞船同时爆炸)
开发步骤:
1> 往项目的res/raw目录中放入音效文件。
2> 新建SoundPool对象,然后调用SoundPool.load()加载音效,调用SoundPool.play()方法播放指定音效文件。
public class AudioActivity extends Activity {
private SoundPool pool;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//指定声音池的最大音频流数目为10,声音品质为5
pool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
final int sourceid = pool.load(this, R.raw.pj, 0);//载入音频流,返回在池中的id
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//播放音频,第二个参数为左声道音量;第三个参数为右声道音量;第四个参数为优先级;第五个参数为循环次数,0不循环,-1循环;第六个参数为速率,速率最低0.5最高为2,1代表正常速度
pool.play(sourceid, 1, 1, 0, -1, 1);
}
});
}
}
示例代码:
package com.ithiema.soundpool;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private SoundPool soundPool;
private int soundId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化声音池
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// 加载声音到声音池
soundId = soundPool.load(this, R.raw.la, 1);
}
public void click(View view) {
soundPool.play(soundId, 1.0f, 1.0f, 0, 0, 1);
}
}
4、视频播放
在main.xml布局文件添加用于视频画面绘制的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />
SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.reset();//重置为初始状态
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
/* 设置Video影片以SurfaceHolder播放 */
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setDataSource("/mnt/sdcard/oppo.mp4");
mediaPlayer.prepare();
mediaPlayer.start();//播放
mediaPlayer.pause();//暂停播放
mediaPlayer.start();//恢复播放
mediaPlayer.stop();//停止播放
mediaPlayer.release();//释放资源
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class VideoActivity extends Activity {
private static final String TAG = "VideoActivity";
private EditText filenameText;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
private String filename;//当前播放文件的名称
private int position;//记录播放位置
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mediaPlayer = new MediaPlayer();
this.filenameText = (EditText) this.findViewById(R.id.filename);
this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
ImageButton playButton = (ImageButton) this.findViewById(R.id.play);
ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause);
ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset);
ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);
ButtonClickListener listener = new ButtonClickListener();
playButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
resetButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
this.surfaceView.getHolder().setKeepScreenOn(true);
this.surfaceView.getHolder().addCallback(new SurfaceListener());
}
private class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.play://来自播放按钮
filename = filenameText.getText().toString();
play();
break;
case R.id.pause://来自暂停按钮
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
break;
case R.id.reset://来自重新播放按钮
if(!mediaPlayer.isPlaying()) play();
mediaPlayer.seekTo(0);
break;
case R.id.stop://来自停止按钮
if(mediaPlayer.isPlaying()) mediaPlayer.stop();
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
/**
* 播放视频
*/
private void play() throws IOException {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/mnt/sdcard/"+ filename);//设置需要播放的视频
mediaPlayer.setDisplay(surfaceView.getHolder());//把视频画面输出到SurfaceView
mediaPlayer.prepare();
mediaPlayer.start();
}
private class SurfaceListener implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {//方法在onResume()后被调用
Log.i(TAG, "surfaceCreated()");
if(position>0 && filename!=null){
try {
play();
mediaPlayer.seekTo(position);
position = 0;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "surfaceDestroyed()");
}
}
@Override
protected void onPause() {//当其他Activity被打开,停止播放
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();//得到播放位置
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onDestroy() {
if(mediaPlayer.isPlaying()) mediaPlayer.stop();
mediaPlayer.release();
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filename"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="oppo.mp4"
android:id="@+id/filename"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
android:id="@+id/play"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause"
android:id="@+id/pause"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset"
android:id="@+id/reset"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"
android:id="@+id/stop"
/>
</LinearLayout>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="240dip"
android:id="@+id/surfaceView"
/>
</LinearLayout>
5、使用摄像头拍照
在main.xml布局文件添加用于显示取景画面的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />
SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = Camera.open();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();//开始预览
camera.autoFocus(null);//自动对焦
camera.takePicture(null, null, null, jpegCallback);//拍照片
camera.stopPreview();//停止预览
camera.release();//释放摄像头
<uses-permission android:name="android.permission.CAMERA"/>
package cn.itcast.picture;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class TakePictureActivity extends Activity {
private static final String TAG = "TakePictureActivity";
private SurfaceView surfaceView;
private Camera camera;
private boolean preview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//高亮
setContentView(R.layout.main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(new SufaceListener());
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
}
private final class SufaceListener implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();//打开摄像头
Camera.Parameters parameters = camera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
if(preview) camera.stopPreview();
camera.release();
camera = null;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(camera!=null && event.getRepeatCount()==0){
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH:
camera.autoFocus(null);//自动对焦
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_CAMERA:
//拍照
camera.takePicture(null, null, new PictureCallbackListener());
break;
}
}
return true;
}
private final class PictureCallbackListener implements Camera.PictureCallback{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File(Environment.getExternalStorageDirectory(), "itcast.jpg");
FileOutputStream outStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outStream);
outStream.close();
//重新浏览
camera.stopPreview();
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
5、音视频采集
第一步:在功能清单文件AndroidManifest.xml中添加音频刻录和照相机权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第二步:编写音频刻录代码:
recorder.reset();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(320, 240);
recorder.setVideoFrameRate(3); //每秒3帧
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/mnt/sdcard/itcast.3gp");
recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
recorder.prepare();//预期准备
recorder.start();//开始刻录
...
recorder.stop();//停止刻录
recorder.release(); //刻录完成一定要释放资源
package cn.itcast.video;
import android.app.Activity;
import android.content.Context;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class VideoRecordActivity extends Activity {
private static final String TAG = "VideoRecordActivity";
private Button startButton;
private Button stopButton;
private boolean record;
private MediaRecorder recorder;
private SurfaceView surfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
recorder = new MediaRecorder();
ButtonListener listener = new ButtonListener();
startButton = (Button)this.findViewById(R.id.start);
stopButton = (Button)this.findViewById(R.id.stop);
this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
startButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
stopButton.setEnabled(false);//不可用
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(320, 240);//设置分辨率
this.surfaceView.getHolder().setKeepScreenOn(true);
}
private final class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.start:
stopButton.setEnabled(true);//设置可用
startButton.setEnabled(false);
recorder.reset();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(320, 240);
recorder.setVideoFrameRate(3); //每秒3帧
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/mnt/sdcard/itcast.3gp");
recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
recorder.prepare();//预期准备
recorder.start();//开始刻录
record = true;
break;
case R.id.stop:
stopButton.setEnabled(false);
startButton.setEnabled(true);
if(record){
recorder.stop();//停止刻录
record = false;
}
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
6、Mp3播放器
package com.itheima.mp3player;
import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button bt_play;
private Button bt_pause;
private Button bt_stop;
private Button bt_replay;
private MediaPlayer mediaPlayer ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_play = (Button) findViewById(R.id.bt_play);
bt_pause = (Button) findViewById(R.id.bt_pause);
bt_stop = (Button) findViewById(R.id.bt_stop);
bt_replay = (Button) findViewById(R.id.bt_replay);
bt_play.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_stop.setOnClickListener(this);
bt_replay.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
}
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.bt_play:
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
System.out.println("播放失败...失败的状态码:"+what);
return false;
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
System.out.println("播放完毕了..");
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/sdcard/aaa.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
break;
case R.id.bt_pause:
if("已经暂停".equals(bt_pause.getText().toString())){
mediaPlayer.start();
bt_pause.setText("暂停");
return;
}
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
bt_pause.setText("已经暂停");
}
break;
case R.id.bt_stop:
mediaPlayer.stop();
mediaPlayer.reset();
break;
case R.id.bt_replay:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/sdcard/aaa.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
7、播放在线音乐
package com.example.playonlinemusic;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_path;
private MediaPlayer mediaPlayer;
private SeekBar seekBar1;
private Button bt_pause;
private Timer timer;
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
bt_pause = (Button) findViewById(R.id.bt_pause);
}
public void play(View view){
try {
String path = et_path.getText().toString().trim();
if(TextUtils.isEmpty(path)){
Toast.makeText(getApplicationContext(), "路径不能为空", 0).show();
return;
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(path);
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "播放失败:"+what, 0).show();
return false;
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
//网络上的资源 准备需要花费一定的时间
//异步的准备
mediaPlayer.prepareAsync();
//注册一个准备完毕的监听器
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
int max = mediaPlayer.getDuration();
seekBar1.setMax(max);
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
//得到当前播放的进度
int position = mediaPlayer.getCurrentPosition();
seekBar1.setProgress(position);
}
};
timer.schedule(task, 1000, 500);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void pause(View view){
if("继续".equals(bt_pause.getText().toString().trim())){
mediaPlayer.start();
bt_pause.setText("暂停");
return;
}
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
bt_pause.setText("继续");
}
}
public void stop(View view){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
if(timer!=null&&task!=null){
timer.cancel();
task.cancel();
timer = null;
task = null;
}
}
}
public void replay(View view){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}
}
@Override
protected void onDestroy() {
if(timer!=null&&task!=null){
timer.cancel();
task.cancel();
timer = null;
task = null;
}
super.onDestroy();
}
}
8、视频播放器
package com.example.playonlinemusic;
import java.util.Timer;
import java.util.TimerTask;
import com.example.playvideo.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
private EditText et_path;
private MediaPlayer mediaPlayer;
private SeekBar seekBar1;
private Button bt_pause;
private VideoView vv;
private Timer timer;
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
bt_pause = (Button) findViewById(R.id.bt_pause);
vv = (VideoView) findViewById(R.id.vv);
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
vv.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
}
public void play(View view) {
String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(getApplicationContext(), "路径不能为空", 0).show();
return;
}
vv.setVideoPath(path);
vv.start();
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
seekBar1.setMax(vv.getDuration());
System.out.println("视频的总时间:"+vv.getDuration());
int position = vv.getCurrentPosition();
seekBar1.setProgress(position);
System.out.println(position);
}
};
timer.schedule(task, 1000, 500);
}
public void pause(View view) {
if("继续".equals(bt_pause.getText().toString())){
vv.start();
return;
}
if(vv.isPlaying()){
vv.pause();
bt_pause.setText("继续");
}
}
public void stop(View view) {
vv.stopPlayback();
if(timer!=null&&task!=null){
timer.cancel();
task.cancel();
timer = null;
task = null;
}
}
public void replay(View view) {
vv.seekTo(0);
}
@Override
protected void onDestroy() {
if(timer!=null&&task!=null){
timer.cancel();
task.cancel();
timer = null;
task = null;
}
super.onDestroy();
}
}
9、SurfaceView播放视频
package com.example.playonlinemusic;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import com.example.playvideo.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
private EditText et_path;
private MediaPlayer mediaPlayer;
private SeekBar seekBar1;
private Button bt_pause;
private SurfaceView sv;
private SurfaceHolder holder;
private Timer timer;
private TimerTask task;
private int position;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
bt_pause = (Button) findViewById(R.id.bt_pause);
sv = (SurfaceView) findViewById(R.id.sv);
//获取到界面显示的容器
holder = sv.getHolder();
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("holder 被销毁了");
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("holder 被创建了.");
if(position>0){
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
mediaPlayer.seekTo(position);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("播放器的大小变化了..");
}
});
}
public void play(View view) {
path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(getApplicationContext(), "路径不能为空", 0).show();
return;
}
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void pause(View view) {
if("继续".equals(bt_pause.getText().toString())){
mediaPlayer.start();
return;
}
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
bt_pause.setText("继续");
}
}
public void stop(View view) {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
}
public void replay(View view) {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}