audio does not play after pressing button in iphone safari - reactjs

I have a react component that has a button with a handler to play an audio.
const initializeAudio = () => {
let context = new AudioContext()
let analyser = context.createAnalyser()
let audioSrc = context.createMediaElementSource(audioRef.current)
audioSrc.connect(analyser)
analyser.connect(context.destination)
analyser.fftSize = 512
setAnalyser(analyser)
}
const play = () => {
if (!audioSet) {
initializeAudio()
setAudioSet(true)
audioRef.current.volume = 0.5
}
audioRef.current.play()
setPlaying(true)
interval = window.setInterval(turnToTime, 1000)
}
<audio
data-keepplaying
ref={audioRef}
id="audioElement"
src={audio}
/>
<WaveForm analyser={analyser} />
<button onClick={isPlaying ? pause : play} className="media_button">
<FontAwesomeIcon
className="fa-icon"
icon={isPlaying ? "pause" : "play"}
style={!isPlaying && { paddingLeft: "0.4rem" }}
></FontAwesomeIcon>
</button>
Everything works fine on firefox and chrome, but won't work on safari on iphone, any one know why?
I feel that my audio is created in response to a click which is what the new restriction needed, but why is safari not working?

From reading your code I would guess that the second line is throwing an error in Safari. Safari still has no official support for the Web Audio API and the AudioContext is only available as webkitAudioContext.
If initializing the context is your only concern using the following technique might work.
let context = new (window.AudioContext || window.webkitAudioContext)();
However I would not recommend using it as it treats Safari's implementation similar to the one in Firefox or Chrome even though they differ in many aspects. As I'm the author of standardized-audio-context using this package is of course my preferred way to handle inconsistent browser support for the Web Audio API. :-)
You can use it like this.
import { AudioContext } from 'standardized-audio-context';
let context = new AudioContext();
I hope this helps.

Related

React Native Foldable Devices

I'm working on a project where one of the requirements we were asked was to support foldable devices, unfortunately I can't find much content on the internet about their development in React Native.
Is there a library or something that React Native has to know if the device on which the user is using the application is a foldable device?
I'm very new to the subject and would like to know if anyone has come up with strategies to solve this "problem". For example, when the foldable device is open it shows the text "Open", if it is closed it shows "Closed".
const { width } = Dimensions.get("window")
const [bannerWidth, setBannerWidth] = useState<number>(width)
useEffect(() => {
const remove = Dimensions.addEventListener("change", status => {
console.log(status.window.width)
setBannerWidth(status.window.width)
})
return () => {
remove.remove()
}
}, [])
console.log(bannerWidth)
do this

window.confirm not working on iphone not working

I have a web page with following event:
const onDeleteBankData = async (bankData) => {
if (!window.confirm('Are you sure you wish to delete this item?')) {
return;
}
}
I tested it in Chrome and on an Android device and it works. However on iPhone this does not work. Is this expected behavior? Is it a browser thing, or something I need to setup in REACT?

Get Stream IO React Native UnFollowing option not available

I am using Get Stream Io react native in my project https://github.com/GetStream/react-native-activity-feed
I see there is a functionality in the library of following a particular user, whereas the opposite is not available i.e. how to unfollow a user.
Please point me in the right direction on how to achieve this in react native
Our library doesn't include the logic for following/unfollowing. It has a button that allows you to set it up yourself though. Which is the FollowButton you're talking about. You could do something like this:
<FollowButton
followed={async () => {
// check if you're following the user
const following = await your_timeline_feed.following({filter: ['user:user_42'] })
if (following) {
return true;
}
return false;
}} // renders the button as "following"
clicked={async () => {
// your logic for following/unfollowing
await your_timeline_feed.follow('user', 'user_42');
}}
/>
Read more here:
https://getstream.github.io/react-native-activity-feed/#!/FollowButton
https://getstream.io/docs/following/?language=js

Send message from service woker to react component in some of the browser which donot support BroadcastChannel

I am able to successfully get push notification from my server and update react component with this
self.addEventListener('push', event => {
const data = event.data.json();
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
image: './Flowster-icon-32.png'
})
);
const channel = new BroadcastChannel('sw-messages');
channel.postMessage(data);
});
But browser sucs IE, safari, donto support BroadcastChannel, even client.postMessage is not supported in these browser, how can i make it work in those browser. Thanks for the help in advance.
Since the BroadcastChannel doesn't support IE and safari browser, as a workaround, I suggest you could try to use the sysend.js in IE or safari browser.
Reference: How can I use the BroadcastChannel API or something similar in Safari 10+?

Form submit with Post method not working with target _blank in PWA

<form
action="some url"
method="POST"
target="_blank"
>
{_.map(somedata, (value, key: string) => (
<input name={key} key={key} type="hidden" value={value}/>
))}
</form>
I am working with a page which submits a form on click of some button, this posts some data and opens a page in a new tab. This works perfectly fine in chrome mobile android but does not work(the new url opens in the new tab but shows no data posted by the form) when i create a PWA shortcut using Add to Home Screen feature and submit the form from inside of it. Also the new tab opens inside PWA only instead of mobile android chrome.
I apologise that this relates to Jquery (it uses core JS at its heart) but I believe this is something similar to solve the form posting issue. It uses the JS FormData object which allows for files as well.
function formPost(form){
var formData = new FormData();
$(form).find('input,select,textarea').each(function(){
if ($(this).attr('type') == 'file'){
use = $(this).get(0).files[0];
} else {
use = $(this).val();
}
formData.append($(this).attr('name'),use);
})
var request = new XMLHttpRequest();
request.open('POST',$(form).attr('action'));
request.send(formData);
}
Its worth saying that for browsers this requires at least IE10 but I think at end of 2019 we shouldn't be worrying about that !! https://caniuse.com/#search=formdata (PWAs operate in "modern browsers").

Resources