I have a problem playing my video from my phone. Output is a black screen, nothing happens. What should I do?
public class Videoview extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.plant_info);
VideoView videoView =(VideoView) findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/raw/kitkat.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
Related
This is my activity and it's working.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent intent = getIntent();
String link = intent.getStringExtra("link");
VideoView videoview = (VideoView) findViewById(R.id.videoView2);
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoview);
Log.i("Link",link);
Uri video = Uri.parse(link);
videoview.setMediaController(mediacontroller);
videoview.requestFocus();
videoview.setVideoURI(video);
videoview.start();
}
This is the screen when playing:
But when the controller hides, the video screen is not fullscreen.
How can Videoview display fullscreen when mediacontroller hides?
I think you can create your myMediaController below to triger onHide of mediaController and refresh videoview
class myMediaController extends MediaController{
private View anchorView = null; //this ref to videoview from setAnchorView(View view)
public myMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
anchorView = view;
}
#Override
public void hide() {
super.hide();
anchorView.invalidate(); //refresh with and height of video view on screen
anchorView.refreshDrawableState();
}
}
I am using the below code to test on emulator and device, the result came out was, the emulator can show the MediaController just below the VideoView(default is at the bottom of the screen). However, when I tested in device, it does not show me the MediaController.
I would like to know why.
final VideoView videoView = (VideoView)findViewById(R.id.videoGuide1);
videoView.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener(){
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height){
MediaController mediaController = new MediaController(WatchVideo.this);;
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
}
});
}
});
Try this It worked for me....
VideoView vv;
vv = (VideoView) findViewById(R.id.videoView);
vv.setVideoPath(filename);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
Try to this way
MediaController controller;
Videoview videoview;
videoview=(VideoView)findViewById(R.id.videoview);
controller = new MediaController(this);
videoview.setVideoURI(Uri.parse(path));
videoview.setMediaController(controller);
controller.setMediaPlayer(videoview);
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
int duration = videoview.getDuration();
videoview.requestFocus();
videoview.start();
controller.show();
Log.d("Video view is prepared","------>");
}
});
Here's my activity code:
public class VideoActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView) findViewById(R.id.activity_video_videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri
.parse("http://www.ebookfrenzy.com/android_book/movie.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
While video is buffering and not played I want to display progress bar. How can I do it?
Set an OnPreparedListener and dismiss the dialog there:
Declare the progress dialog in your activity:
private static ProgressDialog progressDialog;
Then in onCreate method:
...
progressDialog = ProgressDialog.show(this, "", "Loading...", true);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
progressDialog.dismiss();
videoView.start();
}
});
Check the new Lines Added should do what you intend.
public class VideoActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView) findViewById(R.id.activity_video_videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri
.parse("http://www.ebookfrenzy.com/android_book/movie.mp4");
videoView.setMediaController(mediaController);
// New Line
ProgressDialog pDialog = ProgressDialog.show(this, "", "Loading Video");
videoView.setVideoURI(video);
//New Line Added
pDialog.dismiss
videoView.start();
}
}
public class video extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.surface);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.wildlife);
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
}
here is my code to play the video. But the video stays in my screen and it does not play. Is there any solution ?
video_player_view.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/VideoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java code:
public class VideoPlayerActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_view);
String video_file_path = ?;//give the path of your video
VideoView video_view = (VideoView) this.findViewById(R.id.VideoPlayerView);
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(video_file_path);
video_view.requestFocus();
// start video
video_view.start();
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
endActivity();
}
});
}
public void endActivity() {
this.finish();
}
}
Why are you using this code?
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
If it is not necessary then remove it video will be played with sound. Otherwise please write what you exactly want to do with video playback.
Can you give an example, how to display video from the url?
The easier way:
public class A extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
VideoView videoView = new VideoView(this);
setContentView(videoView);
videoView.setVideoPath("http://example.com/video.mp4");
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}
}