views:

67

answers:

1

Hi,

I need to determine what file type a file is and then perform a certain action for it. this seems to work fine for some types, however all media types such as videos and sound files get mixed up. I determine the file type by doing this:

   BOOL matchedMP3 = ([[rowValue pathExtension] isEqualToString:@"mp3"]);

   if (matchedMP3 == YES)
   {
    NSLog(@"Matched MP3");
            }

I do this for various file types and just define an "else" for all the others. Here's the problem though. The iPhone calls them both. Here's what the log reveals:

2010-05-11 18:51:12.421 Test [5113:207] Matched MP3

2010-05-11 18:51:12.449 Test [5113:207] Matched ELSE

I've never seen anything like this before.

This is my "matchedMP3" function:

  BOOL matchedMP3 = ([[rowValue pathExtension] isEqualToString:@"mp3"]);
   if (matchedMP3 == YES)
   {
    NSLog(@"Matched MP3");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *manager = [NSFileManager defaultManager];
    self.directoryContent = [manager directoryContentsAtPath:documentsDirectory];

    NSString *errorMessage = [documentsDirectory stringByAppendingString:@"/"];

    NSString *urlAddress = [errorMessage stringByAppendingString:rowValue];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:urlAddress]];

    moviePlayer.movieControlMode = MPMovieControlModeDefault;

    moviePlayer.backgroundColor = [UIColor blackColor];

    [moviePlayer play];
   }

and here's the else statement:

   else {
    NSLog(@"Matched ELSE");
    [[NSUserDefaults standardUserDefaults] setObject:rowValue forKey:@"rowValue"];

    NSString*rowValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"rowValue"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *manager = [NSFileManager defaultManager];
    self.directoryContent = [manager directoryContentsAtPath:documentsDirectory];

    NSString *errorMessage = [documentsDirectory stringByAppendingString:@"/"];

    NSString *urlAddress = [errorMessage stringByAppendingString:rowValue];

    webViewHeader.prompt = rowValue;

    [documentViewer setDelegate:self];

    NSString *encodedString = [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //Create a URL object.
    NSURL *url = [NSURL URLWithString:encodedString];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [documentViewer loadRequest:requestObj];

    [navigationController pushViewController:webView animated:YES];

   }

I can't see a reason why it wouldn't work. What happens is that both the webview and the MediaPlayer toggle their own player, so they overlap and play their sound/video a few secs apart from each other.

Any help would be appreciated & thank for you taking the time to read through my code.

+4  A: 

Didn't you mistakenly do something like

  if(conditionA)
  {
  ...
  }
  if(conditionB)
  {
  ...
  }
  else
  {
  ....
  }

In this case, if conditionA is satisfied but conditionB is not, the else clause is executed, because the first if clause finishes at the second if. You would want

  if(conditionA)
  {
  ...
  }
  else if(conditionB)
  {
  ...
  }
  else
  {
  ....
  }

instead. Beware the else if.

By the way, you don't have to and shouldn't compare a BOOL against YES.

 if(matchedMP3)

is enough. Remember, if accepts a boolean expression inside. A boolean value is a boolean expression!

Yuji
You are right, how could i have missed that? Thanks very much for helping me out :)
David Schiefer
How could you have missed that? It's a common mistake. We've all done it.
JeremyP