Play video from url in VideoView [Android]
Try this code.. This code works perfectly for me..
VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
videoView.start();
How to play video from server url in video view android
Try this, it will work for you,
try {
String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
How to play a video from video url when onclick imagebutton in android?
Try using these two onCreate methods for the two Activities.
Video Vewing Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.ltVideoUrl);
String path1 = "https://d1e6yi6s3cx2ur.cloudfront.net/videos/0/_20160316_ios-user.m4v";
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri uri = Uri.parse(path1);
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});}
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnWatchVideo = (ImageView) findViewById(R.id.btnWatchVideo);
btnWatchVideo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, VideoActivity.class));
}
});}
Play back video stream via HTTP with SwiftUI
So, I figured out that the stream is a MJPEG stream and the AVPlayer doesen't support that. Therefore it only shows the first frame.
So I've implemented a camera service that fetches the MJPEG stream, like this:
protocol CameraServiceDelegateProtocol {
func frame(image: UIImage) -> Void
}
protocol CameraServiceProtocol {
var rosServiceDelegate: CameraServiceDelegateProtocol { get set }
}
class CameraService: NSObject, ObservableObject {
var cameraServiceDelegate: CameraServiceDelegateProtocol
let realUrl = URL(string: "http://192.168.45.100:8080/stream?topic=/image_raw")
var dataTask: URLSessionDataTask?
var receivedData: NSMutableData = NSMutableData()
var session: URLSession?
init(delegate: CameraServiceDelegateProtocol) {
cameraServiceDelegate = delegate
}
func play() {
session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
dataTask = session?.dataTask(with: realUrl!)
dataTask?.resume()
}
func stop() {
dataTask?.cancel()
}
}
extension CameraService: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
if self.receivedData.length > 0,
let receivedImage = UIImage(data: self.receivedData as Data) {
DispatchQueue.main.async {
self.cameraServiceDelegate.frame(image: receivedImage)
}
self.receivedData = NSMutableData()
}
completionHandler(URLSession.ResponseDisposition.allow) //.Cancel,If you want to stop the download
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.receivedData.append(data)
}
}
Related Topics
Android - Play Sound on Button Click - Null Pointer Exception
How to Read and Edit Android Calendar Events Using the New Android 4.0 Ice Cream Sandwich API
Android Logcat Logs Chatty Module Line Expire Message
Build Errors After Android Studio 3.2.1 Upgrade
How to Add Action Bar Options Menu in Android Fragments
Androidstudio - Module Dependencies in Gradle
Logcat Not Displaying My Log Calls
Dynamically Add Textviews to a Linearlayout
Change Checkbox Value Without Triggering Oncheckchanged
Assign Width to Half Available Screen Width Declaratively
Error:Unexpected Lock Protocol Found in Lock File. Expected 3, Found 0
How to Pause Flash Content in an Android Webview When My Activity Isn't Visible