Local video only plays when i play a web video before - AVFoundation

282 views Asked by At

Im trying to play a local video using AVFoundation. I don't know whats going on here that only if I play a video on the web and then play the local video it works. If I just play the local video it gives me the error:

Item cannot be played he assets tracks were loaded, but could not be made playable.

And Here's my code:

 - (void)setURL:(NSURL*)URL
{

if (mURL != URL)
{
    [mURL release];
    mURL = [URL copy];

    NSLog(@"Im here %@",mURL);
}
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:mURL options:nil];

    NSArray *requestedKeys = [NSArray arrayWithObjects:kTracksKey, kPlayableKey, nil];

    /* Tells the asset to load the values of any of the specified keys that are not already loaded. */
    [asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
     ^{      
         dispatch_async( dispatch_get_main_queue(), 
                        ^{
                            /* IMPORTANT: Must dispatch to main queue in order to operate on the AVPlayer and AVPlayerItem. */
                            [self prepareToPlayAsset:asset withKeys:requestedKeys];
                        });
     }];
}

- (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys
{

if (!asset.playable) 
{
    NSString *localizedDescription = NSLocalizedString(@"Item cannot be played", @"Item cannot be played description");
    NSString *localizedFailureReason = NSLocalizedString(@"The assets tracks were loaded, but could not be made playable.", @"Item cannot be played failure reason");
    NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
                               localizedDescription, NSLocalizedDescriptionKey, 
                               localizedFailureReason, NSLocalizedFailureReasonErrorKey, 
                               nil];
    NSError *assetCannotBePlayedError = [NSError errorWithDomain:@"StitchedStreamPlayer" code:0 userInfo:errorDict];

    [self assetFailedToPrepareForPlayback:assetCannotBePlayedError];

    return;
}


if (self.mPlayerItem)
{

    [self.mPlayerItem removeObserver:self forKeyPath:kStatusKey];            

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:AVPlayerItemDidPlayToEndTimeNotification
                                                  object:self.mPlayerItem];
}

self.mPlayerItem = [AVPlayerItem playerItemWithAsset:asset];

[self.mPlayerItem addObserver:self 
                  forKeyPath:kStatusKey 
                     options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                     context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:self.mPlayerItem];

seekToZeroBeforePlay = NO;

if (!self.mPlayer)
{
    [self setPlayer:[AVPlayer playerWithPlayerItem:self.mPlayerItem]];  

    [self.player addObserver:self 
                  forKeyPath:kCurrentItemKey 
                     options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                     context:AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext];

    [self.player addObserver:self 
                  forKeyPath:kRateKey 
                     options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                     context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
}

if (self.player.currentItem != self.mPlayerItem)
{
    [self.mPlayer replaceCurrentItemWithPlayerItem:self.mPlayerItem];

    [self syncPlayPauseButtons];
}

[self.mScrubber setValue:0.0];
[self play:nil];
loading.hidden = YES;

}

NSLog gives the local URL that im using:

Im here file:///var/mobile/Containers/Data/Application/E806EC2A-2C5D-4B86-AD05-D9FD29E8FDDD/Documents/downloads/VVV%20-%20One%20Direction,%20Band%20Aid%2030,%20Mark%20Ronson,%20Bruno%20Mars,%20OneRepublic,%20DSCVR%20Ones%20To%20Watch%202015.m4v

Thanks in Advance!!

1

There are 1 answers

1
Monikanta On

//You can use MPMoviePlayerController for Playing a Video. Add MediaPlayer.framework (#import )

MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] init];
movieController.controlStyle = MPMovieControlStyleDefault;//MPMovieControlStyleNone;
[movieController setContentURL:@"Your local Url Path"];
[movieController.view setFrame:CGRectMake (0,0,214,186)];

[self.view addSubview:movieController.view];
[movieController.view bringSubviewToFront:self.view];
[movieController prepareToPlay];
[movieController play];