本地视频仅在我播放网络视频之前播放 - AVFoundation

时间:2014-11-25 12:52:08

标签: ios objective-c video avfoundation avasset

我正在尝试使用AVFoundation播放本地视频。我不知道这里发生了什么,只有当我在网上播放视频然后播放本地视频时才有效。如果我只是播放本地视频,它会给我错误:

项目无法播放 资产跟踪已加载,但无法播放。

这是我的代码:

 - (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提供即时使用的本地URL:

我在这里文件:/// 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

先谢谢!!

1 个答案:

答案 0 :(得分:0)

//您可以使用MPMoviePlayerController播放视频。     添加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];