Log in or register to post comments

Video playback issue with Unity/Android

January 22, 2013 - 1:19pm #1

Hello guys, 

having a bit of a problem with video playback, using the Unity extension on an android device. I already solved the very same problem on the iOS platform, so I really hope, that there will be a way on android as well. Please allow me to tell you a bit more about my setup and what I'm trying to achieve.

So, I'm using both Vuforia AR and video playback plugin to enable augmented reality content on a mobile device. The videos I want to show, have to be downloaded at runtime, so I can't ship them inside the streaming assets folder. That would have been too easy :) Since the app also has many other (native) features, that I can't build (at least it would be very time consuming) inside Unity, I made Unity part of a regular android eclipse project. It took me some time to integrate Unity as a library, make sure trackers get recognized, etc..., but all that is 100% up and running. No issues there.

When I launch my app (Unity not yet running at that moment), it downloads the videos from my server and stores them inside a folder like that: /data/data/com.mycompany.myproject/files/videoassets/

The download works perfectly and the videos are actually located at that path (I checked). Now I fire up the Unity 3D part of my application and once a tracker image is recognized (that works too), I want it to load one of these videos.

My first approach was to use Application.persistentDataPath and then simply append the necessary subfolders, to finally rebuild the path mentioned above. I checked that in console and those paths match.

As you probably know, the vuforia video prefab by default needs a video file name set inside editor. Since I can't do that, because I don't know the video names in advance, I had to customize both VideoPlaybackBehaviour.cs and VideoPlayerHelper.cs. My modifications are minimal though. Maybe it's not too confusing, if I tell you, how I solved the problem for the iOS version of my app.

VideoPlaybackBehaviour has a variable named m_path. On iOS I manually set that path to the video I want to play back. The videos are located in a subfolder of my Documents folder: Documents/myvideos/

When I tried to use Application.persistentDataPath to hand it the exact path to my video, it wouldn't work either, but as soon as I handed it a path like: ../Documents/myvideos/myvideo.mp4 it started to work. Notice the ../ - that was crucial. Without that, no chance of getting the video to play. It appeared, as if I had to go one folder back (hence the ../), then go inside my Documents folder and take it from there.

Now, my problem is to find a similar solution on android as well. Long story short, I guess it comes down to this: The video playback plugin does not accept an exact path (using Application.persistentDataPath). It kinda wants something relative (at least my guess).

If you think my approach is way to complicated and there is an easier solution, please feel welcome to advice. All I want to do, is loading a video, that is not located inside the streaming assets folder, but inside a folder, I can write to at runtime.

Thanks in advance. Your help will be very much appreciated.

Video playback issue with Unity/Android

September 11, 2016 - 12:27am #17

were you able to accomplish what you intended to do . if yes , could you please help me to achieve that ?

Video playback issue with Unity/Android

September 6, 2013 - 4:01am #16

Addition:

I saw, that vuforia uses the Unity built in fullscreenplayer on Android. So the fix for video from internal storage could only be done by Unity.

Video playback issue with Unity/Android

September 6, 2013 - 3:44am #15

Hey everyone,

 

I found out, the videos in internal storage world-readable permissions will not play in Fullscreen on Android, because of world-readable permissions. Files in internal storage are only read/writable by the app, which stored them there. The used MediaPlayer in vuforia example for Android Fullscreen Play has no access to the file. If it were on sd-card, the mediaplayer could read it. But when it is not desired and you wish to use the videos only from internal storage, then you must use a FileDescriptor with MediaPlayer.setDataSource().

Implementation for native Android Code found in the net:

FileInputStream fi = new FileInputStream(file);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fi.getFD());
pl.prepare();
pl.start();

(solution for Android Code found here: http://stackoverflow.com/questions/9732609/videoview-can-play-mp4-file-in-application-data-directory)

I don´t know, if I could change vuforia Code for Android Fullscreen Play by myself, but I think not. May be in some of the next versions Vuforia could bring this "fix" in the VideoExample. If it is needed. May be the most people don´t need it. For now and a "quickfix" I will use SD-Card on Android.

Greetings,

Martin

Video playback issue with Unity/Android

September 5, 2013 - 11:53pm #14

You are welcome to search the forums, however this is not really anything to do with Vuforia, so you may not have much luck here.

Video playback issue with Unity/Android

September 5, 2013 - 6:55pm #13

Hi Alessandro,

Do you know how to download video from my server and save it?

Thank you in advance.

Video playback issue with Unity/Android

September 5, 2013 - 8:40am #12

Yes, I did so but no solution yet. I tried the standard persistantDataPath given bei Unity:

/data/data/myproject/files/movie.mp4

This works on devices, which can play video to texture. But not on the other, even they can play Fullscreen.

So I tested the same what you do in the vuforia Video Sample.

I extended the path with file:// at the beginning, but this doesn´t work to. I will try other things. First looking to native JAVA/Android programming and solving the problem there and I will give Feedback, what is needed in Unity/Vuforia to solve this problem.

 

Thanks for your answer.

Video playback issue with Unity/Android

September 5, 2013 - 7:51am #11

Hi,

the fullscreen case relies on the variable mFullScreenFilename in the VideoPlayerHelper.cs script

(in Assets/Vuforia Video Playback/Scripts/).

You may need to adjust the value of that variable for that specific case, as the sample is meant to show the general functionality, but the sample code might need to be slightly adjusted for custom / specific cases.

 

Video playback issue with Unity/Android

September 5, 2013 - 6:02am #10

Hi Alessandro,

sorry for opening this thread again, but my question fits on this one. Everything is the same as in the example of scoopandspoon.

The videos are downloaded and saved in persistanceDataPath. On iOS all is fine. But Android makes problems. Devices which are able to play video as texture work. But devices, which are NOT able to play video as texture, don´t show the video. They are able to show them in Fullscreenmode, I checked that with the vuforia example and in reading the debugflags. But it seems, they show the FullscreenVideo only then, when the video is in the streamingAssetsFolder. If I save them in persistantDataPath, devices witjh texture-ability can play them, devices without video-texture-ability can´t play the fullscreen video. I think it has something to do with the path, the Fullscreenplayer can´t find the video. But I am not sure, if this is realy the problem. It´s just my thought.

Have you tested video in persistantDataPath on Android with Devices, that only support FullscreenMode?

Greetings,

Martin

Video playback issue with Unity/Android

September 5, 2013 - 3:00am #9

HI, scoopandspoon.

I want to ask you how to download video from my server and save it on ipad.

Thanks you in advance.

Video playback issue with Unity/Android

January 25, 2013 - 12:25am #8

You're welcome.

Video playback issue with Unity/Android

January 24, 2013 - 11:56pm #7

Thanks,

your help is very much appreciated. The code you posted looks quite interesting. Haven't been able to get it to work yet, but once I have a little more time at my hand, I'll give it another go and definitely report back.

Thanks again.

 

Video playback issue with Unity/Android

January 24, 2013 - 2:33am #6

Hi, I have investigated the issue a bit more, and I managed to play the video from the persistentDataPath;

I will share with you the steps I have followed:

  1. Manually copied a video file from my PC to the device SD card (I did that using ADB push, via USB)
  2. Setting the file Path in the Unity Editor inspector to /mnt/sdcard/Movies/video1.m4v
  3. Added the code below in the public bool Play(bool fullScreen, float seekPosition) function in VideoPlayerHalper.cs

 

Int he code below, in particular, you can see that I am copying the video file from the SD card to the Application.persistentDataPath directory and then changing the mFilename to that file (i.e. not using the SD card any more; so the file in the SD card is only used as source file to be copied to the persistentDataPath; this is because I don;t have an implementation which downloads videos from a server, so I use the SD card as a replacement for a server, if you like);
then I play the video from the persistentDataPath;
 
and it works.
 
Here is the code mentioned above:
public bool Play(bool fullScreen, float seekPosition)
    {
        // On Android we use Unity's built-in full screen movie player

        // On iOS we overlay a native full screen player as a new subview of the main window
        // (note that the Unity engine is not paused in this case)
		
		Debug.Log("=====");
		Debug.Log("=====");
		Debug.Log("=====");
		
		Debug.Log("mFilename is: " + mFilename);
		
		if (File.Exists (mFilename)) {
			Debug.Log ("File " + mFilename + " exists.");
		}
		else {
			Debug.Log ("Warning !!! - File " + mFilename + " does not exist");
		}
		
		
		string filenameInPersistentDataPath = Application.persistentDataPath + "/" + "copy.m4v";
		Debug.Log("filenameInPersistentDataPath is: " + filenameInPersistentDataPath);
		
		if (File.Exists (filenameInPersistentDataPath)) {
			Debug.Log ("File " + filenameInPersistentDataPath + " exists.");
		}
		else {
			Debug.Log ("Warning !!! - File " + filenameInPersistentDataPath + " does not exist");
			
			// Let's copy the file, since it does not exist yet
			Debug.Log ("Copying file to persisteneDataPath ");
			try 
			{
				File.Copy(mFilename, filenameInPersistentDataPath);
				Debug.Log ("Successfully copied file to persistentDataPath !");
			}
			catch (Exception ex) 
			{
				Debug.Log (" !!! - Failed to copy file to persistentDataPath");
				Debug.Log (ex.Message);
			}
		}
		
		
		Debug.Log ("Verification - Listing files in persistentDataPath: " + Application.persistentDataPath);
		string[] pdpFiles = Directory.GetFiles(Application.persistentDataPath);
		foreach (string fn in pdpFiles) 
			Debug.Log (fn);
		
		
		
		if (File.Exists(filenameInPersistentDataPath))
		{
			mFilename = filenameInPersistentDataPath;
			Debug.Log ("setting the mFilename to the filenameInPersistentDataPath");
		}
		
		
		Debug.Log ("Starting playing file: " + mFilename);
		
		
		if (fullScreen && (Application.platform == RuntimePlatform.Android))
        {
            if (mFilename == null)
            {
                return false;
            }

            Handheld.PlayFullScreenMovie(mFullScreenFilename, Color.black, FullScreenMovieControlMode.Full, FullScreenMovieScalingMode.AspectFit);
            return true;
        }
        else
        {
            return videoPlayerPlay(fullScreen, seekPosition);
        }
    }

 

One important thing is to use the Debug.Log() combined with File.Exists() function and Directory.GetFiles() functions, so to verify what is exactly happening at every step.

So, I don;t know what exactly goes wrong in your case, but I hope you can use the code above to dig into your problem.

Let me know how it goes, I'm happy to help further.

 

 

Video playback issue with Unity/Android

January 23, 2013 - 11:44am #5

Hi,

thanks for the info. That sounds very interesting. I'll do a test and report back.

However, since I don't know if users have a sdcard installed, I can't 100% rely on that. Anyways, still worth a shot.

Video playback issue with Unity/Android

January 23, 2013 - 11:06am #4

Hi, it seems that using the Application.persistentDataPath (i.e. "/data/data/com.myorg.myproj/files") can incur in some read/write issues; this may possibly affect the ability of the videoplayer to load such file (although it is not clear why some Unity functions succeed in writing and reading from there, while the videoplayer fails);

however I have managed to use the SD Card of the Android device (instead of the persistentDataPath);

the path to the SD Card is specified by:

"/mnt/sdcard/"

so, if you have for instance a video called "myvideo.m4v" in a directory called "videos" in the sdcard, then the correct path to the video will be:

"/mnt/sdcard/videos/myvideo.m4v"

So, to make a simple test you could just copy your video file to the device SD card and just set the m_path variable to such path (/mnt/sdcard/ .....);

in my case this worked flawlessly for a few devices.

If that works, the next steps would be to consider one of these two options:

  1. store the downloaded videos into the sdcard (if your downloading code allows for such a thing)
  2. dynamically copy (using some simple C# script functions) the downlaoded videos from the persistentDataPath into the SDCard

Could you do such test ?

 

 

 

Video playback issue with Unity/Android

January 23, 2013 - 1:38am #3

Hello,

yes, I'm using the video-on-texture.

Video playback issue with Unity/Android

January 23, 2013 - 1:34am #2

Hi, can you confirm that you are using the "video-on-texture" and not the fullscreen one ?

(asking because this can make a difference in the way the file path is handled)

Log in or register to post comments