• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: how to integrate Core Audio code into the run loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to integrate Core Audio code into the run loop


  • Subject: Re: how to integrate Core Audio code into the run loop
  • From: Jeff Moore <email@hidden>
  • Date: Sun, 07 Sep 2008 09:06:51 -0700

Basically, it sounds like you are trying to do too much work on the main thread. How about moving your networking code and audio playback code to it's own work thread? That will do two good things for you. First, your main thread (which is where AppKit does all it's work for the most part) will be free to do UI stuff. Secondly, you'll be taking much better advantage of the multiple CPU cores we have in our systems these days.

On Sep 7, 2008, at 8:04 AM, John Michael Zorko wrote:


... what i've tried hasn't worked :-(

Hello, all ...

I have what is likely a very easy question. I'm new to Core Audio, and i'm working with AudioFileStreamExample. I was quite happy to find this, and i've learned a lot about Core Audio by seeing it work, but the problem is that this example doesn't illustrate how to integrate into the run loop. Basically, my app plays the audio streamed from the server fine, but the user can't do anything else while it's going on.

What i've done is:

1. replace the example's main() function with my own, NSURLConnection-based code, like this:

- (void)playStream:(Song *)streamToPlay
{
	NSLog(@"playing mp3 stream %s", [streamToPlay.mp3 UTF8String]);

// initialize a mutex and condition so that we can block on buffers in use.

pthread_mutex_init(&myData.mutex, NULL);
pthread_cond_init(&myData.cond, NULL);

OSStatus err = AudioFileStreamOpen(&myData, MyPropertyListenerProc, MyPacketsProc,
kAudioFileAAC_ADTSType, &myData.audioFileStream);
if (err)
{
NSLog(@"AudioFileOStreamOpen error %d", err);
}

NSString *url = @"http://he3.magnatune.com/all/";;
url = [url stringByAppendingString:streamToPlay.mp3];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection)
{
receivedData = [[NSMutableData data] retain];
}
else
{
NSLog(@"can't connect to server");
}
}


... and inside my NSURLConnection's received-data method, I tried to integrate into the run loop thusly:

- (void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data
{
unsigned int length = [data length];

NSLog(@"recv'd %d bytes", length);

OSStatus status = AudioFileStreamParseBytes(myData.audioFileStream, length, [data bytes], 0);


	CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, false);

   [receivedData appendData:data];
}

... but alas, it doesn't work -- my app is still unresponsive while the stream is playing. I'm also not so sure that didReceiveData is the best place to do this, but I don't know of a better place yet. Any pointers?



--

Jeff Moore
Core Audio
Apple


_______________________________________________ Do not post admin requests to the list. They will be ignored. Coreaudio-api mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >how to integrate Core Audio code into the run loop (From: John Michael Zorko <email@hidden>)

  • Prev by Date: how to integrate Core Audio code into the run loop
  • Next by Date: re: AU Preset Data
  • Previous by thread: how to integrate Core Audio code into the run loop
  • Next by thread: Custom parameter display transformations
  • Index(es):
    • Date
    • Thread