2012年7月20日星期五

Developing Audio Work based on iOS 4.0

    To start with some conceptions about audio framework in iOS, I find Matt has done a good job to get some common thoughts about its history since iOS2.0 to iOS4.0 in this page.
    Then I write a DEMO using MPMoviePlayerController, it can do background playing and remote control as well, while it will be paused by the system when the App get into background. I fix this issue by using some flags, to determine when system pause it, after get into background it begins to play again. But as you can see you will find out that a short time of volume reducing and music paused. So I have a try of AVPlayer and make the MPMoviePlayerController as a backup solution.
     AVPlayer is a much more complicated API than MPMoviePlayerController. While MPMoviePlayerController provides status callbacks, I can only monitor AVPlayer's status with KVO. Some status I should monitor is an AVPlayerItem. I learned mostly from Apple's StitchedStreamPlayer Sample Code. It goes will when only one item is played and managed as well as background play and remote control. The best part is that it would not have the issue MPMoviePlayerController gets.
     Problems come when facing more then one item to play and manage. The App crashes in a concurrent thread which is from a NSNotificationCenter. I track and debugged the code many hours and finally make sure it's a KVO related issue. The Origin code of Apple's Sample first add item's observers and then make replaceCurrentItemWithPlayerItem, this resulting a crash when a notification for the previous item calls after that item is released. I changed the order to first make replaceCurrentItemWithPlayerItem and then add item's observers and fix the crash bug.
     The whole Project is now available in Github here(MyMusicAVPlayer).


PS:
If you want to make your App as a background playing one, see Apple's this page Technical Q&A 1668.
"You must declare that your application supports background audio execution, and assign an appropriate category to your audio session."
When you use MPMoviePlayerController, you should first "assign an appropriate category to your audio session" and then assign MPMoviePlayerController's useApplicationAudioSession to NO.


If you're making background vedio playing Matt has a good Note about what he has gone through in his App, you can check the page here “Background audio through an iOS movie player.


And as iOS 4.0 has many limits, an ideal method to get the duration of an AVPlayerItem in iOS 4.0 is:

[[playItem asset] duration], [playItem duration] is availble since 4.3.

没有评论:

发表评论