我有一个活动TextureView
,Surface
和MediaPlayer
正在播放视频,但没有控制器出现。我用谷歌搜索了一段时间但是找不到解决方案。
如何制作播放/暂停和寻求者?
代码:
public class VideoActivity extends Activity implements SurfaceTextureListener, OnClickListener {
private MediaPlayer mMediaPlayer;
private TextureView textureView;
private Uri videoUri;
private Button playPause;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.video_layout_new);
playPause = (Button) findViewById(R.id.playPauseBtn);
playPause.setOnClickListener(this);
int videoIndex = getIntent().getExtras().getInt("video");
videoUri = null;
switch (videoIndex) {
case 1:
videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video1);
break;
case 2:
videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video2);
break;
case 3:
videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3);
break;
case 4:
videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video4);
break;
}
textureView = (TextureView) findViewById(R.id.textureView1);
textureView.setSurfaceTextureListener(this);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int widthGo = (int) (width * 0.95);
int heightGo = (int) (widthGo * 0.8);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(widthGo, heightGo);
lp.gravity = Gravity.CENTER;
textureView.setLayoutParams(lp);
}
@Override
protected void onPause() {
super.onPause();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(VideoActivity.this, videoUri);
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
public void onClick(View v) {
if (v.equals(playPause)) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
playPause.setBackgroundResource(R.drawable.play_btn);
} else {
mMediaPlayer.start();
playPause.setBackgroundResource(R.drawable.pause_btn);
}
}
}
}