iPhone MPMoviePlayer没有视频

时间:2011-06-24 08:04:44

标签: iphone xcode mpmovieplayer live-streaming

这是应该播放实时视频/音频流的代码, 它工作正常,但唯一的问题是它没有显示视频, 只有音频不是视频......

#import <MediaPlayer/MediaPlayer.h>

@implementation movieplayerViewController
-(void)awakeFromNib{
NSURL *mediaURL = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
MPMoviePlayerController *mp  = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:nil]; 

[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setFullscreen:YES];

[self.view addSubview:[mp view]];

[mp prepareToPlay];
[mp play];

}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    NSError *error = [[notification userInfo] objectForKey:@"error"];
    if (error) {
        NSLog(@"Did finish with error: %@", error);
    }
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

2 个答案:

答案 0 :(得分:0)

如果您使用的是iOS&lt; 4.0然后就会发生这种情况。因为在iOS 4.0中有新的播放视频类。希望下面的代码可以帮助你

-(void)playMovieFromLocalPath:(NSString *)strPath{

    NSURL *movieURL = [[NSURL alloc]initFileURLWithPath:strPath];


    NSString *strVersion = [[UIDevice currentDevice] systemVersion];
    float version = [strVersion floatValue];

    if(version < 4.0){
        MPMoviePlayerController *themovie = [[MPMoviePlayerController alloc]initWithContentURL:movieURL];
        themovie.scalingMode=MPMovieScalingModeAspectFill;
        [themovie play];
    }
    else{
        MPMoviePlayerViewController *themovie = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidFinishPlaybackWithReason:) name:MPMoviePlayerPlaybackDidFinishNotification object:themovie.moviePlayer];
        [self presentMoviePlayerViewControllerAnimated:themovie];
    }
}

-(void)DidFinishPlaybackWithReason:(NSNotification *)aNotification{
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];
    [player stop];
    [self dismissMoviePlayerViewControllerAnimated];
}  

答案 1 :(得分:0)

ı以这种方式写道。谢谢你的帮助

-(void)awakeFromNib{ [self playMovieFromLocalPath:@"http://eu01.kure.tv:1935/liveedge/shaber.smil/playlist.m3u8"]; }