Created
July 11, 2014 06:00
-
-
Save t-oginogin/7603642c2325906a0d97 to your computer and use it in GitHub Desktop.
iOS イヤホンジャックに機器を接続した状態で、本体の内蔵スピーカーから音を鳴らす ref: http://qiita.com/t_oginogin/items/c0034a3d830d28ec18e5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| #import <AVFoundation/AVFoundation.h> | |
| @interface SampleViewController : UIViewController <AVAudioPlayerDelegate> | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "SampleViewController.h" | |
| #import <AVFoundation/AVAudioSession.h> | |
| #import <AudioToolbox/AudioServices.h> | |
| @interface SampleViewController () | |
| { | |
| AVAudioPlayer *player; | |
| } | |
| @end | |
| @implementation SampleViewController | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view. | |
| // overrideOutputAudioPort:でAVAudioSessionPortOverrideSpeakerを設定するための準備 | |
| [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; | |
| // AudioSessionを有効化 | |
| [[AVAudioSession sharedInstance] setActive:YES error:nil]; | |
| // 電話が鳴るなどAudioSessionの割り込み発生時の処理を登録 | |
| NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
| [center addObserver:self selector:@selector(sessionDidInterrupt:) name:AVAudioSessionInterruptionNotification object:nil]; | |
| // 鳴らす音の準備 | |
| NSString* path = path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"aiff"]; | |
| NSURL* url = [NSURL fileURLWithPath:path]; | |
| player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; | |
| player.delegate = self; | |
| } | |
| // AudioSessionの割り込み発生時の処理 | |
| - (void)sessionDidInterrupt:(NSNotification *)notification | |
| { | |
| switch ([notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue]) { | |
| case AVAudioSessionInterruptionTypeBegan: | |
| [player stop]; | |
| break; | |
| case AVAudioSessionInterruptionTypeEnded: | |
| default: | |
| [[AVAudioSession sharedInstance] setActive:YES error:nil]; | |
| break; | |
| } | |
| } | |
| // サウンド再生が終わったときの処理 | |
| - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag | |
| { | |
| // 内蔵スピーカーへの強制出力を止める | |
| [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil]; | |
| } | |
| - (IBAction)sound:(id)sender { | |
| // 内蔵スピーカーへ強制出力 | |
| [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil]; | |
| [player play]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment