0
此代碼同步surfaceview和媒體播放器在線播放安卓:MediaPlayer的IsPlaying模塊:0
我的代碼:
static SurfaceView videoSurface;
static SurfaceHolder videoHolder;
static MediaPlayer mediaPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
videoSurface = (SurfaceView) findViewById(R.id.surfView);
videoHolder = videoSurface.getHolder();
videoHolder.addCallback((Callback) this);
mediaPlayer = new MediaPlayer();
new Thread(new Runnable() {
@Override
public void run() {
String link = "link.m3u8";
imgPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
play(link);
}
});
}
}).start();
}
public static void play(final String link) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mediaPlayer.setDisplay(videoHolder);
mediaPlayer.setDataSource(link);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
此代碼的工作,但後幾秒鐘
如果加載完成後,我正在點擊在,錯誤正在播放0
但是,如果我等幾秒鐘,然後點擊後玩與你合作問題!
你開始的線程就好像他們是煙花 – lelloman
好吧,我用它在用戶界面,但不工作 – Mohammad