I am trying to build an app to play video from an URL, I am using rtsp, I have tried with most of the examples that I have found online, however none has worked.. This is my code so far in the client:
package com.example.rtsp_player;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String uri = "rtsp://192.168.2.170:5544/";
VideoView v = (VideoView) findViewById( R.id.videoView );
v.setVideoURI( Uri.parse(uri) );
v.setMediaController( new MediaController( this ) );
v.requestFocus();
v.start();
}
}
And this is how I am broadcasting using vlc:
:sout=#transcode{vcodec=h264,vb=800,fps=30,width=320,height=180,acodec=mp4a,ab=128,channels=2,samplerate=22050} :rtp{sdp=rtsp://192.168.2.170:5544/}**
I don't know if it is because of the codecs or what..
myVideoView = (VideoView) findViewById(R.id.myview);
myVideoView.setVideoPath("rtsp://SERVER_IP_ADDR:5544/");
myVideoView.setMediaController(new MediaController(this));
this may help
Add this line to your code.
Uri uri = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI,"1");
Change the code like this and try.
String uri = "rtsp://192.168.2.170:5544/";
VideoView v = (VideoView) findViewById( R.id.videoView );
Uri uri_add = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI,"1");
v.setVideoURI( uri_add.parse(uri) );
v.setMediaController( new MediaController( this ) );
v.requestFocus();
v.start();
Related
I want to add a youtube video to my page. I downloaded the video and added it into the res-> raw folder. The video is marked minecraft_trailer.mp4. I am getting an error in the javascript page and I cant figure out what it is. Please help.
package com.treacheryofimages.www;
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class OtherActivity7 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other7);
}
VideoView vv = (VideoView)this.findViewById(R.id.videoView1);
String fileName = "android.resource://" + getPackageName() + "/" +
R.raw.minecraft_trailer;
vv.setVideoURI(Uri.parse(minecraft_trailer));
vv.start();
}
}
The error is coming from the vv.setVideoURI(Uri.parse(minecraft_trailer)); line
I cant tell what it is. Its possibly whats inside the ().
Also, what is supposed to go inside those ()
You are passing the wrong variable minecraft_trailer when calling vv.setVideoURI();
it should be vv.setVideoURI(Uri.parse(fileName ));
check this out :
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
I want to play a video from res/raw in android here is my code:
public class CreditsActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.creditslayout);
VideoView v = (VideoView) findViewById(R.id.video);
v.setVideoURI(Uri.parse("android.resource://test.test.test/raw/maincredits"));
v.start();
}
}
But I hear only the sound of video. And the emulator doesn't show the video itself the size of file is 2.8 MB.
Try below code :
public class CreditsActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.creditslayout);
VideoView v = (VideoView) findViewById(R.id.video);
v.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video_file));
v.start();
}
}
Please try this.
Uri uri = Uri.parse("android.resource://test.test.test/"+R.raw.[video_resid]);
v.setVideoURI(uri);
try Uri.parse("android.resource://test.test.test/" + R.raw.yourVideoName)
Try this
// trailer_final is the video name
String fileName = "android.resource://"+ getPackageName()+"/raw/trailer_final";
VideoView mvideo = (VideoView) findViewById(R.id.playVideo);
mvideo.setVideoPath(fileName);
MediaController controller = new MediaController(this);
mvideo.setMediaController(controller);
mvideo.requestFocus();
mvideo.start();
Try this:
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
Just pass the name of the video residing in the raw folder under res of the project.
The emulator doesn't show the video. I had this problem on a project. We tried it on an actual device and it played perfectly. Try to play it on actual device. It will likely work.
I am making simple videoview app from this link
http://www.androidpeople.com/android-videoview-example
Now what should be the Uri here, i mean should i write path like:
"C:\Folder 1\file.mp4"
I am usin mp4 file from :
http://www.mp4mobi.com/mp41315287045/Swept_Away_Bride.htm
see this example code
package com.video.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoPlayActivity extends Activity {
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.video);
VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
final MediaController mc = new MediaController(this);
videoView.setMediaController(new MediaController(this){
/*public void hide()
{
mc.show();
}*/
});
//videoView.setVideoURI(Uri.parse("http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4"));
videoView.setVideoPath("/sdcard/rabbit-and-snail.3gp");
videoView.requestFocus();
videoView.start();
}
}
in this ,uri locates the path of the ur video file.
Add the video file to your sdcard.
Use:
Uri.parse("file:/sdcard/videofilename")
Should work.
You can't Add File From your PC you have to put that .MP4 file to Raw folder or in your asset Folder....
1st you Create Folder in Application's resource with Name = raw.
After That put file in raw folder...
And than use this Code...
VideoView videoHolder = (VideoView) findViewById(R.id.web_Video);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.data);
I want to play a movie from my sd-card. Ive tried using the following code:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String("/sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
But when Im trying to play the file i get an error message. "video not found" or something similar. When i tried streaming from the web, the video worked but was very laggy. Whats the best way to play videos in my app?
Thanks
Try this...
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory()+"/sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
It is observed that setVideoPath() fails, while setVideoURI() works well for both Web and Local so I insist you to use this.
VideoView videoView = (VideoView) findViewById(R.id.videoView);
final String MEDIA_PATH = new String("file:///sdcard/robot.avi");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoURI(MEDIA_PATH);
videoView.setMediaController(mediaController);
videoView.start();
Use this code.Hope it will work
public class VideoPlayActivity extends Activity {
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
File clip=new File(Environment.getExternalStorageDirectory(),
"haha.mp4");
if (clip.exists()) {
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
}
}
}
Try with
video_view.setVideoURI(Uri.parse(path));
you can not pass directly as a string path if you are trying to set as a uri. The code which is working fine for me :
path = Environment.getExternalStorageDirectory() + "/file_name";
// Add controls to a MediaPlayer like play, pause.
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
// Set the path of Video or URI.
video_view.setVideoURI(Uri.parse(path));
// Set the focus.
video_view.requestFocus();
video_view.start();
your problem is that the video path is not set the right way:
just switch to this code:
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/robot.avi";
that will solve your problem if the video "robot.avi" exists on the root folder of the sd card
You are playing your video in your own VideoView,
But if you have nothing to customize and just want to show the video in the screen,why dont you use the default player to play the video.
File imgFile = new File(Environment.getExternalStorageDirectory()+"FileName");
//make sure the video is in SDCard,
//if its located in any folder care to pass full absolute path
Intent tostart = new Intent(Intent.ACTION_VIEW);
tostart.setDataAndType(Uri.parse(imgFile.getPath()), "video/*");
startActivity(tostart);
May be avi does not support in android.convert it into mp4 or wmv or 3gp.
try this code
public class VideoPlayActivity extends Activity {
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
File clip=new File(Environment.getExternalStorageDirectory(),
"robot.mp4");
if (clip.exists()) {
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
}
}
}
I am beginner in android development and try to play video from link. But it's giving error "sorry,we can't play this video". I tried so many links but for all links its show same error.
My code is the following
public class VideoDemo extends Activity {
private static final String path ="http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.videoview);
video = (VideoView) findViewById(R.id.video);
video.setVideoPath(path);
ctlr = new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
}
}
Logcat shows following error message:
04-12 15:04:54.245: ERROR/PlayerDriver(554): HandleErrorEvent: PVMFErrTimeout
It has something to do with your link and content. Try the following two links:
String path="http://www.ted.com/talks/download/video/8584/talk/761";
String path1="http://commonsware.com/misc/test2.3gp";
Uri uri=Uri.parse(path1);
VideoView video=(VideoView)findViewById(R.id.VideoView01);
video.setVideoURI(uri);
video.start();
Start with "path1", it is a small light weight video stream and then try the "path", it is a higher resolution than "path1", a perfect high resolution for the mobile phone.
Try this:
String LINK = "type_here_the_link";
setContentView(R.layout.mediaplayer);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
pDialog = new ProgressDialog(this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(mVideoView);
Uri videoUri = Uri.parse(videoUrl);
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(videoUri);
} catch (Exception e) {
e.printStackTrace();
}
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
finish();
}
});
You can do it using FullscreenVideoView class. Its a small library project. It's video progress dialog is build in. it's gradle is :
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.0'
your VideoView xml is like this
<com.github.rtoshiro.view.video.FullscreenVideoLayout
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your activity , initialize it using this way:
FullscreenVideoLayout videoLayout;
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
Uri videoUri = Uri.parse("YOUR_VIDEO_URL");
try {
videoLayout.setVideoURI(videoUri);
} catch (IOException e) {
e.printStackTrace();
}
That's it. Happy coding :)
If want to know more then visit here
Edit:
gradle path has been updated. compile it now
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'
Try Exoplayer2
https://github.com/google/ExoPlayer
It is highly customisable
private void initializePlayer() {
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
Uri uri = Uri.parse(getString(R.string.media_url_mp3));
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(uri);
}
#Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer();
}
}
Check this url for more details
https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2
please check this link :
http://developer.android.com/guide/appendix/media-formats.html
videoview can't support some codec .
i suggested you to use mediaplayer , when get "sorry , can't play video"
I also got stuck with this issue.
I got correct response from server, but couldn`t play video. After long time I found a solution here.
Maybe, in future this link will be invalid.
So, here is my correct code
Uri video = Uri.parse("Your link should be in this place ");
mVideoView.setVideoURI(video);
mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// here write another part of code, which provides starting the video
}}
Check this UniversalVideoView library its simple and straight forward with controller as well.
Here is the code to play the video
Add this dependancyy in build.gradle
implementation 'com.linsea:universalvideoview:1.1.0#aar'
Java Code
UniversalVideoView mVideoView = findViewById(R.id.videoView);
Uri uri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/contactform-d9534.appspot.com/o/Vexento%20-%20Masked%20Heroes.mp4?alt=media&token=74c2e448-5b1b-47b7-b761-66409bcfbf56");
mVideoView.setVideoURI(uri);
UniversalMediaController mMediaController = findViewById(R.id.media_controller);
mVideoView.setMediaController(mMediaController);
mVideoView.start();
Xml Code
<FrameLayout
android:id="#+id/video_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/black">
<com.universalvideoview.UniversalVideoView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
app:uvv_autoRotation="true"
app:uvv_fitXY="false" />
<com.universalvideoview.UniversalMediaController
android:id="#+id/media_controller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:uvv_scalable="true" />
</FrameLayout>
Check whether your phone supports the video format or not.Even I had the problem when playing a 3gp file but it played a mp4 file perfectly.