TheWidlarzGroup / react-native-video

A <Video /> component for react-native
https://docs.thewidlarzgroup.com/react-native-video/
MIT License
7.22k stars 2.9k forks source link

[BUG]: MixWithOthers iOS not work for first load #4027

Open krisnapy opened 4 months ago

krisnapy commented 4 months ago

Version

6.4.2

What platforms are you having the problem on?

iOS

System Version

iOS 17.5.1

On what device are you experiencing the issue?

Real device

Architecture

Old architecture

What happened?

External audio such as Spotify stops when the video has been played even if I set MixWithOthers to “mix”, but when the audio is played again, the audio does not stop. I thought it would stop when first load/play.

https://github.com/user-attachments/assets/9e3b0def-f1c3-4b23-bd13-c47bad1b04fa

Reproduction

-

Reproduction

Step to reproduce this bug are:

Component:

import React from 'react'
import Video, { IgnoreSilentSwitchType, MixWithOthersType } from 'react-native-video'

export function LandingScreen(): JSX.Element {
  return (
    <Video
      style={{ flex: 1 }}
      resizeMode="contain"
      source={{ uri: 'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4' }}
      ignoreSilentSwitch={IgnoreSilentSwitchType.IGNORE}
      mixWithOthers={MixWithOthersType.MIX}
      controls
    />
  )
}

AppDelegate:

#import "AppDelegate.h"
#import "RNBootSplash.h"
#import <Firebase.h>
#import <React/RCTLinkingManager.h>
#import "RNFBMessagingModule.h"

#import <React/RCTBundleURLProvider.h>
#import <AVFoundation/AVFoundation.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // RN Firebase
  [FIRApp configure];

  self.moduleName = @"factum";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

  self.initialProps = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

// RNBootSplash
- (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
                          moduleName:(NSString *)moduleName
                           initProps:(NSDictionary *)initProps {
  UIView *rootView = [super createRootViewWithBridge:bridge
                                          moduleName:moduleName
                                           initProps:initProps];

  [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView];

  return rootView;
}

// Deep Links
- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

// Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
  return [RCTLinkingManager
            application:application
            continueUserActivity:userActivity
            restorationHandler:restorationHandler
         ];
}

@end
moskalakamil commented 3 months ago

Hi @krisnapy, thanks for sharing issue! We'll try to fix it as fast as possible!

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 30 days with no activity. If there won't be any activity in the next 14 days, this issue will be closed automatically.

freeboub commented 2 months ago

Sorry for very long answer time. It looks like something easy to fix (1 line):

I think following patch should be enough to fix the issue:

diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift
index 345acbf0..4b002828 100644
--- a/ios/Video/RCTVideo.swift
+++ b/ios/Video/RCTVideo.swift
@@ -753,6 +753,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
     @objc
     func setMixWithOthers(_ mixWithOthers: String?) {
         _mixWithOthers = mixWithOthers
+        RCTPlayerOperations.configureAudio(ignoreSilentSwitch: _ignoreSilentSwitch, mixWithOthers: _mixWithOthers, audioOutput: _audioOutput)
         applyModifiers()
     }
freeboub commented 2 months ago

hum, that is not enough ... If we define only mixWithOther, there is no audio output at all ... @KrzysztofMoch It would be great to integrate this change as part of: https://github.com/TheWidlarzGroup/react-native-video/pull/3850/files I think you already fix the issue I see in your PR, but as I don't have real iphone yet, I cannot test properly ...

freeboub commented 2 months ago

I also wonder if configureAudio (or your new function) should be called inside applyModifiers ? but I am not sure ...