views:

1099

answers:

6

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.

A: 

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.

hwrdprkns
hey thanks dude but i have looked at this page for like million times and have used this line of code. It does play audio files but not the video and i think there is something related with videoview and assets or raw folder. If you have any idea in your head please feel free to share or a piece of code.
Abhishek Talwar
If I call MediaPlayer.create(contect, R.raw.video) I get the same exception, inside the MediaPlayer class, as if I prepare the Mediaplayer myself with a file descriptor.
Janusz
A: 

a- use the media player api and the sample code. b- put the media file in raw folder c- get the file descriptor to the file d- mdeiaplayer.setDataSource(fd,offset,); - its a three argument constructor e- then when onPreared , mediaplayer.start();

Khajan
ok thnxi will try this but can you please provide me with some sample codethat would be very helpfull. thanks again
Abhishek Talwar
Does not work, have a look at the question for an explanation.
Janusz
A: 

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>
Sandeep
This is not working at all. The Uri points just to /2130968576 in the case of my resource.
Janusz
+4  A: 

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 :
http://stackoverflow.com/questions/3367894/android-copy-rawfile-to-sdcard-video-mp4

NSchubhan
A: 

Hi guys,
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
Sephy
A: 

Unless it has changed in Android 2.1 or 2.2, you can't play a video that is included in the application package (unlike audio resources).

Ian G. Clifton