android play a video from res/raw - android

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.

Related

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

Android Eclipse- how to add a youtube video

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

SD Card video not running in the video view

I have a video file saved on an SD Card. The path to it is saved in my database. While running the video file, I fetch the path from the database.
My video path is:
/mnt/sdcard/VideoLog/2013-01-01 11.18.57.mp4
and the code is as follows:
video_view_player = (VideoView) findViewById(R.id.videoview_player);
video_view_player.setVideoPath(filename);
//video_view_player.setMediaController(new MediaController(this));
//video_view_player.requestFocus();
video_view_player.start();
The filename in the video path is fetched from the database.
I even tried converting the .mp4 video into .3gp, but in vain.
public class MainActivity extends Activity {
VideoView vv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv=(VideoView)findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vv);
Uri video = Uri.parse("/sdcard/sample_mpeg4.mp4");
vv.setMediaController(mediaController);
vv.setVideoURI(video);
vv.start();
}
}
Try out this way:
VideoPLayer = (VideoView) findViewById(R.id.VideoPLayer);
mediaController = new MediaController(m_context);
VideoPLayer.setVideoPath(m_videoUrl);
VideoPLayer.setVideoURI(Uri.parse(videoUrl));
VideoPLayer.setMediaController(m_mediaController);
VideoPLayer.start();
mediaController.show();

Android 2.2 VideoView problem

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

Categories

Resources