I've been researching for some time now, and all the questions I can find point to mp4 'hinting' but I have no control over the videos.
I was about to give up when i discovered that MX video player on the android market can somehow play these mp4 urls I have. So at the moment I use an intent to launch the urls in mx player but I'd much rather display the video within my app.
Any ideas how mx player does this would be great,
Thanks
Check this. It will help you.
String LINK = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
setContentView(R.layout.video);
VideoView videoView =(VideoView)findViewById(R.id.webView);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
Refer this link i had pasted the whole code for playing the mp4 videos in your app by creating a videoview and you might minor error you can manually remove that error.
You can do it using FullscreenVideoView class. Its a small library project. it's gradle is :
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.0'
It's steps is here:
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
Dear All;
I am new in android app;
I try to play video from my resource , but no idea how to do it...
any help??
regards..
I think the following can help (that's the way I implement it):
VideoView vd;
vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://org.android.Test.Test/raw/vid"); vd.setVideoURI(uri);
vd.start();
where Test.Test is the name of my package.
Remark: You didn't ask it, but some video files do not work on emulator (you hear only
the music, but see no picture). In this case debug it on your phone.
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).
I want to play video files in my application which are part of my application.
for that i create 'raw' folder in 'res' and write following code in my activity.
MediaPlayer mp = MediaPlayer.create(VideoPlayer.this,R.raw.jeevrangala);
mp.start();
now i am testing it on emulator, but it does not displaying any thing.
is any one have solution to play video files in raw folder. please let me know.
Initial i was trying to play video files from raw folder. but cant run it. so i use another way to do this
VideoView video=(VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
//Uri uri = Uri.parse("android.resource://play.vedio/"+R.raw.dobeernotdrugs);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://one.two/raw/"+resource);
video.start();
video.requestFocus();
resource is file name which you want to play and one.two is package name your path may as like
"android.resource://package_name/raw/file_name"
You can use this code to run video on emulator.. for me it is working
VideoView videoHolder = (VideoView) findViewById(R.id.video_view);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.samplevideo);
videoHolder.setVideoURI(video);
videoHolder.start();
And please turn on your graphics acceleration for the emulator..
I am trying to play a video in android emulator
I have the video in my assets folder as well as the raw folder
But after doing some research still i cant play video in my emulator
i am working on android 2.1
My video format is mp4 so i don't think that should be a problem
Could anyone just give me an example code so that i can understand a bit more?
The problem is that the VideoView that I need to display the Video will take only a URI or a File path to point to the Video.
If I save the video in the raw or assets folder I can only get an input stream or a file descriptor and it seems nothing of that can be used to initialize the VideoView.
Update
I took a closer look at the MediaPlayer example and tried to start a MediaPlayer with a FileDescriptor to the assets files as in the code below:
SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);
player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Now I get a the following exception:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
It seems there is no other way then copying the file to the sdcard on startup and that seems like a waste of time and memory.
## Perfectly Working since Android 1.6 ##
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = getUriFromRawFile(context, R.raw.your_raw_file);
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();
And then
public static Uri getUriFromRawFile(Context context, #ResRaw int rawResourceId) {
return Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.getPackageName())
.path(String.valueOf(rawResourceId))
.build();
}
## Check complete tutorial ##
String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();
where videocontainer of type videoview.
Try:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
PlayVideoActivity.java:
public class PlayVideoActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
VideoView videoView = (VideoView) findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
videoView.start();
}
}
activity_play_video.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</VideoView>
</LinearLayout>
If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.
So, solutions :
change the extension of the file to .mp3 or .png and see if it works
build your app manually and use the zip-0 option
Did you try to put manually Video on SDCard and try to play video store on SDCard ?
If it's working you can find the way to copy from Raw to SDcard here :
android-copy-rawfile-to-sdcard-video-mp4.
I found it :
Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);
Personnaly Android found the resource, but can't play it for now. My file is a .m4v
VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);
//Set video name (no extension)
String myVideoName = "my_video";
//Set app package
String myAppPackage = "com.myapp";
//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);
//Set the video URI
myVideo.setVideoURI(myVideoUri);
//Play the video
myVideo.start();
https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f
I think that you need to look at this -- it should have everything you want.
EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
But I still recommend reading the information at the link.
It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)
here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here:
android media player not working
In the fileName you must put the relative path to the file (without /asset)
for example:
player.setDataSource(
getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);
Use the MediaPlayer API and the sample code.
Put the media file in raw folder.
Get the file descriptor to the file.
mediaplayer.setDataSource(fd,offset,length); - its a three
argument constructor.
Then when onPreared , mediaplayer.start();
MainCode
Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);
myVideoView=(VideoView)findViewbyID(R.idV.Video_view);
myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();
XML
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="+#/Video_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>