我正在尝试使用VideoView上的MediaPlayer播放视频。不幸的是我只播放了音频。没有显示视频。我需要使用Mediaplayer,因为我的视频位于受保护而不是Worldreadable的地方(并且在播放之前要复制很大)并且需要进行流式传输。
(视频很好,可以播放。在像Acer A210这样的安卓平板电脑上,对于非可翻译文件不敏感,我可以使用VideoViews setVideoURI方法直接播放视频,我需要以下代码来播放视频例如三星片)
有人能告诉我我做错了什么吗? Thx提前。
public class VideoPlayer extends Activity implements SurfaceHolder.Callback {
private VideoView objVideoView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_videoplayer);
objVideoView = (VideoView) findViewById(R.id.myVideoView);
String strVideoNames = "";
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras != null) strVideoNames = extras.getString("strVideoNames");
} else {
strVideoNames = (String) savedInstanceState.getSerializable("strVideoNames");
}
playVideo(getVideoUrl(strVideoNames));
}
ArrayList<String> listVideoNames;
public void playVideo(String strVideoNames) {
if (strVideoNames.contains(";")) {
String[] strAVideoUrls = strVideoNames.split(";");
listVideoNames = new ArrayList<String>(Arrays.asList(strAVideoUrls));
} else {
listVideoNames = new ArrayList<String>();
listVideoNames.add(strVideoNames);
}
playVideoWithMediaPlayer();
}
MediaPlayer objMediaPlayer;
public void playVideoWithMediaPlayer() {
if (listVideoNames.size() > 0) {
try {
SurfaceHolder objSurfaceHolder = objVideoView.getHolder();
objSurfaceHolder.addCallback(this);
objSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
objMediaPlayer = new MediaPlayer();
} catch (Exception e) {
alert(e.getMessage());
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
try {
File fileVideo = new File(getVideoUrl(listVideoNames.get(0)));
FileInputStream instreamVideo = new FileInputStream(fileVideo);
objMediaPlayer.setDataSource(instreamVideo.getFD());
objMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer _objMediaPlayer) {
listVideoNames.remove(0);
if (listVideoNames.size() > 0) {
playVideoWithMediaPlayer();
} else {
_objMediaPlayer.release();
}
}
});
objMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer _objMediaPlayer) {
Log.d("MediaPlayer","Prepared ...");
objMediaPlayer.start();
}
});
objMediaPlayer.prepare();
} catch (Exception e) {
alert(e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceDestroyed(SurfaceHolder holder) { objMediaPlayer.stop(); }
}
答案 0 :(得分:13)
objVideoView.setZOrderMediaOverlay(true);
objVideoView.videoView.setZOrderOnTop(true);
onCreate(...)方法中的?
答案 1 :(得分:0)
通过扩展VideoView类创建自定义VideoPlayer并使用它:
public class VideoPlayer extends VideoView {
public VideoPlayer(Context context) {
super(context);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
TyrooLog.i(TAG, "onMeasure");
int width = getDefaultSize(videoWidth, widthMeasureSpec);
int height = getDefaultSize(videoHeight, heightMeasureSpec);
if (videoWidth > 0 && videoHeight > 0) {
if (videoWidth * height > width * videoHeight) {
TyrooLog.i(TAG, "video too tall, correcting");
height = width * videoHeight / videoWidth;
} else if (videoWidth * height < width * videoHeight) {
TyrooLog.i(TAG, "video too wide, correcting");
width = height * videoWidth / videoHeight;
} else {
TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
}
}
TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}
}