AVPlayer + loop + background - loops

I need to:
play a sound
loop
continue to play in background
All works fine with AVAudioPlayer, but now I have to use AVPlayer to play also music songs from the iPod library. But there is a big problem: AVPlayer has no numberOfLoops property like in AVAudioPlayer.
Ok, you can register a notification to the NSNotificationCenter for AVPlayerItemDidPlayToEndTimeNotification (or you can also use the addBoundaryTimeObserverForTimes method with specified times equal to the song duration) to be notified when a song has played to the end.
But this works only if the app is in the foreground, in the background the song continues to play, but stops at the end.
The notification is received also in the background (checked with a NSLog), but the selector's code:
AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero];
...has no effect, the song doesn't loop.
Any suggestions?

The problem you have is the second the player does a seektotime your app looks like it's done playing audio, and so it's thrown on the bonfire. :)
I spent days creating this workaround:
Add "App plays audio" to "Required background mode" in info.plist
Create another AVplayer that plays a one second mp3 file at 0.00000001f rate (silentMp3Player.rate=0.0000001f) which starts when your app enters background and stops when it re-enters foreground.

Try to add this code:
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

Related

How to get duration of Youtube video without playing the video in Reactjs

I am a newbie in ReactJS and I badly need some help.
So I have a video catalog that only shows the thumbnails of the videos with label and overlay duration. Before I was using React-Player by Pete Cook but I don't want that my video player has share, like and watch later buttons so I decided to not use it and use video-player instead. I just use Image tag for showing thumbnail and I will just pass the Youtube link to the video player if the image is clicked.
Now my problem is that I am having a hard time in getting the video duration. When I was still using React-Player, I can get it after clicking play button (not the result that I want but at least I was able to get the duration). Any solution for this?
Your problem is you're providing a Youtube URL to the video-player library's Player Component which clearly doesn't support it as mentioned here.
I even tried it you'll get no duration value nor the video will be played.
However, if you changed your Youtube link to for example this one https://www.w3schools.com/html/movie.mp4
You'll find that everything is working perfectly and you can get the duration state from the Player's states without even playing the video.
const { player } = this.player.getState();
console.log(player.duration);
I added these two lines to the a method called changeSource mentioned here under Examples title.

How to play music in loop in libgdx?

I created the music in on create like this:
music_background = Gdx.audio.newMusic(Gdx.files.internal("background_music.mp3"));
music_background.setLooping(true);
the problem that its not playing in loop.
I also tried without the loop and instead registering for setOnCompletionListener but it also doesn't play. when I tried to reload the file like this:
music_background = Gdx.audio.newMusic(Gdx.files.internal("background_music.mp3"));
Inside the event it worked but only one time.
I think that the problem is that when its done playing the file dispose itself...
How can I play music in loop? what I'm doing wrong?
You are doing it correct, but MP3s are not good for looping, use OGG instead. MP3s will add a short silence at the start, OGG or WAV doesn't have this limitation.
Here is my code that works perfectly:
menuMusic = Gdx.audio.newMusic(Gdx.files.internal("data/sounds/music_menu.ogg");
menuMusic.setLooping(true);
menuMusic.play();
If you have all your files in MP3 just download Audacity, import you MP3s, edit away the blank audio and export as OGG.

Recording a video with audio using GPUImageMovieWriter without jerkiness at start and end of recording?

I am using GPUImage framework to record multiple videos one after other in close intervals with having various filters enabled in real time using GPUImageVideoCamera and GPUImageMovieWriter.
When I record the video, video starts with a jerk(freeze for half a seconds) and ends with a jerk also. I know the reason behind this are the statements in which I pass the movieWriter object to VideoCamera's audioEncodingtarget.
So In my case when I record multiple videos one after other(with different objects of GPUImageMovieWriter), the video preview view freezes at start and end of each recording.
If I remove the audio encoding target statement, conditions improves significantly but of course I don't get the audio.
Currently I am using a AVAudioRecorder while recording to save audio tracks but I believe this is not a ideal work around.
Is there any way to solve this problem.
-- I looked at the RosyWriter example by Apple, their app work almost similarly but smoothly at almost constant 30 fps. I tried to use the RosyWriter code(after removing the code that add purple effect) to save the required videos while showing GPUImageVideoCamera's filtered view to user but in vain. When applied unmodified rosywriter code just records two videos and rest video fails. I also tried to pass in the rosywriter code the capture session from GPUImageVideoCamera but only gets videos with black frames and no audio.
Please help on how can I can record GPUImage filtered videos with audio without this jerkiness. Thanks in advance
I faced the same issue and here is my workaround.
As you pointed out, this problem happened because setAudioEncodingTarget method internally calls addAudioInputsAndOutputs to set audio in/output to the capture session.
To avoid this issue, I created justSetAudioEncodingTarget method for VideoCamera as below,
(on GPUImageVideoCamera.m)
// just set
-(void)justSetAudioEncodingTarget:(GPUImageMovieWriter*)newValue {
if( newValue == nil ) {
return;
}
addedAudioInputsDueToEncodingTarget = YES;
[super setAudioEncodingTarget:newValue];
}
The following steps is my scenario and I checked out it smoothly worked.
Called VideoCamera's addAudioInputsAndOutputs after the VideoCamera was created.
This is not right before starting the recording. :)
Set MovieWriter to the VideoCamera by justSetAudioEncodingTarget that I made above.

loop sound actionscript Flash

i want to know if anyone can help me with actionscript in Flash.
i want to have some different buttons and play different sound loops when i click in the button, but every loop has to start when the other finished because of the music melody. can anyone help me?
Thank you
Teresa
By using SoundChannel class in AS3, you can get addEventListener - Event.SOUND_COMPLETE, which will notify you about the sound complete and then you can play the next sound
Well i am assuming you want to play the sound associated with the last button pressed and not play many sounds together.
So you have a global sound variable which you change on button press
var sound:Sound=new Sound(new URLRequest("music.mp3"));
var soundChannel:SoundChannel=new SoundChannel();
soundChannel=sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,loop_Sound);
function loop_Sound(e:Event){
soundChannel=sound.play();
//I know you don't need to add the listener again to the sound channel
//but this is something i just do
soundChannel.addEventListener(Event.SOUND_COMPLETE,loop_Sound);
}
//call this function when the button is pressed
//you could name the button name as the sound file's name
//and use e.currenttarget.name or something
function change_Sound(e:MouseEvent){
sound=new Sound(new URLRequest("music2.mp3"));
}
This would load the sound when the button is pressed but play it only when your current sound finishes playing and if the button is not pressed while it is playing it would loop the current sound .Best Of Luck!!

silverlight media play stop state

I am using SMF silverlight media player. I am using the following code to get me the current volume on player
this.item = function(){
alert(this.player.GetVolume());
}
which works fine but I also want the current status of the media. Whats the property for that. I didn't see that in API docs
thanks
I believe you want the PlayState property, which is of type MediaPluginState and can be one of these values:
Closed
Opening
Buffering
Playing
Paused
Stopped
Individualizing
AcquiringLicense
ClipPlaying

Resources