このメソッドは、すべての可能性をサポートしています。
- ユーザーによって画面がロックされました。
- リスト項目
- ホームボタンが押されました。
iOS を実行している AVPlayer のインスタンスがある限り、デバイスの自動ロックが防止されます。
最初に、UIBackgroundModes 配列に audio 要素を追加して、Info.plist ファイルからオーディオ バックグラウンドをサポートするようにアプリケーションを構成する必要があります。
次に、AppDelegate.m を
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:
これらの方法
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
および#import < AVFoundation/AVFoundation.h >
次に、AVPlayer を制御するビュー コントローラーで
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
と
- (void)viewWillDisappear:(BOOL)animated
{
[mPlayer pause];
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
次に、
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if([mPlayer rate] == 0){
[mPlayer play];
} else {
[mPlayer pause];
}
break;
case UIEventSubtypeRemoteControlPlay:
[mPlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[mPlayer pause];
break;
default:
break;
}
}
ユーザーがホームボタンを押した場合に再生を再開するには、別のトリックが必要です (この場合、再生はフェードアウトで中断されます)。
ビデオの再生を制御する場合 (私は再生方法を持っています) セット
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
タイマーを起動して再生を再開する対応するメソッドが呼び出されます。
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
[mPlayer performSelector:@selector(play) withObject:nil afterDelay:0.01];
}
Backgorundでビデオを再生するのはうまくいきます。ありがとうございます。