views:

1891

answers:

5

I am working on an iPhone application that uses a MediaPlayer to play a couple different videos. It works great for the first video but when I try to play another the screen stays black and only the audio plays. Does anyone have any idea why this might be happening?

Here is my code:

-(NSURL *)movieURL
{
    NSBundle *bundle = [NSBundle mainBundle];
    if (bundle) 
    {  
     NSString *moviePath = [bundle pathForResource:vidName ofType:@"mov"];
     if (moviePath)
      mMovieURL = [NSURL fileURLWithPath:moviePath];

     if (vidName == @"Vid01")
      vidName = @"Vid02";
     else if (vidName == @"Vid02")
      vidName = @"Vid03";
    }

    return mMovieURL;
}    

- (void)onHitButton1 {
        mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
        mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
        [mMoviePlayer play];
    }
A: 

Are you really going to other videos?

I've noticed a bug in 3.0 that if you try to play a video twice from the same file you get some weird playback artifacts. The code you have above looks like it would only ever have a URL pointing to a single file.

The workaround I had was to rename my file, but that means you have to copy it to the documents directory in order to change the name... perhaps you could see if the iPhone file system would support a symbolic or hard link back to the original file in the resource directory so you could feed the movie player different names.

I filed a RADAR on this issue, you should too. Just put together a super simple example showing what is not working.

Kendall Helmstetter Gelner
I am changing the URL inside movieURL. I change it to Vid01, Vid02, Vid03, etc. The sound is correct for the different videos, it is just that the screen is black when it plays.
Matt
+1  A: 

I figured it out. I needed to release the MediaPlayer before calling the second video.

Code example:

- (void)onHitButton1 {
    [mMoviePlayer release];
    mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
    mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
    [mMoviePlayer play];
}
Matt
+1  A: 

You should also note that the playback issues may only occur in the simulator, not on the phone (such as subsequent playback 'flashing' or not displaying an image).

joshpaul
I noticed that as well. Thanks!
Matt
A: 

hi, please tell me how to play video files from ipod library,or is there any possibility to search and play video or audio files stored anywhere in iphone . ? plz give me example code or url to study about it ..

Thanks.

http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AudioandVideoTechnologies/AudioandVideoTechnologies.html Look through their developer stuff, it's amazing and very well written.
Sneakyness
A: 

Shouldn't vidname be changing before the resource path instead of after?

Either way, it would be better to write it to accept an NSString which you could pass the filename to. You know like

-(NSURL *)movieURLForVideo: (NSString) videoFileName

? Also I didn't like your lack of {}'s. This makes it much easier to read/debug for me. It's a good habit to have in general. Taking the extra time to write things as neat as you can will save you headaches later.

-(NSURL *)movieURL
{
    NSBundle *bundle = [NSBundle mainBundle];
    if (bundle) 
    {     
        if (vidName == @"Vid01")
        {
            vidName = @"Vid02";
        }
        else if (vidName == @"Vid02")
        {
            vidName = @"Vid03";
        }
        else if (vidName == @"Vid03")
        {
            vidName = @"Vid01";
        }
    NSString *moviePath = [bundle pathForResource:vidName ofType:@"mov"];
        if (moviePath)
        {
            mMovieURL = [NSURL fileURLWithPath:moviePath];
        }
    }
    return mMovieURL;
}

Is vidName being set to Vid01 somewhere? I didn't see it so I have to ask. This is just confusing in general. I've been writing for OS X, so I'm not sure if there are any differences, but I use this when I'm loading maps in my current project:

    NSString* mapPath = [[NSBundle mainBundle] pathForResource:mapFileName ofType:mapFileType];
    NSURL* mapURL = [NSURL fileURLWithPath: mapPath];
    currentMap_ = [[Map alloc] initWithContentsOfURL: mapURL];

Just seems like you're writing way more than you need to be.

Sneakyness