Vuforia Play video url from cloud metadata

Vuforia Play video url from cloud metadata 


Step 1:- Vuforia Cloud database we have to add one text file in metadata section. and in this text file, we have to add one video URL (Without streaming file means to say I can't able to play youtube URL because youtube is a streaming server. )

Step 2:- "onVuforiaUpdate" This function return metadata Url and cloud object name


 NSString *video_url = [NSString stringWithFormat:@"%s",result->getMetaData()];


Step 3:- "VideoPlaybackEAGLView" In Eagle view i have again prepare Player with this new Video Url 

[eaglView preparePlayers_st:video_url];


Step 4:- if we don't found any Url then we have to change media type

mediaState = ERROR

Step5:- VideoPlayerHelper class i have add one "load_with_URL" in this function i have pass url to play player in prepare function 

Here i share with you code Step by step 

1> VideoPlaybackViewController 

- (void) onVuforiaUpdate: (Vuforia::State *) state{        // Get the tracker manager:    Vuforia::TrackerManager& trackerManager = Vuforia::TrackerManager::getInstance();        // Get the image tracker:    Vuforia::ObjectTracker* objectTracker = static_cast<Vuforia::ObjectTracker*>(trackerManager.getTracker(Vuforia::ObjectTracker::getClassType()));        // Get the target finder:    Vuforia::TargetFinder* finder = objectTracker->getTargetFinder();        // Check if there are new results available:    const int statusCode = finder->updateSearchResults();        if (statusCode < 0)    {                // Show a message if we encountered an error:        ////NSLog(@"update search result failed:%d", statusCode);        if (statusCode == Vuforia::TargetFinder::UPDATE_ERROR_NO_NETWORK_CONNECTION) {            [self showUIAlertFromErrorCode:statusCode];        }    }    else if (statusCode == Vuforia::TargetFinder::UPDATE_RESULTS_AVAILABLE)    {              // Iterate through the new results:        for (int i = 0; i < finder->getResultCount(); ++i)        {                        const Vuforia::TargetSearchResult* result = finder->getResult(i);                        // Check if this target is suitable for tracking:            if (result->getTrackingRating() > 0)            {                               // Create a new Trackable from the result:                Vuforia::Trackable* newTrackable = finder->enableTracking(*result);                if (newTrackable != 0)                {                                        NSString *video_url = [NSString stringWithFormat:@"%s",result->getMetaData()];                    ////NSLog(@"%@",video_url);                    VuforiaSamplesAppDelegate *appDelegate = (VuforiaSamplesAppDelegate*)[[UIApplication sharedApplication] delegate];                    if([video_url length]!=0)                    {                       [eaglView preparePlayers_st:video_url];                        appDelegate.lastmediaURL = video_url;                    }else                    {                                                [eaglView No_url_available];                        appDelegate.lastmediaURL = @"";                                                                                         }                                       ////NSLog(@"Nishant Checking ---> %s",result->getMetaData());                                        if (extendedTrackingEnabled) {                        newTrackable->startExtendedTracking();                    }                }                else                {                    //NSLog(@"Failed to create new trackable.");                }            }        }    }
}





2>VideoPlaybackEAGLView

- (void) preparePlayers_st:(NSString *)getURL {    ////NSLog(@"9.......................function");    [self Players_refresh:getURL];}
- (void)Players_refresh:(NSString *)getURL
{
    
    ////NSLog(@"10.......................function");
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //back ground thread
        
        
        // For each target, create a VideoPlayerHelper object and zero the
        // target dimensions
        // For each target, create a VideoPlayerHelper object and zero the
        // target dimensions
        for (int i = 0; i < kNumVideoTargets; ++i) {
            videoPlayerHelper[i] = [[VideoPlayerHelper alloc] initWithRootViewController:videoPlaybackViewController];
            videoData[i].targetPositiveDimensions.data[0] = 0.0f;
            videoData[i].targetPositiveDimensions.data[1] = 0.0f;
        }
        
        // Start video playback from the current position (the beginning) on the
        // first run of the app
        for (int i = 0; i < kNumVideoTargets; ++i) {
            videoPlaybackTime[i] = VIDEO_PLAYBACK_CURRENT_POSITION;
        }
        
        // For each video-augmented target
        for (int i = 0; i < kNumVideoTargets; ++i) {
            // Load a local file for playback and resume playback if video was
            // playing when the app went into the background
            VideoPlayerHelper* player = [self getVideoPlayerHelper:i];
            NSString* filename;
            
            switch (i) {
                case 0:
                    filename = @"VuforiaSizzleReel_1.mp4";
                    break;
                default:
                    filename = @"VuforiaSizzleReel_1.mp4";
                    break;
            }
            
            if (NO == [player load_with_URL:filename playImmediately:YES fromPosition:videoPlaybackTime[i] setURL:getURL]) {
                ////NSLog(@"Failed to load media");
            }
        }
        
        dispatch_async( dispatch_get_main_queue(), ^{
            // main thread
        });
    });
}
- (void)No_url_available {
    ////NSLog(@"11.......................function");
    VideoPlayerHelper* player = [self getVideoPlayerHelper:0];
    [player mediastateUpdate];
    
}




3>VideoPlayerHelper

-(void)mediastateUpdate{    mediaState = ERROR;}

- (BOOL)load_with_URL:(NSString*)filename playImmediately:(BOOL)playOnTextureImmediately fromPosition:(float)seekPosition setURL:(NSString *)getURL{    //    (void)AudioSessionSetActive(true);    BOOL ret = NO;        // Load only if there is no media currently loaded    if (NOT_READY != mediaState && ERROR != mediaState) {        NSLog(@"Media already loaded.  Unload current media first.");    }    else {        // ----- Info: additional player threads not running at this point -----                // Determine the type of file that has been requested (simply checking        // for the presence of a "://" in filename for remote files)        if (NSNotFound == [filename rangeOfString:@"://"].location) {            // For on texture rendering, we need a local file            localFile = YES;            NSString* fullPath = nil;                        // If filename is an absolute path (starts with a '/'), use it as is            if (0 == [filename rangeOfString:@"/"].location) {                fullPath = [NSString stringWithString:filename];            }            else {                // filename is a relative path, play media from this app's                // resources folder                fullPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];            }                        // mediaURL = [[NSURL alloc] initFileURLWithPath:fullPath];                       // NSLog(@"BEFOR---%@----",getURL);                        if([getURL length] == 0){                getURL = @"http://183.182.84.29/download/OLX.mp4";            }else            {             mediaState = READY;            }                        //NSLog(@"AFTER---%@----",getURL);            mediaURL = [[NSURL alloc] initWithString:getURL];                        if (YES == playOnTextureImmediately) {                playImmediately = playOnTextureImmediately;            }                        if (0.0f <= seekPosition) {                // If a valid position has been requested, update the player                // cursor, which will allow playback to begin from the                // correct position                [self updatePlayerCursorPosition:seekPosition];            }                        ret = [self loadLocalMediaFromURL:mediaURL];                    }        else {            // FULLSCREEN only            localFile = NO;                        mediaURL = [[NSURL alloc] initWithString:filename];                        // The media is actually loaded when we initialise the            // MPMoviePlayerController, which happens when we start playback            mediaState = READY;                        ret = YES;        }    }        if (NO == ret) {        // Some error occurred        mediaState = ERROR;    }        return ret;}



Note:- this is automatic play the video , if you want to add play pause button then you have to changes in this line "playImmediately = NO"

if (NO == [player load_with_URL:filename playImmediately:YES fromPosition:videoPlaybackTime[i] setURL:getURL]) {                ////NSLog(@"Failed to load media");            }






Comments