视频播放-SurfaceView

Posted zhongyinghe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视频播放-SurfaceView相关的知识,希望对你有一定的参考价值。

1、视图

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <EditText
 9         android:id="@+id/et_path"
10         android:hint="请输入视频文件"
11         android:layout_width="fill_parent"
12         android:layout_height="wrap_content"
13         android:text="/sdcard/mm.mp4"
14         />
15 
16     <SeekBar
17         android:id="@+id/sb" 
18         android:layout_width="fill_parent"
19         android:layout_height="wrap_content" />
20 
21     <LinearLayout 
22         android:layout_width="fill_parent"
23         android:layout_height="wrap_content"
24         android:orientation="horizontal"
25         >
26         <Button 
27              android:id="@+id/bt_play"
28              android:layout_weight="1"
29              android:layout_width="0dip"
30              android:layout_height="wrap_content"
31              android:text="播放"
32             />
33          <Button
34               android:id="@+id/bt_pause" 
35              android:layout_weight="1"
36              android:layout_width="0dip"
37              android:layout_height="wrap_content"
38              android:text="暂停"
39             />
40           <Button 
41               android:id="@+id/bt_replay"
42              android:layout_weight="1"
43              android:layout_width="0dip"
44              android:layout_height="wrap_content"
45              android:text="重播"
46             />
47            <Button
48               android:id="@+id/bt_stop" 
49              android:layout_weight="1"
50              android:layout_width="0dip"
51              android:layout_height="wrap_content"
52              android:text="停止"
53             />
54         
55     </LinearLayout>
56     
57     <SurfaceView 
58         android:id="@+id/sv"
59         android:layout_width="fill_parent"
60         android:layout_height="wrap_content"
61         />
62 
63 </LinearLayout>

2、android代码

  1 package com.example.videoplay;
  2 
  3 import java.io.File;
  4 
  5 import android.media.AudioManager;
  6 import android.media.MediaPlayer;
  7 import android.media.MediaPlayer.OnCompletionListener;
  8 import android.os.Bundle;
  9 import android.app.Activity;
 10 import android.view.Menu;
 11 import android.view.SurfaceHolder;
 12 import android.view.SurfaceHolder.Callback;
 13 import android.view.SurfaceView;
 14 import android.view.View;
 15 import android.view.View.OnClickListener;
 16 import android.widget.Button;
 17 import android.widget.EditText;
 18 import android.widget.SeekBar;
 19 import android.widget.SeekBar.OnSeekBarChangeListener;
 20 import android.widget.Toast;
 21 
 22 public class MainActivity extends Activity implements OnClickListener{
 23     private EditText et_path;
 24     private Button bt_play,bt_pause,bt_replay,bt_stop;
 25     private MediaPlayer mediaPlayer;
 26     private SurfaceView sv;
 27     private int currentPosition;
 28     private SeekBar sb;
 29     private boolean isplaying;
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_main);
 34         
 35         et_path = (EditText) findViewById(R.id.et_path);
 36         bt_play = (Button) findViewById(R.id.bt_play);
 37         bt_pause = (Button) findViewById(R.id.bt_pause);
 38         bt_replay = (Button) findViewById(R.id.bt_replay);
 39         bt_stop = (Button) findViewById(R.id.bt_stop);
 40         
 41         sb = (SeekBar) findViewById(R.id.sb);
 42         
 43         //监听进度条拖动改变状态
 44         sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
 45             //当拖动停止时的调用的方法
 46             @Override
 47             public void onStopTrackingTouch(SeekBar seekBar) {
 48                 int process = sb.getProgress();
 49                 if(mediaPlayer != null && mediaPlayer.isPlaying()){
 50                     mediaPlayer.seekTo(process);
 51                 }
 52                 
 53             }
 54             
 55             @Override
 56             public void onStartTrackingTouch(SeekBar seekBar) {
 57                 // TODO Auto-generated method stub
 58                 
 59             }
 60             
 61             @Override
 62             public void onProgressChanged(SeekBar seekBar, int progress,
 63                     boolean fromUser) {
 64                 // TODO Auto-generated method stub
 65                 
 66             }
 67         });
 68         
 69         sv = (SurfaceView) findViewById(R.id.sv);
 70         
 71         //为什么要增加这个回调?主要是监控holder的变化的;因为如果你按一下小房子,声音仍在,而holder容易被销毁,再次打开时,看不到画面
 72         sv.getHolder().addCallback(new Callback(){
 73 
 74             @Override
 75             public void surfaceChanged(SurfaceHolder holder, int format,
 76                     int width, int height) {
 77                 // TODO Auto-generated method stub
 78                 System.out.println("holder被改变了!");
 79                 
 80             }
 81 
 82             @Override
 83             public void surfaceCreated(SurfaceHolder holder) {
 84                 // TODO Auto-generated method stub
 85                 System.out.println("holder被创建了!");
 86                 if(currentPosition > 0){
 87                     play(currentPosition);
 88                 }
 89                 
 90             }
 91 
 92             @Override
 93             public void surfaceDestroyed(SurfaceHolder holder) {
 94                 // TODO Auto-generated method stub
 95                 System.out.println("holder被销毁了!");
 96                 
 97                 if(mediaPlayer != null && mediaPlayer.isPlaying()){
 98                     currentPosition = mediaPlayer.getCurrentPosition();//获取当前所播放的位置
 99                     stop();//holder被销毁时,就stop媒体
100                 }
101                 
102             }
103             
104         });
105         
106         bt_play.setOnClickListener(this);
107         bt_pause.setOnClickListener(this);
108         bt_replay.setOnClickListener(this);
109         bt_stop.setOnClickListener(this);
110     }
111 
112 
113     @Override
114     public void onClick(View v) {
115         switch(v.getId()){
116         case R.id.bt_play:
117             play(0);
118             break;
119         case R.id.bt_pause:
120             pause();
121             break;
122         case R.id.bt_replay:
123             replay();
124             break;
125         case R.id.bt_stop:
126             stop();
127             break;
128         }
129         
130     }
131     
132     private void pause() {
133         if("继续".equals(bt_pause.getText().toString().trim())){
134             mediaPlayer.start();
135             bt_pause.setText("暂停");
136             return;
137         }
138         if(mediaPlayer != null && mediaPlayer.isPlaying()){
139             mediaPlayer.pause();
140             bt_pause.setText("继续");
141             return;
142         }
143         
144     }
145 
146 
147     private void replay() {
148         if(mediaPlayer != null && mediaPlayer.isPlaying()){
149             mediaPlayer.seekTo(0);
150             return;
151         }
152         play(0);
153         
154     }
155 
156 
157     private void stop() {
158         // TODO Auto-generated method stub
159         if(mediaPlayer != null && mediaPlayer.isPlaying()){
160             mediaPlayer.stop();
161             mediaPlayer.release();
162             mediaPlayer = null;
163             bt_play.setEnabled(true);
164             isplaying = false;
165         }
166     }
167 
168 
169     private void play(int currentPosition2){
170         String path = et_path.getText().toString().trim();
171         //File file = new File(path);
172         //if(file.exists() && file.length() > 0){
173             try {
174                 mediaPlayer = new MediaPlayer();
175                 mediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
176                 mediaPlayer.setDisplay(sv.getHolder());//设置video以SurfaceHolder播放
177                 mediaPlayer.setDataSource(path);
178                 //mediaPlayer.seekTo(currentPosition2);//不能把这定位放在此,否则播放不了
179                 mediaPlayer.prepare();
180                 mediaPlayer.seekTo(currentPosition2);
181                 mediaPlayer.start();
182                 //获取视频总长度
183                 int max = mediaPlayer.getDuration();
184                 sb.setMax(max);
185                 new Thread(){
186                     public void run(){
187                         isplaying = true;
188                         while(isplaying){
189                             int position = mediaPlayer.getCurrentPosition();
190                             sb.setProgress(position);
191                             try {
192                                 Thread.sleep(500);
193                             } catch (InterruptedException e) {
194                                 // TODO Auto-generated catch block
195                                 e.printStackTrace();
196                             }
197                         }
198                     }
199                 }.start();
200                 mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
201                     
202                     @Override
203                     public void onCompletion(MediaPlayer mp) {
204                         // TODO Auto-generated method stub
205                         bt_play.setEnabled(true);
206                         isplaying = false;
207                         
208                     }
209                 });
210                 bt_play.setEnabled(false);
211             } catch (Exception e) {
212                 // TODO Auto-generated catch block
213                 Toast.makeText(this, "播放失败!", 0).show();
214                 e.printStackTrace();
215             }
216         //}else{
217             //Toast.makeText(this, "音频文件不存在!", 0).show();
218         //}
219     }
220     
221 }

注意:

1)如果要播放互联网文件,则要加入网络权限: <uses-permission android:name="android.permission.INTERNET"/>

2)要注意和音频文件的播放代码进行比较,音频代码为:http://www.cnblogs.com/zhongyinghe/p/5354040.html

以上是关于视频播放-SurfaceView的主要内容,如果未能解决你的问题,请参考以下文章

Android SurfaceView播放视频

使用MediaPlayer和SurfaceView播放视频

SurfaceView+MediaPlayer播放视频

视频播放-SurfaceView

安卓视频播放(阿里云视频点播播放器SDK+SurfaceView)

Android SurfaceView + MediaPlayer实现分段视频无缝播放