[英]Android MediaPlayer (using VideoView) no Video, only Audio plays
I am trying to Play Videos with an MediaPlayer on a VideoView. 我正在尝试在VideoView上使用MediaPlayer播放视频。 Unfortunately I get audio only played.
不幸的是我只能播放音频。 There is no Video displayed.
没有显示视频。 I need to use the Mediaplayer because my Videos are in a protected not Worldreadable place (and are to big to be copied before playing) and need to be streamed.
我需要使用Mediaplayer,因为我的视频位于受保护的,不可读的地方(并且在播放之前需要复制很多),并且需要进行流处理。
(The Videos are alright and can be Played. On android tablets like the Acer A210 which are not that sensible to nonworldreadable files I can play the Videos directly by using the VideoViews setVideoURI method, I need following code to play the videos on eg samsung tablets) (视频没问题,可以播放。在Acer A210这样的Android平板电脑上,这些文件对非世界范围的文件不敏感,我可以使用VideoViews setVideoURI方法直接播放视频,我需要以下代码才能在例如三星平板电脑上播放视频)
Can someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗? Thx in Advance.
事先感谢。
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(); }
}
have u try to use: 你有没有尝试使用:
objVideoView.setZOrderMediaOverlay(true);
objVideoView.videoView.setZOrderOnTop(true);
in onCreate(...) method? 在onCreate(...)方法中?
Create a custom VideoPlayer by extending VideoView class and use it: 通过扩展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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.