ビーとか、ピコンとかシャーとか。
音を再生するために、毎回、AVAudioPlayerを再生するためのシングルトンクラスを作っていました。AVAudioPlayerのplayをするだけだと多重再生ができないので、多重再生するためにAVAudioPlayerのインスタンスを保持する機能を持つという、それだけのやつです。いつかはやっておきたいという思ってました。
あと、音源は、Octave: A free library of UI sounds.を使っています。こちらも、便利クラスとして作っておきたかったのです。これで気軽に音を鳴らせるー。
中身の簡単な説明
podでインストールできます。本家のSpecsには登録していないので、githubからお願いします。メソッドはとりあえず、
// Play sound [[MZRSoundManager sharedInstance] playSoundWithSoundName:soundName ofType:nil]; // Stop sound [[MZRSoundManager sharedInstance] stopSoundWithSoundName:soundName ofType:nil]; // Stop all sounds [[MZRSoundManager sharedInstance] stopAllSounds];
こんな感じです。今後拡張していけたらいいな。録音とかも。Octaveの方もある程度ちゃんと作っていて、再生するためのファイル名はMZRSoundManager+OctaveSounds.hに定数で用意してます。
extern NSString *const MZRSoundManagerOctaveSoundsBeepAttention; extern NSString *const MZRSoundManagerOctaveSoundsBeepBop; extern NSString *const MZRSoundManagerOctaveSoundsBeepBrightpop; extern NSString *const MZRSoundManagerOctaveSoundsBeepGlitchy; extern NSString *const MZRSoundManagerOctaveSoundsBeepHightone; extern NSString *const MZRSoundManagerOctaveSoundsBeepHoldtone; extern NSString *const MZRSoundManagerOctaveSoundsBeepHorn; extern NSString *const MZRSoundManagerOctaveSoundsBeepMetallic; extern NSString *const MZRSoundManagerOctaveSoundsBeepOrgan; extern NSString *const MZRSoundManagerOctaveSoundsBeepPiano; extern NSString *const MZRSoundManagerOctaveSoundsBeepPlucked; extern NSString *const MZRSoundManagerOctaveSoundsBeepReedy; extern NSString *const MZRSoundManagerOctaveSoundsBeepRejected; extern NSString *const MZRSoundManagerOctaveSoundsBeepRoadblock; extern NSString *const MZRSoundManagerOctaveSoundsBeepScrapy; extern NSString *const MZRSoundManagerOctaveSoundsBeepScratchy; extern NSString *const MZRSoundManagerOctaveSoundsBeepSharpstring; extern NSString *const MZRSoundManagerOctaveSoundsBeepShinymetal; extern NSString *const MZRSoundManagerOctaveSoundsBeepTapped; . . .
こんな感じです。
Github
参考
- Octave: A free library of UI sounds.
- CocoaPods Guides – Specs and the Specs Repo
- 1ヶ月使って便利だと思った Sublime Text 2 のショートカット 12 選(Win、Mac両方) | Developers.IO
- CocoaPods podspec作成の逆引きいろいろ – Qiita
sublime textはほんとに便利で、今回の定数はFinderのファイルをコピー->sublimeで複数選択してちゃらちゃら。でサクッと出来てしまいました。podspec作成の逆引きはたまに見ます。
公開するか迷ったけど、もしかして少しでも反応が貰えればやる気出るなと思って公開しました。当分の間は公開することにしました(有料の音源とか追加したくなったら考えないと)。