display video from url - android

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();
}
}

Related

Android videoview black screen only no video output

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();

How to display progress bar while MediaController loads video (live streaming) and not playing it?

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();
}
}

VideoView - MediaController not showing up

I can't get it working... Whenever I touch my video, nothing is happing... Although, I think, the MediaController should be visible...
I currently use following function to play my video:
private void playCachedVideo(final VideoView videoView, final Context c, File f)
{
//videoView.setZOrderOnTop(true);
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(c);
mediaController.setMediaPlayer(videoView);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
}
});
}
});
videoView.setVideoURI(Uri.fromFile(f));
videoView.start();
}
The video plays fine, but I can't get any MediaController to show up...
PS: I'm showing the player in a DialogFragment
Try this:
mMediaController = new MediaController(getActivity());
replace c by getActivity().
You need to attach the controller to the VideoView and then call mediaController.show(timeInMillisec) in onPrepared() callback.
This show the controller on touch of videoview and will hide the controller after specified timeInMillisec.
public class VideoViewActivity extends Activity implements MediaPlayer.OnPreparedListener{
private VideoView mVideoView = null;
MediaController mediaController = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_layout);
mVideoView = (VideoView) findViewById(R.id.VideoView);
mVideoView.setOnPreparedListener(this);
mediaController = new MediaController(mVideoView.getContext());
mediaController.setMediaPlayer(mVideoView);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath("URL");
}
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mediaController.show(2000);
}
}

Android: VideoView can't play video

im trying to get a basic video player working for an android application im working on but whenever i try to play the video i get an error saying "cant play video". Im certain the video is in supported formats and ive tried converting it between several of the supported formats on top of that so i assume its an issue with my code:
public class VideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
VideoView videoView = (VideoView)findViewById(R.id.VideoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("TravelAppTopic4/res/raw/canadavid.mp4");
videoView.start();
}
thanks in advance for any help
edit:im testing on both a virtual device and an S3 to the same result.
public class VideoScreen extends Activity{
private MediaController controller;
Context _ctx = VideoScreen.this;
private Thread thread;
ProgressBar progressBar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.phone_video_screen);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
VideoView video = (VideoView) findViewById(R.id.videoView1);
controller = new MediaController(VideoScreen.this);
video.setMediaController(controller);
video.setVideoPath(_url);
video.requestFocus();
video.start();
progressBar.setVisibility(View.VISIBLE);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
progressBar.setVisibility(View.GONE);
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}
try
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw. canadavid));

video doesnot play but audio listening

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.

Categories

Resources