在iPhone中播放视频和音频无法正常工作

时间:2010-03-17 22:26:52

标签: objective-c iphone mpmovieplayercontroller avaudioplayer

因此,根据我们之前的检查,我们将按钮连接起来以显示图像/视频/音频。那部分工作正常。它知道要播放哪一个,但是,当我们点击视频和音频按钮时,没有任何反应。图像效果很好。

视频和音频正在网上拍摄,它们不是本地的,但到处都说这仍然是可能的。以下是我们播放这两个文件的代码片段:

if ( [fName hasSuffix:@".png"])
    {
        NSLog(@"PICTURE");
        NSURL *url = [NSURL URLWithString: 
                      fName];

        UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
        self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        // self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MainBG.jpg"]];
        [self.view addSubview:[[UIImageView alloc] initWithImage:image]];

    }
    if ( [fName hasSuffix:@".mp4"])
    {
        NSLog(@"VIDEO");
        //NSString *path = [[NSBundle mainBundle] pathForResource:fName ofType:@"mp4"];
        //NSLog(path);
        NSURL *url = [NSURL fileURLWithPath:fName];

        MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [player play];

    }
    if ( [fName hasSuffix:@".mp3"])
    {
        NSLog(@"AUDIO");
        NSURL *url = [NSURL fileURLWithPath:fName];

        NSData *soundData = [NSData dataWithContentsOfURL:url];
        AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithData:soundData error: nil];
        [avPlayer play];

    }

看错了什么?顺便说一下,它编译并运行,但是当我们点击执行该代码的按钮时没有任何反应。

1 个答案:

答案 0 :(得分:0)

我不确定他们为什么不玩,有没有达到视频和音频条件?

在您播放电影之前,您目前失踪的方式

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(movieFinishedCallback:)
        name:MPMoviePlayerPlaybackDidFinishNotification object:player];

然后您将需要该函数来处理回调

-(void)movieFinishedCallback:(NSNotification *)notification{
    NSLog(@"MOVIE FINISHED");
    if(player == [notification object]){
        [[NSNotificationCenter defaultCenter] removeObserver:self
                name:MPMoviePlayerPlaybackDidFinishNotification object:player];
        [player release];
        player = nil;
    }
}