Reputation: 190
I am trying to Play Videos with an MediaPlayer on a VideoView. 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.
(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 e.g. samsung tablets)
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(); }
}
Upvotes: 0
Views: 3929
Reputation: 29
Create a custom VideoPlayer by extending VideoView class and use it:
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);
}
}
}
Upvotes: 0
Reputation: 346
have u try to use:
objVideoView.setZOrderMediaOverlay(true);
objVideoView.videoView.setZOrderOnTop(true);
in onCreate(...) method?
Upvotes: 12