2

I'm creating an app using CodenameOne and have to use an ios native interface to generate some video thumbnails. Here is the code I am using in iOS:

-(NSData*)createThumbnailFromMp4:(NSString*)param{

     NSURL *mediaURL = [NSURL fileURLWithPath:param];
     AVAsset *asset = [AVAsset assetWithURL:mediaURL];
     CMTime duration = [asset duration];
     CMTime snapshot = CMTimeMake(duration.value / 2, duration.timescale);

     AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
     CGImageRef imageRef = [imageGenerator copyCGImageAtTime:snapshot actualTime:nil error:nil];
     UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
     CGImageRelease(imageRef);

     return UIImagePNGRepresentation(thumbnail);
}

The param passed in is the full path to the video, but all I get back when I call this is a 0 length byte[]. Here is the code from CodenameOne:

byte[] thumbnailImage = tg.createThumbnailFromMp4(FileSystemStorage.getInstance().getAppHomePath() + movePath);

Where movePath is just the file name of the locally stored video.

Bhumika
  • 876
  • 7
  • 20
Karry
  • 153
  • 6
  • You code seems right. Check value for asset and duration those might be null. Or just check path is correct or not. – Ajay Gabani Jul 04 '16 at 05:02
  • Personally I'd just save the file instead of returning the byte array which is more flaky. I'd create a png file next to the video with the same URL. Notice that Codename One files are **NOT** URL's so this might be failing. Codename One files are regular iOS files with `file:` sometimes prepended. Try checking if the file name starts with file: and if so just removing it and opening the file as usual and not as a URL. – Shai Almog Jul 05 '16 at 04:24
  • Thanks @ShaiAlmog. I have also tried saving the png within the native code but that wasn't working either. I have confirmed that file name starts with file:// so I have tried removing that and just sending in the path starting with /var.. but it still isn't working. – Karry Jul 06 '16 at 02:14
  • Which portion isn't working? FYI if you have a Mac you can debug the native sources (with include source) on the device. I suggest isolating the part that fails, is it the file creation? Is it the data returned from the native API? Is it the loading of the movie file? – Shai Almog Jul 06 '16 at 05:12
  • @shaiAlmog I'm trying to get hold of a mac so I can debug to find which part is failing. Is there a way to open a normal dialog within the native code so that I can try and find out what is happening that way? Thanks. – Karry Jul 06 '16 at 08:40
  • That won't be simple but you can either write to a text file or just return a String from the method with the print outs of your data. If you do want to call back into Java code there is some instructions on how to do that in the developer guide https://www.codenameone.com/manual/advanced-topics.html#_native_interfaces – Shai Almog Jul 07 '16 at 19:07
  • @ShaiAlmog, so it is creating the thumbnail for a downloaded .mp4 video, but not a locally captured .MOV file stored using CodonameOne app and this code http://stackoverflow.com/questions/18362854/saving-an-image-locally-in-codename-one-project. Can you think of any reason why it would fail for the .mov only? – Karry Jul 10 '16 at 02:11
  • I'm shooting in the dark here but maybe it's a file URL issue? Maybe the mov file URL is different? – Shai Almog Jul 10 '16 at 03:52

3 Answers3

0
 AVAsset *asset = [AVAsset assetWithURL:urlvideo];
 AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
 CMTime time = [asset duration];
 time.value = 1000 ;
 CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
 image1 = [UIImage imageWithCGImage:imageRef];
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
Akshay Degada
  • 139
  • 2
  • 13
0

plz try this code

       -(UIImage*)createThumbnailFromMp4:(NSString*)param{

             NSURL *mediaURL = [NSURL fileURLWithPath:param];
            AVAsset *asset = AVAsset *asset = [AVAsset assetWithURL:mediaURL];       
 AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
                imageGenerator.appliesPreferredTrackTransform=TRUE;
                CMTime time = CMTimeMake(1, 1);
                CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
                UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
                CGImageRelease(imageRef);
                return thumbnail;
        }
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
0

Replace the line:

CGImageRef imageRef = [imageGenerator copyCGImageAtTime:snapshot actualTime:nil error:nil];

with

NSError *error = nil;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:snapshot actualTime:nil error:&error];
if (error) {
   NSLog("ERROR: %@", error);
}

This might be helpful in tracking down why the image isn't being created.

tobyc
  • 2,237
  • 2
  • 20
  • 26