I want to play video content from HttpResponse object. The content is downloading from a streaming sever.
My requirement is to create a HTTP POST request to the server. Request contains the URL of video, username and password of the user for authentication purpose.
I wish to know how can we create a HTTP POST request and play/download the video.
Kindly provide some hints, steps/code to proceed.
Thanks,
I am not 100% sure, but I think that you can't stream most of the video due to the way the format stores the meta data of the video. That's why you tube has to convert your video files instead of just showing them in any format.
There are protocols that encapsulate this metadata and allows you to stream any video (that's what youtube mobile does). You should take a look at RTSP: http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol
If you use an rtsp link in a videoView it should stream the video flawlessly. The point is that your problem is server and not client related.
As an exercise you could grab an rtsp link from m.youtube.com and just pass to a videoView using setVideoPath and it should work.
If can't change the server implementation then you probably them I guess your solutions are:
1) Download and decode the video yourself, you would have to handle all the metadata and ensure that the video does work though. You could, theoretically, compile ffmpeg for android to help you with that, but I couldn't compile it to android with the network option on. That is a lot of work.
2) Write your own implementation of RTSP or other streaming protocol. Download the video in a thread and them create a local server in the device to stream that video file to a videoView instance. I actually have this working on an app. Android doesn't support the specific protocol that the client's servers use and I had to get it working. It took me a full month to do this. I can't post any example code because it's all the client's, but I could give you some further info on this if you are interested.
Either way, if you can't change the server/format of the video then you should prepare for a whole lot of work.
check the below code for showing a url http or rtsp in Video View.
Now before invoking your videoview .. just get
public class VideoViewDemo extends Activity {
/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "";
private VideoView mVideoView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(
VideoViewDemo.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
}
Now since your requirements are different first make http post request to your server ... get the desired input stream.. write that stream as mp4 to your local file system. then you can use below code to play content
// this can be path on file system
String path="/adf.mp4" ;
public void playVideo() {
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(videoView);
videoView.setVideoPath(path);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
mediaController.show();
I tried this and it worked for me :D no rtsp no nothing, just a url with a mp4 video and it played!
public class VideoActivity extends Activity {
public static final String KEY = "video_id";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fotogaleria_video);
Intent i = getIntent();
if(i.hasExtra(KEY)){
String url = "https://sites.google.com/site/tempmp4download/bnm.mp4";
VideoView vv = (VideoView)findViewById(R.id.fotogaleria_video);
// vv.setVideoPath(url);
vv.setVideoURI(Uri.parse(url));
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
}
Look into following links
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
http://w3mentor.com/learn/java/android-development/android-http-services/performa-a-http-post-request-with-the-httpclient-in-android/
For streaming, I think you have to download the full file, and then show it in the video player.
Related
This question already has an answer here:
Streaming videos from Google Cloud
(1 answer)
Closed 1 year ago.
Been Google searching to understand this concept but not getting a definite answer,I want to know if using getdownloadurl() in firebase cloud storage for videos will let the videos get streamed bit by bit instead of downloading all the video files at once,i also want to know if firebase is not the best option for such.
What I'm trying to do is to create an android app where videos uploaded by other users can be streamed but I don't want the videos to be downloaded once after request I need the video to get downloaded bit by bit and change quality based on network connection as the video is being watched as opposed to downloading all the videos at once which would be really slow.
Can you use the native VideoView object in Android? This example has a static URL like the one you would get from Firebase: https://www.geeksforgeeks.org/how-to-play-video-from-url-in-android/
Obviously, you would need to go out and fetch the "download URL" first and then use it as your source.
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Your Video URL
String videoUrl = "https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// finding videoview by its id
VideoView videoView = findViewById(R.id.videoView);
// Uri object to refer the
// resource from the videoUrl
Uri uri = Uri.parse(videoUrl);
// sets the resource from the
// videoUrl to the videoView
videoView.setVideoURI(uri);
// creating object of
// media controller class
MediaController mediaController = new MediaController(this);
// sets the anchor view
// anchor view for the videoView
mediaController.setAnchorView(videoView);
// sets the media player to the videoView
mediaController.setMediaPlayer(videoView);
// sets the media controller to the videoView
videoView.setMediaController(mediaController);
// starts the video
videoView.start();
}
}
I want to play video inside my webview. I have examined different example but my HTML a bit different.
{\"mp4\":[{\"name\":\"240\",\"file\":\"http:\\\/\\\/xxxx.xxxx.net\\\/lt\\\/2016\\\/04\\\/24\\\/test_320x240.mp4\",\"type\":\"video\\\/mp4\"}
I can download HTML code and used function;
webview.loadData(myHTML,"text/html; charset=UTF-8", null);)
However, I can't display videos in my application. Thank you for helping.
You can try playing the video using VideoView rather than trying to load it in a webview.Below is the sample code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoView);
File file = new File(Environment.getExternalStorageDirectory(), "rhyme.mp4");
videoView.setVideoPath(file.getAbsolutePath());
videoView.setMediaController(new MediaController(this));
videoView.start();
}
Also if you want to stream some video files from internet, you can go ahead with ExoPlayer. Here is the link
Kindly mark this reply as answer if it solves your problem.
I am trying to connect my android application with FTP Server which is broadcasting multiple streams using Dynamic Http Streaming at different bit rates.Flash Messaging Server(FMS) is using H.264 codec to broadcast video in flv format.On client side(Android) I am getting f4m(manifest (xml) file)) having diffrent streams.
I am unable to use the f4m file to fetch video in android from FMS.
String url = "http://d2233avv69kunu.cloudfront.net/hds-live/livepkgr/_definst_/liveevent/livestream.f4m";
Uri uri = Uri.parse(url);
// videoView.setVideoURI(uri);
videoView.setVideoPath(str);
MediaController mc = new MediaController(this);
//VMRuntime.getRuntime().setMinimumHeapSize(40);
mc.setAnchorView(videoView);
videoView.setMediaController(mc);
System.out.println("Max Memory - "+java.lang.Runtime.getRuntime().maxMemory());
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
videoView.start();
}
});
In above code I am trying to run video in Video View component by passing link of f4m file.
Flash encoder convert video into 3 file formats as listed below
.f4m (manifest files) - Bootstrap Info,Metadata, Bitrate, Flash Access license Server location
.f4f (fragment files) - extends the F4V format,MP4 fragment std
.f4x (index files) - binary
Kindly provide solution to play video on different android devices via Http Streaming in android.
Thanks in advance.
1) You have left something out of your code. What is str that you are passing to setVideoPath? (this is somewhat irrelevant, though...)
2) The MediaPlayer (and thereby VideoView) cannot play Flash video at all. It's simply not supported. It definitely won't understand an XML file, either, so passing it the f4m file is pointless.
I'm trying to stream a video from VLC to an HTC G1. After several "this should work" I found a sout-chain that allowed me to watch the stream via VLC. I am also able to hear the audio on the android.
The sout-chain I'm currently using:
vlc some_file.mp4 -I http --sout "#transcode{soverlay,ab=128,samplerate=44100,channels=2,acodec=mp4a,vcodec=h264,width=480,height=270,vfilter="canvas{width=480,height=270,aspect=16:9}",fps=25,vb=800,venc=x264{level=12,no-cabac,subme=20,threads=4,bframes=0,min-keyint=1,keyint=50}}:gather:rtp{mp4a-latm,sdp=rtsp://0.0.0.0:5554/stream.sdp}"
That's what I'm doing on the droid:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView) findViewById(R.id.video_view);
vv.setVideoURI(Uri.parse("rtsp://<local_ip>:5554/stream.sdp"));
vv.start();
}
I tried to keep it as minimal as possible (this is actually an example I found in another thread here).
I also tried using MediaPlayer:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse("rtsp://<local_ip>:5554/stream.sdp"));
mp.setDisplay(vv.getHolder());
mp.start();
I use setDisplay(SurfaceHolder) cause someone mentioned MediaPlayer otherwise wont know what display to use.
Any idea what I'm missing or doing wrong?
Edit: I hinted the the file with MP4Box
First, I think there's problem with your audio encoder, it shows "MPEG-1/2 Video" is not an audio encoder, would you please try "mpga"?
and another problem is you are trying to fit the frames into a specified size, width=480,height=270, could you delete this part?
my command works:
vlc /Users/chenyu/Sites/BBC.mp4 -I http --sout "#transcode{soverlay,ab=128,samplerate=44100,channels=2,acodec=mpga,vcodec=h264,fps=25,vb=800,venc=x264{level=1,no-cabac,subme=20,threads=4,bframes=0,min-keyint=1,keyint=50}}:gather:rtp{mp4a-latm,sdp=rtsp://10.0.1.2:5554/stream.sdp}"
also could you try the following code on android side?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
vidView.setVideoPath("rtsp://10.0.1.2:5554/stream.sdp");
vidView.start();
}
Today for one of my app (Android 2.1), I wanted to stream a video from an URL.
As far as I explored Android SDK it's quite good and I loved almost every piece of it.
But now that it comes to video stream I am kind of lost.
For any information you need about Android SDK you have thousands of blogs telling you how to do it. When it comes to video streaming, it's different. Informations is that abundant.
Everyone did it it's way tricking here and there.
Is there any well-know procedure that allows one to stream a video?
Did google think of making it easier for its developers?
If you want to just have the OS play a video using the default player you would use an intent like this:
String videoUrl = "insert url to video here";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(videoUrl));
startActivity(i);
However if you want to create a view yourself and stream video to it, one approach is to create a videoview in your layout and use the mediaplayer to stream video to it. Here's the videoview in xml:
<VideoView android:id="#+id/your_video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
Then in onCreate in your activity you find the view and start the media player.
VideoView videoView = (VideoView)findViewById(R.id.your_video_view);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
String str = "the url to your video";
Uri uri = Uri.parse(str);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
Check out the videoview listeners for being notified when the video is done playing or an error occurs (VideoView.setOnCompletionListener, VideoView.setOnErrorListener, etc).