Reputation: 85
I am trying to make my own audio player using ios CoreAudio
framework in swift
. It requires at least two threads. One thread decodes the audio file and the other thread gave hardware decoded data. Actually second thread that is passing to hardware was callback from OS so I don't care that I just have to pass required data when callback function invoked.
I used DispatchQueue
for decoding, actually it is working well. as follwed:
DispatchQueue.global(qos: .background).async {
while(endOfFile) {
// do decoding
let res = ExtAudioFileRead(input, &frameCount,&convertedData)
}
}
When I used DispatchQueue, CPU usage exceeded 100%
. The phone getting heated though I only played one mp3 music in my player. I don't know what was wrong.
DispatchQueue
?DispatchQueue
?Upvotes: 1
Views: 177
Reputation: 14427
It's not a good practice. Never use loops to wait for something, your some operation should notify when it's finished and use a completion handler.This pattern is usually referred to as busy waiting and it is a great way to discharge your device's battery quickly.
You should use Completion Handler to achieve this kind of stuff.
Completion handlers are callbacks that allow a client to perform some action when a framework method or function completes its task. Often the client uses a completion handler to free state or update the user interface. Several framework methods let you implement completion handlers as blocks (instead of, say, delegation methods or notification handlers).
Follow Apple documentation for more details.
Upvotes: 1