Audio playback issue in react-player for iOS after changing nextjs version from 8 to 10 - iphone

In my Nextjs (v.10) project react-player is used to play mp3 files. I recently updated the project:
"next": "10",
"react": "^17.0.2",
"react-player": "^1.6.6",
In my component I am using react-player like this:
<ReactPlayer
id='soundPlayer'
height='1px'
width='1px'
ref={this.ref}
playing={playing}
volume={1}
url={file}
onPlay={this.onPlay}
onPause={this.onPause}
onProgress={this.onProgress}
onDuration={this.onDuration}
playsinline={true}
file='forceAudio'/>
The user can control playback / pause, and if the file has been played completely, play it again. This works on all devices except some (I think, maybe all) iPhones.
This is what happens: the user presses play, the file is played and the onPause callback does not fire, which must be called when the file has been read in its entirety so that the user can listen to the audio again. Previously, this worked, although according to the documentation, as I understand it, if the file is played completely, onEnded should be called. What am I doing wrong and most importantly why the problem is only for iPhones?
I would be very grateful for any help.

Related

Using Ti.Media.VideoPlayer video does not loop on iPhone 4

Having a weird problem with my app.
Trying to play a video in a loop (see the code below). VideoPlayer repeatMode is set to Ti.Media.VIDEO_REPEAT_MODE_ONE.
Works in simulator, works on iPhone 4s, works on iPad Air... does not work on iPhone 4 (all of them running iOS 7.1.1 apart from simulator - 7.1).
Exact symptoms: after one play, video should restart from beginning, but it stops. If controls are available, I can press play to start playing it again, then after reaching the end it stops again.
I could swear that it worked on iPhone 4 before the 7.1 upgrade, but somehow it stopped.
Anyone could replicate this issue? Or have any idea?
Sys info:
Titanium SDK: Tested with 3.2.3.GA; 3.2.2.GA; 3.2.1.GA; 3.2.0.GA
Platform & version: iOS 7.1.1 (11D201)
Device: works on iPhone 4s, iPad Air, simulator; fails on iPhone 4
Host Operating System: OSX 10.9.2
Titanium Studio: 3.2.3.201404181442
Xcode: 5.1.1 (5B1008)
Alloy: 1.3.1
Code for basic Alloy project to replicate:
index.js
var videoPlayer = Titanium.Media.createVideoPlayer({
autoplay : true,
fullscreen : false,
mediaControlStyle : Titanium.Media.VIDEO_CONTROL_DEFAULT,
repeatMode: Ti.Media.VIDEO_REPEAT_MODE_ONE,
scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT
});
//Video is 12s long so it is easy to notice that it does not loop.
videoPlayer.url = "video/candles.mp4";
$.index.add(videoPlayer);
$.index.open();
index.xml
<Alloy>
<Window class="container">
</Window>
</Alloy>
I would appreciate any suggestions.
I have done some research and played around with the Titanium.Media.VideoPlayer and managed to work around the issue. Below you can find the solution in case anyone else encounter this problem.
First of all, some info on the problem.
I have found some forum boards where developers were having very similar problem with MPMoviePlayerController on iPad 1st gen. Looped video was less or more randomly freezing on loop. So I think there is a problem with the Apple video player on some devices, and not necessary with Titanium implementation.
My first approach was to try replay video on complete event that is fired when video stops playing, instead of using repeatMode.
So far so good. It worked on the first time, yay! But the happiness was rather short. During an hour of test of videoPlayer.play() on event complete, the video sometimes was repeating 10-30 times fine and then suddenly was freezing. What gives? Turns out the video player had some sort of a hiccup.
Normal sequence of events was as follows:
video ends, playbackstate changes to 2,
complete event is fired
I restart video on complete
playbackstate changes to 1, video is playing until the end.
For a mysterious reason once in a while this sequence got additional something happening:
video reaches the end - playbackstate changes to 2,
complete event is fired.
video gets restarted on complete
playbackstate changes to 1, video starts playing
suddenly playbackstate changes to 1, video stops, without firing complete event.
Solution
So, knowing all this, I have set up listeners to listen to playbackstate instead of complete and when I hear playbackstate: 1 then I restart video and tadaaa! Everything works!.
Caveat: not 'everything' unfortunately. I am using custom video controls, so I have lost pause, and stop ability (every time video playback state changes to 'stopped' I restart it forcefully). To make it work I have to remember to remove listener every time I press 'pause/stop' buttons and then reattach it afterwards.
Hope that helps someone.

Using a webview to play an mp3- is it possible to keep it playing even when the screen is locked?

I have a portion of my app that simply launches a webview linked to an mp3 file- it opens the file and plays the audio. What I'd like to do is allow that audio to continue playing even if the user locks their iPhone. (As a side note, is there any way to replace the default Quicktime logo that appears in the player? Not that big a deal, just thought I'd ask =)
A third question- I also do the same thing (webview) with an .m3u linked file to stream live audio throughout the week- is it also possible to keep it playing when the iPhone is locked?
Thanks!!
Ben
Quick Edit: The same URLs work great in IOS Safari (locking and unlocking doesn't affect the audio. Just not in my app's webviews.
I did find my answer though, in case anyone comes here looking. Easiest thing in the world too- just add a row to the plist file labeled "Required Background Modes" and under that set item 0's value to "App plays audio". Voila!
Since the audio is in a UIWebView, I don't believe that you'd be able to play it in the background.
EDIT: I was wrong, adding "App Plays Audio" to the UIBackgroundModes will make it work even when audio is in a UIWebView.
Have you thought about just streaming the .mp3 file yourself? Should be easy enough and you'd be able to setup background streaming. To get you started, I'd check out Matt Gallagher's AudioStreamer class which easily allows you to stream .mp3 files and other audio files from the net: https://github.com/mattgallagher/AudioStreamer

Trying to play an audio file

I'm trying to play an audio file in a cocos2d application. Here is the line which tries to play the sound:
[[SimpleAudioEngine sharedEngine] playEffect:#"pig_squeal.wav"];
If I put a log near this line, the log appears, and I can play the sound with iTunes. But when the sound should be played, there is a message displayed:
AudioStreamBasicDescription: 2 ch, 44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
What's the problem?
This was discussed in comments but I've amalgamated all the possibilities of why it may not work here.:
That's not an error message, just some information.
Is the sound definitely in your library and a part of the target?
You haven't changed the volume of the SimpleAudioEngine or the volume of your device isn't all the way down?
Click the sound in your library. Press Option+Command+1 to bring up the file editor. Scroll down to "Target Membership" and ensure the sound is checked for your target.
Try a different sound effect too? Try and narrow the problem down to "is it SimpleAudioEngine".
Also try and playBackgroundMusic for a sound (This was the solution in this case)
And try an mp3
There isn't a problem. It's a status message written to the log when you initialise the current OpenAL context using alcMakeContextCurrent. As far as I'm aware, you can't get rid of the message.
There is no problems with your code for the Playing of the Sound. Please check the Sound file has been added to the project also do check the format of the sound. It should play the Sound whenever you call the Play Effect. Please also try to PRELOAD the sound effect in the init method.
that message means the song was read correctly and should be playing. possible reasons you don't hear it?
volume turned down.
device malfunctioning
audio has silence in it
audio file is large and will take a long time to load.
sound has been redirect to come out of the headphones or the ear piece (even if not attached)
But the sound is loading and most likely playing.
If you are getting a crash while running in device from xcode pls try this(worked for me). Dissconnect the device from Xcode and run the app in the device. I dont know why it worked like that. But when I did this there was no crash.

iOS 5 Audio Alarms Don't Sound Without kAudioSessionProperty_OverrideCategoryMixWithOthers On

I have an audio app that is having some problems with the way iOS 5 has changed audio behaviors. When my app's audio is playing (AVAudioSessionCategoryPlayback), and a Clock.app alarm or timer is fired from the OS, the UIAlertView notification pops up, but without the audio alert. My application sound ducks fine to get out of the way of the audio alert, but the alarm app's audio alert does not sound.
Naturally, tons of support requests poured in over the iOS 5 change. I have solved this temporarily by setting kAudioSessionProperty_OverrideCategoryMixWithOthers which lets the alarm audio come through, but there are a few very undesirable side-effects when doing this:
Other app's audio can play with/over mine.
The remote control events are not routed to my app, but to iPod.app.
None of the above drawbacks are acceptable for my app's requirements. I have been hacking away at this for some time now but haven't been able to crack it. How can I setup my audio such that:
My app's audio still uses the AVAudioSessionCategoryPlayback category for background audio.
The Clock.app alarms still have their audio alerts make sound
The app still responds to remote control notifications
After writing this question I went to file a bug report on this. I created a small sample project that I thought would replicate the issue, but I could not replicate it! This caused me to dig in deep once again and try to figure out what was up here…
I fired up an iOS alarm, then I placed a break point in audioPlayerBeginInterruption: and traced through my code line by line in the debugger. I noticed that before my code ran (while I was paused in the debugger), the iOS 5 alarm was sounding! Luckily it still sounded even as I was stepping through my app, so I was able to figure out which pieces of code specifically caused it to stop sounding.
Part of my interruptionHandler is to (obviously) stop the internal audio of my app to let the interruption come through. I never thought to inspect this method before, but turns out the problem existed in there. My stop method would call prepareToPlay immediately after stopping to make resuming faster the next time.
[self.player stop];
[self.player prepareToPlay]; // <- iOS 5 alarm sound stopped here.
The docs state the prepareToPlay method
preloads buffers and acquires the audio hardware needed for playback, which minimizes the lag between calling the play method and the start of sound output.
Sounds reasonable, and this worked for lesser iOS versions. My hypothesis is that  must have made a change to the Clock.app alarm system such that the new alarm sounds use the hardware, whereas before it used the software. This is what I think is causing the iOS 5 alarms to be silent in some apps.
Removing the prepareToPlay lines caused the alarm to sound without using kAudioSessionProperty_OverrideCategoryMixWithOthers, thus solving all my issues laid out in this question.
TL;DR
Remove the prepareToPlay calls from your stop sound code logic. It will take a microsecond longer to start later, but will allow interruptions to sound.

Cannot play a recorded sound on device

I'm using the exact code from the iPhone Application Programming Guide Multimedia Support to use AVAudioRecorder to record a file to the disk and then AVAudioPlayer to load and play that file.
This is working fine in the simulator but is not working on the device. The file gets loaded (we can see the NSTimeInterval) but does not play (play returns false).
After it didn't work with the sample code from the website, we tried changing to a bunch of different codecs with no success. And of course, the sound is on.
Thanks a bunch.
SOLVED!
The problem was that after you set the AVAudioSession category to Record for recording, you have to set it to Play for playing. Sounds obvious now but you know how it is. When you use the Simulator it uses a different underlying implementation so this problem does not show up.