TheWidlarzGroup / react-native-video

A <Video /> component for react-native
http://thewidlarzgroup.github.io/react-native-video/
MIT License
7.15k stars 2.88k forks source link

Can't play in background on Ios 11 #797

Closed hathnv closed 6 years ago

hathnv commented 7 years ago

I use rn video to play an audio. playInBackground is not working on ios 11. It still works in lower versions.

fedebaier commented 6 years ago

I had the same problem and found the solution: you must enable the Audio Background Mode in your Xcode project as explained here: Enabling Background Audio

screen shot 2017-10-05 at 16 18 11

anshul-kai commented 6 years ago

No dice! I have the following in AppDelegate.m and is still doesn't work on iOS 11.

  // Set the Audio Session Category to Playback
  // https://developer.apple.com/library/content/qa/qa1668/_index.html
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *setCategoryError = nil;
  BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
  if (!success) { /* handle the error condition */ }
  NSError *activationError = nil;
  success = [audioSession setActive:YES error:&activationError];
  if (!success) { /* handle the error condition */ }
anshul-kai commented 6 years ago

Turns out you need to set ignoreSilentSwitch to ignore for this to work. Although, with that set, playInBackground={false} has no effect.

peterlazar1993 commented 6 years ago

@fedebaier Were you able to get this working?

robcalcroft commented 6 years ago

Also experiencing this issue on iOS 11. Have tested adding the ignoreSilentSwitch prop and enabling background audio in Xcode

aditya-sodhani commented 6 years ago

Same issue here. Tested all the above methods.

Justkant commented 6 years ago

Found this question, apparently this is a known issue in the iOS 11 simulator and this issue is now fixed in iOS 11.2 simulator, available since Xcode 9.2 (currently beta).

robcalcroft commented 6 years ago

@Justkant cheers for posting that will try it out in the newer sim. If I install onto my phone (11.1) will I still see the issue or is it specifically a simulator issue?

fedebaier commented 6 years ago

@peterlazar1993 Yes, in my case it was solved doing what I mention in my previous comment. I don't know why the downvotes, though. It worked for me and I just wanted to share it.

peterlazar1993 commented 6 years ago

@fedebaier Were you testing on simulator or device? Also what was the OS version.

@robcalcroft Please ping here if you get it working :)

robcalcroft commented 6 years ago

@peterlazar1993 Will do 😄

Justkant commented 6 years ago

@robcalcroft I cannot confirm that the issue is only on the simulator, you'll need to test that yourself.

rfw commented 6 years ago

+1

kay-is commented 6 years ago

@fedebaier worked for me on device, didn't try this on the simulator.

ArtisanalPickle commented 6 years ago

Simply works on iOS 10. Broken on iOS 11, despite proper settings.

It's worth noting that this will likely cause an app store rejection for declaring that your app uses background audio, without actually offering that feature. Reviewers will test on iOS 11, see you enable background but don't offer it as a feature and will think you're planning on slipping by something nefarious.

It happened to us :)

ArtisanalPickle commented 6 years ago

Actually regarding my last comment, I can't say with certainty that this is isolated to iOS 11. In fact, in the latest simulator (iOS 11.2) and a iOS 10.3 simulator and the device itself, I get identical behavior:

background audio for video files (in my case a streaming HLS .m3u8 file), functions perfectly when the user locks their phone, but NOT when hitting the home button.

My component looks like this:

<Video source={{uri: uri}}   // Can be a URL or a local file.
        onFullscreenPlayerDidDismiss={this.onDismiss}
        ref={(player) => {
          if(!this.state.playing && player) {
            player.presentFullscreenPlayer()
            this.setState({ playing: true })
          }
        }}                                      // Store reference
        rate={1.0}                              // 0 is paused, 1 is normal.
        volume={1.0}                            // 0 is muted, 1 is normal.
        muted={false}                           // Mutes the audio entirely.
        paused={false}                          // Pauses playback entirely.
        resizeMode="cover"                      // Fill the whole screen at aspect ratio.*
        repeat={false}                           // Repeat forever.
        playInBackground={true}                // Audio continues to play when app entering background.
        playWhenInactive={true}                // [iOS] Video continues to play when control or notification center are shown.
        ignoreSilentSwitch={"ignore"}           // [iOS] ignore | obey - When 'ignore', audio will still play with the iOS hard silent switch set to silent. When 'obey', audio will toggle with the switch. When not specified, will inherit audio settings as usual.
        progressUpdateInterval={PROGRESS_MILLISECONDS} // [iOS] Interval to fire onProgress (default to ~250ms)
        onError={this.onVideoError}               // Callback when video cannot be loaded
        onProgress={this.onProgress}
        onLoadStart={this.onStart}
        onEnd={this.stopPlaybackPing}
       /> 
frenkie commented 6 years ago

I had this background audio problem with the Xcode 9.1 simulator (while it worked on my device) and I can confirm that @Justkant's comment on using Xcode 9.2 and it's 10.0 simulator version and iOs 11.2, worked for me.

keyuanupup commented 6 years ago

i have a good news。i resolve this problem!

keyuanupup commented 6 years ago

in AppDelegate.m

import "AppDelegate.h"

import <AVFoundation/AVFoundation.h>

UIBackgroundTaskIdentifier _bgTaskId;

}

-(void)applicationWillResignActive:(UIApplication )application { //开启后台处理多媒体事件 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; AVAudioSession session=[AVAudioSession sharedInstance]; [session setActive:YES error:nil]; //后台播放 [session setCategory:AVAudioSessionCategoryPlayback error:nil]; //这样做,可以在按home键进入后台后 ,播放一段时间,几分钟吧。但是不能持续播放网络歌曲,若需要持续播放网络歌曲,还需要申请后台任务id,具体做法是: _bgTaskId=[AppDelegate backgroundPlayerID:_bgTaskId]; //其中的_bgTaskId是后台任务UIBackgroundTaskIdentifier _bgTaskId;在appdelegate.m中定义的全局变量 }

+(UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId { //设置并激活音频会话类别 AVAudioSession *session=[AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; [session setActive:YES error:nil]; //允许应用程序接收远程控制 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; //设置后台任务ID UIBackgroundTaskIdentifier newTaskId=UIBackgroundTaskInvalid; newTaskId=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid) { [[UIApplication sharedApplication] endBackgroundTask:backTaskId]; } return newTaskId; }

keyuanupup commented 6 years ago

this is work for me.so i share for you.I hope to help you。Contact me。email:307464314@qq.com。

keyuanupup commented 6 years ago

and i find in this 'react-native-video' README.md had explained ‘Play in background on iOS’

letminin commented 6 years ago

updating Xcode fix the issue indeed, thanx for ur share ! 👍

cobarx commented 6 years ago

This is working properly for me on Xcode 9.4 and both real & simulator devices running iOS 11.4 as long as I enable Background Audio and set ignoreSilentSwitch to "ignore". I've added this to the docs.

How are things looking for other people?

dkoo761 commented 5 years ago

This solution above from @keyuanupup also has the important side benefit of allowing background audio to function in a sequential multi-file situation (such as a playlist) where there is a slight gap in playback while swapping out the player's current audio file source for the next audio file's source. Without this solution, iOS suspends the app as soon the first file in the playlist finishes playing.

https://github.com/react-native-community/react-native-video/issues/797#issuecomment-374826812

anastely commented 4 years ago

Hello Guys, I don't know if my issue #2038 related or not but can you check it?

Nesh108 commented 1 year ago

I am having the same issue. This is the configuration:

      ignoreSilentSwitch={'ignore'}
      playInBackground={true}
      playWhenInactive={true}

And background modes are set (audio, fetch, processing).

Nesh108 commented 1 year ago

Did anyone here find a solution in the meantime? For me it's happening both on iOS and Android.