Android load video in VideoView - android

I want to load a video in a video view from raw folder with the following code
String uri = "android.resource://" + getPackageName() + "/" + R.raw.preview;
VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
mVideoView.setVideoURI(Uri.parse(uri));
mVideoView.requestFocus();
mVideoView.start();
I receive NullPointerException at this line: mVideoView.setVideoURI(Uri.parse(uri));
Any ideas in what should I do ?

Make sure the findViewById function call is returning a VideoView object and is not null.
Null pointer errors typically happen when you call an method to a object that is null.
Chances are the reference to R.id.videoView1in your layout xml file is wrong or you could have an error in your xml layout file that isn't showing up.
If you're using Eclipse or Android Studio, the R.i.videoView1 should be blue, showing that it was found in the layout file.
Also you can verify the object isn't null before calling the methods to be sure. See below:
String uri = "android.resource://" + getPackageName() + "/" + R.raw.preview;
VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
if (mVideoView != null)
{ mVideoView.setVideoURI(Uri.parse(uri));
mVideoView.requestFocus();
mVideoView.start();
} else
{ //toast or print "mVideoView is null"
}

Try this:
String path = "android.resource://" + getPackageName() + "/" + R.raw.preview;
VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
mVideoView.setVideoPath(path);

Related

Android: not able to play video using videoview

Please find below xml
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Java Code:
String url = "android.resource://com.example.androidtutorial/" + R.raw.instructions;
videoView.setVideoURI(Uri.parse(url));
videoView.start();
Error is shown in Logcat:
2019-10-20 10:51:16.708 5063-5063/? E/androidtutoria: Unknown bits set in runtime_flags: 0x8000
2019-10-20 10:51:22.825 5063-5080/com.example.androidtutorial E/MediaPlayerNative: error (1, -2147483648)
2019-10-20 10:51:23.178 5063-5063/com.example.androidtutorial E/MediaPlayer: Error (1,-2147483648)
I saw many samples on the internet that uses the same code, but in my case, it is not working.
I advice you to user a WebView instead of video view.
that will work better.
But for your answer
1.check your connection
2.check your permissions
3.check Url that you build with Debug
4.put start method in try catch
Now its seems issue in this line:
videoView.setVideoURI(Uri.parse(url));
uri means a file path in your device not a url, check it and try again.
u can update me in comments
Edited:
use this method:
public Uri getRawUri(String filename) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
File.pathSeparator + File.separator + getPackageName() + "/raw/" +
filename);
}
next solution: use Uri.fromFile instead of Uri.parse
And
Use this 2 methods:
video.setVideoURI(URI);
video.setMediaController(new MediaController(this));
video.requestFocus();
video.start();
Try this in your onCreate() method:
Uri URI = getRawUri(instructions);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video)
video.setVideoURI(URI);
video.setMediaController(mediaController);
video.requestFocus();
video.start();

VideoView "Can't play video"

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
String uriPath = (Environment.getExternalStorageDirectory() + "MyVideo.3gp");
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}
Main xml
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_gravity="center_horizontal" />
what's wrong with my code? I can play the video through storage but won't run on my app
Try this:
String uriPath = (Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyVideo.3gp");
I think it must be a wrong path reference.
P.S: Not sure if File.separator is necessary.
In Video View If You get "Can't play video" message than it occurs because of any of below reasons:
1) If your file format is Unsupported
2) If your file codecs are Unsupported
3) If your file contains any internal Error
4) Forgot android.permission.READ_EXTERNAL_STORAGE permission in manifes

Code to open video in videoView from android phone storage

I have been modifying an example video player code; the example code played the video from the program resource directory. I copied the video to the phone's /storage/emulated/0/DCIM/ directory and add three lines:
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
to replace:
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This change causes a file not found error. The video is in the Phone storage DCIM directory. I also tried adding the .mp4 to the video name.
In debug mode the uri appears to be the correct value: /storage/emulated/0/DCIM/steprock
Can anyone spot the code error? A partial listing is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView = (VideoView) findViewById(R.id.video_view);
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
//videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This is code which play video from path you describe for video in VideoView.
// Video path.
path = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock.mp4";
videoView = (VideoView) findViewById(R.id.video_view);
final MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse(path);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
I found the error, forgot to add the read permission below to the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application ...

How to play video from raw folder with Android device?

Please help,
How to play videos in android device from raw folder for offline mode?
Successful example1: I can play the video from SDcard used the below code.
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("file:///sdcard/test.mp4");
intent.setDataAndType(uri, type);
startActivity(intent);
Failed example2:
Question: May I put the test.mp4 to res/raw folder?
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.taipei);
intent.setDataAndType(uri, type);
startActivity(intent);
Have anyone can help me? Please.
Copy the video into your project's res/raw folder. Create raw folder under res folder. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
Create videoView in your xml file.
// To get files from any resource folder (eg: raw, drawable, etc.)
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension, "raw", getPackageName());
// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path));
Check this solution How to play videos in android from assets folder or raw folder?
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.your_raw_file); //do not add any extension
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
in my code "applicationdemo" is the the name of my video file.
String video_url = "android.resource://" + context.getPackageName() + "/" + R.raw.applicationdemo;
final VideoView videoView = findViewById(R.id.dialog_video);
Uri videoUri = Uri.parse(video_url);
MediaController mediaController= new MediaController(context);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(videoUri);
videoView.requestFocus();
videoView.start();
I struggled with this for dynamic video names. The solution that worked for me was:
//Somewhere set the video name variable
String video+name="myvideo";
//setup up and play video
VideoView videoView=(VideoView)findViewById(R.id.video);
videoView.setVisibility(View.VISIBLE);
String uriPath = "android.resource://"+getPackageName()+"/raw/"+ video_name;
Uri UrlPath=Uri.parse(uriPath);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(UrlPath);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
if (position == 0) {
try{
videoView.requestFocus();
videoView.start();
}catch (Exception e){
System.out.printf("Error playing video %s\n", e);
}
}else{
videoView.pause();
}
}
});
And in XML
<VideoView android:layout_width="300dp"
android:id="#+id/video"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:keepScreenOn="true"
/>
i think , everyone gave an answer, but doesn't explain the scenario. The main problem here is, If im not mistaken , Android assume that the video coming from your SD Card is dynamic, where in it could be possible , that the format is not supported or supported, thus it enables / ask you to select or open for other third party media software.
While anything you play UNDER RAW folder, requires a handler such as videoview or built in media player which leads to conclusion that anything you put in your RAW folder should be supported / readable by the Android OS.
However , the thread starter here wants that his RAW files , to be played using a third party media player.
This Solution will exactly helps you that what you want.
VideoView myVideo;
private MediaController media_control;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideo = (VideoView) findViewById(R.id.playVideo);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bootanimation_nexus);
media_control = new MediaController(this);
myVideo.setMediaController(media_control);
myVideo.setVideoURI(uri);
myVideo.start();
}
player = ExoPlayerFactory.newSimpleInstance(requireActivity(), new DefaultTrackSelector(), new DefaultLoadControl());
Uri uri = RawResourceDataSource.buildRawResourceUri(getOfflineVideo(offlinePosition));
MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(requireActivity(),
"MyExoplayer"), new DefaultExtractorsFactory(), null, null);
//setup player with mediaSource

Eclipse is Marking File Path for VideoView as incorrect

I have a VideoView here but Eclipse is saying raw cannot be resolved to a verable and apw cannot be resolved to a variable at line videoHolder.setVideoURI(Uri.parse(#raw/apw.avi));
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView) findViewById(R.id.VideoView);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoURI(Uri.parse(#raw/apw.avi));
videoHolder.requestFocus();
videoHolder.start();
Try this way :
Uri videouri = Uri.parse("android.resource://" + getPackageName() + "/raw/apw");
videoHolder.setVideoURI(videouri);
Thanks.
You must copy the video into your project's res/raw folder.
It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename: apw.mp4
When you work with this resource in code, you will reference through the generated R statics - it will have the file extension removed: R.raw.apw
VideoView videoHolder = (VideoView) findViewById(R.id.VideoView);
videoHolder.setMediaController(new MediaController(this));
String uri = "android.resource://" + getPackageName() + "/" + R.raw.apw;
videoHolder.setVideoURI(Uri.parse(uri));
videoHolder.requestFocus();
videoHolder.start();

Categories

Resources