相关文章推荐
潇洒的薯片  ·  mpdf ...·  3 周前    · 
善良的鞭炮  ·  内容滚动条和子 div ...·  1 年前    · 
低调的水龙头  ·  html - Form Submit ...·  1 年前    · 

使用SurfaceView播放视频,其实很简单,但是经常会碰见有声音没图像的问题.

其实使用SurfaceView播放视频只需要主要下面几个地方就可以了

1. surfaceChanged也就是Suface是否创建成功

2. onPrepared 也就是MideoPlayer是否加载并准备完成

3. surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 类型必须是SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS

java代码:

1 package VideoTest.Test;
2
3  import java.io.FileDescriptor;
4
5 import android.app.Activity;
6 import android.content.res.AssetFileDescriptor;
7 import android.media.AudioManager;
8 import android.media.MediaPlayer;
9 import android.media.MediaPlayer.OnBufferingUpdateListener;
10 import android.media.MediaPlayer.OnCompletionListener;
11 import android.media.MediaPlayer.OnPreparedListener;
12 import android.os.Bundle;
13 import android.view.SurfaceHolder;
14 import android.view.SurfaceView;
15
16
17 public class VideoTest extends Activity implements SurfaceHolder.Callback,
18 OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener{
19
20 private SurfaceView surfaceView;
21 private SurfaceHolder surfaceHolder;
22 private MediaPlayer mediaPlayer;
23
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27
28 this.surfaceView = new SurfaceView(this);
29 this.surfaceHolder = this.surfaceView.getHolder();
30 this.surfaceHolder.addCallback(this);
31
32 //类型必须设置成SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS
33 this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
34 setContentView(this.surfaceView);
35 }
36
37 @Override
38 public void onDestroy() {
39 super.onDestroy();
40 if (this.mediaPlayer != null) {
41 this.mediaPlayer.release();
42 this.mediaPlayer = null;
43 }
44 }
45
46 public void PlayVideo(){
47
48 try {
49 //视频文件放在 assert中
50 AssetFileDescriptor aFD = this.getAssets().openFd("welcome.3gp");
51 FileDescriptor fileDescriptor = aFD.getFileDescriptor();
52 this.mediaPlayer = new MediaPlayer();
53 this.mediaPlayer.setDataSource(fileDescriptor, aFD.getStartOffset(), aFD.getLength());
54 aFD.close();
55
56 //视频放在SDCard中
57 //File sdFile = Environment.getExternalStorageDirectory();
58 //this.mediaPlayer = new MediaPlayer();
59 //this.mediaPlayer.setDataSource(sdFile.getAbsoluteFile() + File.separator + "welcome.3gp");
60
61 //视频放在res中
62 //this.mediaPlayer = MediaPlayer.create(this, R.raw.welcome);
63
64 //网上的视频,没有测试
65 //this.mediaPlayer
66 // .setDataSource("http://xy2.163.com/download/down/wukong.mp3");
67
68 this.mediaPlayer.setDisplay(this.surfaceHolder);
69 this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
70 //准备完成后才可以播放,另外如果文件特别大或者从网上获得的资源
71 //会在这里等待时间过长,造成堵塞,这样的话就得用
72 //this.mediaPlayer.prepareAsync(),然后监听是否准备完毕在开始
73
74 this.mediaPlayer.prepare();
75 this.mediaPlayer.start();
76
77 } catch (Exception e) {
78 // TODO: handle exception
79 }
80 }
81
82 @Override
83 public void surfaceChanged(SurfaceHolder holder, int format, int width,
84 int height) {
85
86 }
87
88 @Override
89 public void surfaceCreated(SurfaceHolder holder) {
90 //SurfaceCreated 创建成功才可以播放视频
91 PlayVideo();
92 }
93
94 @Override
95 public void surfaceDestroyed(SurfaceHolder holder) {
96
97 }
98
99 //@Override
100 public void onBufferingUpdate(MediaPlayer mp, int percent) {
101 // TODO Auto-generated method stub
102
103 }
104
105 //@Override
106 public void onCompletion(MediaPlayer mp) {
107 // TODO Auto-generated method stub
108
109 }
110
111 //@Override
112 public void onPrepared(MediaPlayer mp) {
113 //MediaPlayer加载准备完毕才能开始播放
114 //mp.start();
115 }
116
117 }