0

因此,根据我们之前所做的检查,我们将按钮链接起来以在点击时显示图像/视频/音频。那部分工作正常。它知道要播放哪一个,但是,当我们单击视频和音频按钮时,什么也没有发生。图像一个工作正常。

视频和音频被用于在线 URL,它们不是本地的,但到处都说这仍然是可能的。这是我们播放两个文件的代码片段:

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];

    }

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

4

1 回答 1

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;
    }
}
于 2010-07-14T15:17:37.447 回答