Audio streaming with QTKit

Discussion in 'Mac Programming' started by fernandovalente, Jan 9, 2010.

  1. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #1
    Hi, I would like to stream audio with QTKit, but I couldn't find any guide about it. Anyone knows how to do it?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I would imagine it's not any different than normal use. Just give your QTMovie an NSURL, and add your class as an observer of QTMovieLoadStateDidChangeNotification notifications.
     
  3. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #3
    I tried

    QTMovie *myMovie = [[QTMovie alloc] initWithURL:[NSURL URLWithString:mad:"http://87.98.222.14:7030"];
    [myMovie play];

    But that doesn't work :(
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    First off, QTMovie doesn't have an initWithURL: method, it's initWithURL:error: so that shouldn't compile.

    Second, you're not observing the load state, which is required to play it properly and ensure there's no error.

    Third, since you appear to be on Snow Leopard and you're only playing back the file, you should use QTMovieOpenForPlaybackAttribute to enable QuickTime X playback.

    Here's the way to do it, which also shows you the error if there is one

    Code:
    
    - (void)play
    {
    	NSError *error = nil;
    	NSURL *url = [NSURL URLWithString:@"http://87.98.222.14:7030"];
    	NSDictionary *movieAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
    								url, QTMovieURLAttribute,
    								[NSNumber numberWithBool:YES], QTMovieOpenForPlaybackAttribute,
    								nil];
    	movie = [[QTMovie alloc] initWithAttributes:movieAttrs error:&error];
    	if (!movie || error)
    		NSLog(@"Couldn't init movie: %@", [error localizedDescription]);
    	else
    		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:QTMovieLoadStateDidChangeNotification object:movie];
    
    }
    
    - (void)loadStateDidChange:(NSNotification *)notif
    {
    	QTMovieLoadState state = [[movie attributeForKey:QTMovieLoadStateAttribute] integerValue];
    	if (state == QTMovieLoadStateError)
    		NSLog(@"error: %@", [[movie attributeForKey:QTMovieLoadStateErrorAttribute] localizedDescription]);
    	else if (state >= QTMovieLoadStatePlayable)
    		[movie play];
    }
    Side note, debugging load state info can be rather annoying since there are several values for the state. I added a handy function to CocoaDev StringFromQTMovieLoadState() to aid in that.
     
  5. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
  6. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #6
    Is there anyway to check if the stream stopped for any reason like connection issues?
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    Possibly, but I'm not aware of anything. You could play around with the various notifications that QT sends.
     

Share This Page