views:

371

answers:

1

So we have buttons linked up to display images/videos/audio on click depending on a check we do earlier. That part works fine. It knows which one to play, however, when we click the buttons for video and audio, nothing happens. The image one works fine.

The video and audio are being taken for a URL online, they are not local, but everywhere said this was still possible. Here is a little snippet of the code where we play the two files:

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

    }

See anything wrong? By the way it compiles and runs, but nothing happens when we hit the button that executes that code.

A: 

I am not sure why they are not playing, does it ever reach the video and audio conditons?

Either way your currently missing before you play the movie

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

You will then need the function to handle the callback

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