2014-11-25 114 views
1

我試圖播放本地視頻使用AVFoundation。我不知道這裏發生了什麼,只有當我在網絡上播放視頻,然後播放本地視頻時才起作用。如果我只是播放本地視頻,它會給我錯誤:本地視頻只播放時,我以前播放網絡視頻 - 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給出了使用,即時通訊本地網址:

我在這裏的文件:///無功/移動/集裝箱/數據/應用/ E806EC2A-2C5D- 4B86-AD05-D9FD29E8FDDD /文檔/下載/ VVV%20-%20One%20Direction,%20Band%20Aid%2030%20Mark%20Ronson,%20Bruno%20Mars,%20OneRepublic,%20DSCVR%20Ones%20To%20Watch%202015。 m4v

在此先感謝!

回答

0

//您可以使用MPMoviePlayerController播放視頻。 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]; 
+0

不工作,我想使用AvFoundation。順便說一句,非常感謝你的回答:) – 2014-11-25 13:37:48