1

我正在尝试使用 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:

我在这里 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

提前致谢!!

4

1 回答 1

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];
于 2014-11-25T13:11:15.187 回答