标签:
这里只基本介绍如何播放视频:surfaceview是利用双缓冲区的原理来使得用户可以流畅的观看视频的:
比如A、B缓冲区,先由A缓冲区填满推送到设备播放,然后再A播放的时候填满B缓冲区,等A播放完了就播放B缓冲区的内容,如此交换执行。
public class MainActivity extends ActionBarActivity { private SurfaceHolder holder; private SurfaceView sf; private MediaPlayer mediaplayer; private String path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sf=(SurfaceView) findViewById(R.id.sf); holder= sf.getHolder(); // 这里是设置surfaceview不维护自己的缓冲区而是等待屏幕的渲染引擎将视频推送到用户面前 } public void start(View v) { mediaplayer =new MediaPlayer(); mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // path="data/data/musics/oppo.3gp"; path="http://192.168.61.173:8080/mymusics/oppo.3gp"; try { mediaplayer.setDataSource(path); mediaplayer.setDisplay(holder); //设置视频在哪里播放,holder即surfaceview空间里。
// mediaplayer.prepareAsync(); // mediaplayer.setOnPreparedListener(new MypreparedListener()); mediaplayer.prepare(); mediaplayer.start(); } catch (Exception e) { e.printStackTrace(); } } class MypreparedListener implements OnPreparedListener { @Override public void onPrepared(MediaPlayer mp) { mediaplayer.start(); } } }
标签:
原文地址:http://www.cnblogs.com/bokeofzp/p/4740516.html