1.重量级控件surfaceView
拿到surfaceView这个控件并不能直接进行操作需要将进程暂停一点时间
等其初始化完毕后在进行其他操作(可以直接在子线程中更新ui
内部维护了两个线程
一个负责加载,一个负责显示,两个线程来回切换
package text.vido; import android.media.MediaPlayer; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; public class MainActivity extends AppCompatActivity { private SurfaceView sfv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sfv=(SurfaceView)findViewById(R.id.sfv); final MediaPlayer mediaPlayer=new MediaPlayer(); final SurfaceHolder holder=sfv.getHolder(); new Thread(){ public void run(){ try { SystemClock.sleep(400); mediaPlayer.setDataSource("http://10.39.1.16/1.mp4"); mediaPlayer.prepareAsync(); //SurfaceHolder维护视频播放的内容 mediaPlayer.setDisplay(holder); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.start(); } }); } catch (IOException e) { e.printStackTrace(); } }; }.start(); } }
也可以采用surfaceHolder自带的生命周期方法 addCallback