Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Video Rendering

Discussion in 'Android' started by Magius96, Aug 4, 2015.

  1. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    The app that I'm nearly done building will provide the user an interface for taking short videos and saving them to a specified folder for the application. There are two parts that I am struggling with. First, I have to be able to display a thumbnail of the video. Second, when the user clicks on the thumbnail, I need to play the video back to them.

    I'm working with a fairly generous budget, so I don't mind using paid assets, as long as they are tried and proven to do what I need, and work on android devices. (Working on iOS is a bonus as well). I am also working with Unity Pro, so that is not an issue either.

    However, I'm certain that if there is an asset, that I should be able to do it myself via code. Can anyone point me in the right direction for getting thumbnail of video files displaying, and for playing back the video itself?
     
  2. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    For thumbnails, have a look at androids thumbnail api:
    http://developer.android.com/reference/android/media/ThumbnailUtils.html

    Using this api myself, works great!

    For the playback when clicking on the video. I think you store the video data, like name, path, in an List or Array? Just use the path to playback the video. If you dont have an solution for videoplayback yet, you can just use Unitys Handheld.PlayFullScreenMovie, if the video format is compatible with it, you could also code your own native video player by using androids media player class, you could also just pass the video path to your native android video player or actually use one of the video players on the asset store. EasyMovieTexture does a good job for video playback.
     
  3. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    Thank you for your quick response. I do have the full path to the video that was created. I have been looking into using Handheld.PlayFulLScreenMovie, but all the code references I can find say that the video had to have been added to the StreamingAssets folder when creating the project. Obviously that's not possible in this case. As far as i can tell, all the existing movie playing options I've seen require the movie to either have been added while creating the app, or to be in ogg format. Neither of these options is really any good for me.

    What I'd really like, and I'm having difficutly finding, is some code, or an asset that will play the video in the device's native video player. I mean, that player works, there is no sense trying to reinvent the wheel.
     
  4. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    As for the ThumnailUtils, i'm still relatively new to Unity, how would I call that from C# code in Unity?
     
  5. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    Handheld.PlayFulLScreenMovie also works if the video is stored somewhere else. Make sure your path is correctly defined. An example:
    Code (CSharp):
    1. void Start () {
    2.         Handheld.PlayFullScreenMovie ("file:///storage/extSdCard/Groot.mp4");
    3.     }
    /storage/extSdCard/Groot.mp4 is my example path, make sure you add file:// so the result looks like above.
    Tested it. Works fawlessly.

    An alternatve, you could just call:
    Application.OpenURL ("file:///storage/extSdCard/Groot.mp4");
    This will open an popup dialog to choose an media player.

    Tho, Handheld.PlayFulLScreenMovie is better for an app imho.

    For the Thumbnail, you should create an native java plugin. Example code:
    Code (JavaScript):
    1.   public static byte[] ReturnBytes(String path) {
    2.     Bitmap bmpt = ThumbnailUtils.createVideoThumbnail(path, 1);
    3.     ByteArrayOutputStream stream = new ByteArrayOutputStream();
    4.     bmpt.compress(Bitmap.CompressFormat.PNG, 100, stream);
    5.     return stream.toByteArray();
    6.   }
    Then from unitys code, use something like:
    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. ...
    3. void Start(){
    4. byt[] byt;
    5. AndroidJavaClass ajc = new AndroidJavaClass("com.thumb.128bit"); //example package name from previosuly compiled jar
    6. byt= ajc.CallStatic<byte[]>("ReturnBytes","/storage/extSdCard/Groot.mp4");
    7. Texture2D tex = new Texture2D(320, 180);
    8. tex.LoadImage(byt);
    9. }
    10.  
    Il neet to get bytes for my project, if thats ok for you, you can literally just use it like that. Else just pass directly the bitmap to unity.

    If its your first time doing an native plugin, have a look at:

    Thats an good tutorial for beginners, with source on github. Just use the thumbnail api with the help of my example code above.
     
    Last edited: Aug 7, 2015
  6. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    You sir are a god-send. I have not tried this yet, but I will definitely be trying this on Monday. Thank you so much, this is the very last piece of the puzzle for my app, and with your help, I just might meet my deadline. :)
     
  7. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    Hope it helps. If you ecounter any further problems, im happy to help ya out :)
     
  8. Magius96

    Magius96

    Joined:
    Feb 28, 2015
    Posts:
    12
    Just wanted to let you know, that I did make my deadline. I was able to get it working with the info you provided. We didn't bother with the thumbnail portion yet, but we at least have the video playing. Figured we'd save the thumbnail for a future update.