AVAudioPlayerNode - Get Player State? - swift

In an iOS-project I am using the AVAudioPlayerNode in conjunction with the AVAudioEngine and an AVAudioUnitTimePitch. Everything works peachy. However, I was wondering if there is a way to figure out what the current player's state (e.g. isPlaying, isPaused) or at least the playback position is.
While AVAudioPlayer at least allows you to get the currentTime-parameter, I could not yet figure out how to get that information with AVAudioPlayerNode. I tried playing around with the nodeTimeForPlayerTime and playerTimeForNodeTime methods described in the swift documentation but I couldn't make any progress.
Any help would be highly appreciated.

Since the AVAudioPlayerNode is designed as an audio stream, it doesn't necessarily keep track of the time within a particular file. However, the AVAudioPlayerNode does keep a running time of how long its been playing all audio. This timer doesn't reset with each file, in order to change it, you must explicitly tell it where you want to start counting from.
So to find the current time the player has been playing you must do the following:
player.lastRenderTime.sampleTime / file.fileFormat.sampleRate
Now in order to get the timer to reset after each file, you must explicitly reset the players current time. To do this use the player.playAtTime: function.
If you would like an example, check one out here: https://github.com/danielmj/AEAudioPlayer

Related

NStimer and Dispatchqueue starts a little late when using the airpot

My app provides voice to AVPlayer and gives a little term using Timer in the middle of playback so that users can read along.
It works well on simulators and real devices.
However, when you pair the air pot, the timer starts about 0.2 seconds late.
Why does this happen and how can I fix it?
I used 'Timer.scheduler' and 'Dispatchqueue.Main.asyncAfter', but both had the same symptoms.
I gather that you are starting playback and simultaneously starting a timer, but when you do this with Bluetooth earphones, that they don’t seem in sync. If that’s the case, I might suggest not using your own timer, but rather let the AVPlayer tell you where it is.
For example, you might use addPeriodicTimeObserver(forInterval:queue:using:). The AVPlayer tell you when it got to a particular point in the playback.

Is there a way of changing playback position using AudioKit / AKPlayer?

Been scratching my head with this one, to no avail.
Am just trying to find out how to instantly change the current playback position when using the AKAudioPlayer provided by AudioKit.
player.playhead is read-only, so cant be changed.
changing player.startTime whilst the player is already playing seems to change the playback position in terms of the reported .playhead position but the actual audio being played does not change position - am I missing something here?
Obviously I can stop audio and restart at new position but a several second expensive CPU gap is not desirable for a a simple mp3 / wav file player!
Any ideas?
We've added a new player called AKPlayer which will also allow scanning through the file. The player will stream from disk by default. This player is designed to fix some of the shortcomings in AKAudioPlayer.
OK Aurelius pointed me in the right direction of a new class introduced a couple weeks ago called AKClipPlayer
Like the standard Apple AV class you can now set the .currentTime property (in seconds) (the AKAudioPlayer class did not allow this) - it seems to stop playback doing this though so don't forget to .play() your node right after - there is no glitch or pause, is seamless on an old iPhone6S

iphone html 5 video - how to start from different time

What is the correct way to begin playback of a video from a specific time?
Currently, the approach we use is to check at an interval whether it's possible to seek via currentTime and then seek. The problem with this is, when the video fullscreen view pops up, it begins playback from the beginning for up to a second before seeking.
I've tried events such as onloadmetadata and canplay, but those seem to happen too early.
Added information:
It seems the very best I can do is to set a timer that tries to set currentTime repeatedly as soon as play() is called, however, this is not immediate enough. The video loads from the beginning, and after about a second, depending on the device, jumps. This is a problem for me as it provides an unsatisfactory experience to the user.
It seems like there can be no solution which does better, but I'm trying to see if there is either:
a) something clever/undocumented which I have missed which allows you to either seek before loading or otherwise indicate that the video needs to start not from 00:00 but from an arbitrary point
b) something clever which allows you to hide the video while it's playing and not display it until it has seeked (So you would see a longer delay on the phone before the fullscreen video window pops up, but it would start immediately where I need it to instead of seeking)
do something like this;
var video = document.getElementsById("video");
video.currentTime = starttimeoffset;
more Information can be found on this page dedicated to video time offset howtos
For desktop browser Chrome/Safari, you can append #t=starttimeoffsetinseconds to your video src url to make it start from certain position.
For iOS device, the best we can do is to listen for the timeupdated event, and do the seek in there. I guess this is the same as your original approach of using a timer.
-R

set seek time in AVQueuePlayer in iphone

I am developing an application in that I want to play two video at the same time, That is not possible using MPMoviePlayer. So I have used AVQueuePlayer To play video. As I am success to play video but the problem is in jumping to particular time. For that we have method call seekToTime and we need to variable of the CMTime datatype.
I am able to jump at time in 1,2,3 seconds etc, My problem that I want to jump at Time 1.2, 1.3 , 1.4 second etc. but I am not able to move the video at that time.
Can any one know the solution of this problem than please help me to solve the problem.
The seekToTime: method, however, is tuned for performance rather than precision. If you need to move the playhead precisely, instead you use seekToTime:toleranceBefore:toleranceAfter Reference
toleranceBefore: The accuracy of the time before to which you would like to move the playback cursor.
[time-beforeTolerance]
toleranceAfter: The accuracy of the time after to which you would like to move the playback cursor.
[time+afterTolerance]
Lets say both parameters represents the margin of in-accuracy.
I have seen other Question on SO using self.player.currentItem.asset.duration to get the duration.
Good Luck.

MPMovie player how to get the amount of time played?

I was trying to go through the iPhone's sample code for mediaplayer.
I want to be able to capture the amount of time the media player has played the video. The duration at which the media player has stopped. Is there a method or property that will tell me the duration of play of the media??
Unfortunately the current API for MPMoviePlayerController allows basically no control. You can tell it to play and stop... otherwise where's a delegate method so you can be notified when the movie finishes playing and that's it, there's no additional controls. (a real bummer)
However, while we cant discuss the new 3.2 SDK yet, I'll give you a tip and say go look at the documentation of MPMoviePlayer in 3.2 and I think you'll be happy.
http://developer.apple.com/iphone/prerelease/library/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
moviePlayer.currentPlaybackTime
It's not possible to do KVO on it but you could do like me and create an scheduledTimer which updates every second to check what the current playbacktime is and update your graphics accordingly :)
Yes, You can use the property "duration" defined by MPMediaPlayerController. Plese try it out and check the output. U can refer the here duration property