AVAudioPlayer playing thread

Discussion in 'iOS Development' started by jbonedev, May 24, 2009.

  1. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Hello gurus, looking for some help with AVAudioPlayer.

    We are working on a project where we have a two dimensional array of AVAudioPlayers. One dimension represents time, e.g. quarter notes. The other dimension represents the instruments which should play at that time. Some of these will be nil.

    We are using [NSThread sleepForTimeInterval:] to keep time to wait for the next notes to be played. I can provide an example in pseudo code if this is not clear. This is not an event handling thread, we create this ourselves.

    This works fine until some other threads need to do something, such as the user tries to scroll the table view. Then the sound becomes choppy, as in the timing gets off.

    Is there some better way to do this like some kind of scheduling or timer?

    We're hoping not to use the Audio Queue Service as this seems too low level for what we need, manually managing buffers and such. Also the samples we are playing are very dynamic and in different formats such as wav and mp3.
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I somewhat understand what you are saying. Purely because you mentioned "timer" I'll point you towards the NSTimer class.
  3. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thank you for the reply. I'll check out NSTimer. Though from the class reference I'm a bit discouraged about this part
    That could be just as bad as the current lag I'm seeing with NSThreed sleepForTimeInterval but I will definitely give this a shot!

    Thanks
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Need to multithread you app. Create an detach a new thread. In the second thread, you will need to se up another autorelease pool AND runloop if you are using timed events. In the AV thread, set up a timer that fires at whatever your bpm is. I would use a variable and have the timer fire every 60/BPM.

    That is your only solution to your problem. Also, I wouldn't use the instruments/time in a two dimensional array.

    Please though, whatever you do, don't make the main thread sleep like you are currently doing.
  5. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    AVAudioPlayer might be a little heavy for how many things you're doing.
  6. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I am not making the main thread sleep. I am creating a new thread using [NSThread alloc].
    But I have a question here about this. I need to scroll a table while this thread is working. Both scrollToRowAtIndexPath and setContentOffset do not seem to work from this separate thread. I can get them to work from the event handling thread for example in my implementation of didSelectRowAtIndexPath, but from my separate player thread the same code doesn't work. I do notice that once I handle some other UIBarButtonItem pushed event the table does scroll to the last location set in the other thread. Do I need to fire some event for this to take place form the other thread?

    EDIT okay I just discovered performSelectorOnMainThread: ...

    This is my fear, but the Audio Queue Service seems way too low level for this. Is there no happy medium?
  7. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    SystemSoundIDs are good for short sounds.
  8. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thanks so much, this is a huge help!

    Using this method I don't see any performance lag at all, so I'm wondering if I should even bother with moving to using NSTimers. If I'm not getting any lag using my thread sleep method, is there anything else the NSTimers buy me other than more code?
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    performSelectorOnMainThread performs the selector on the main thread. You might as well not even multithread the app.
  10. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I need to scroll the UITableView while I'm playing my sounds Like say you were reading sheet music, your eyes would follow the notes as they were played. How do you suggest I do this from the sound playing thread without using performSelectorOnMainThread?

Share This Page