Determining AVPlayer bit rate - iphone

I am playing live audio stream using AVPlayer and AVPlayerItem and trying to determine the current bit rate of the stream. I searched in the net and found this help :
Determening MPMovieController bit-rate
Inspired by the above thread, I tried to compute it using the following code:
NSArray *logEvents=playerItem.accessLog.events;
AVPlayerItemAccessLogEvent *event = (AVPlayerItemAccessLogEvent *)[logEvents lastObject];
double bitRate=event.observedBitrate;
But the variable bitRate is always zero when checked inside a timer.
In fact [logEvents count] is also always zero.
Could you please tell me what is wrong with the technique ?
Thanks a lot.

In addition to Ooops's suggestion, it might be wise to register for the AVPlayerItemNewAccessLogEntryNotification notification to check for the bitrate.
Since the access log array isn't KVO compliant, using the notification would allow you to not use a timer to check for updates and you wouldn't have to worry about waiting for the player item to be ready. If the events are being fired too frequently, you could choose to ignore some.

Nothing's wrong with the method. Check if your playerItem is actually loaded. The accessLog is nil until the playerItem is 'access'ed. Try to get the accessLogs after your player becomes AVPlayerStatusReadyToPlay and you'll get the log.

Related

AVAudioPlayerNode - Get Player State?

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

How to get notified when AVAudioPlayer loops back to the beginning?

I'm playing a sound file in a loop and need to restart an animation with it every time the loop starts from the beginning.
I couldn't find a delegate method for it in the documentation. Is there a way to get notified when the player loops around to the beginning?
My approach was to use a timer which checks current play time every few milliseconds but this sounds like a horrible solution.
No, AFAIK your solution is the only one which works. The finishedPlaying method is not called whilst looping so sampling the position is the only technique.
I'm updating some inherited code to add looping and came to this conclusion under iOS8.
You can set up a delegate for the avaudioplayer instance. Then the delegate will be notified whenever the sound has successfully finished playing. Then you can probably restart the player to play again and restart your animation. Just off the top of my head. Look here for more information.

MPMoviePlayerController - handle phone call

I'm using MPMoviePlayerController to play a video.
When a call comes, i want to pause and when he completes, i want to continue the video.
I couldn't find relevant solution in my search.
Is this something possible?
Its possible to handle the interruptions using AVAudioSessionDelegate "beginInterruption" and "endInterruption". But when I am setting the session to play back, my movie player sound is not heard at all.
Does any one know the solution?
There is a callEventHandler in Core Telephony framework. I hope, You could find what you want here.
We've to set the session active after endInterruption.

How to get an AVCapture Timecode?

I'm working on a video capture app using the AVFoundation framework, based on the AVCam sample by Apple. I'd like to implement functionality to set a maximum video length, and have the capture automatically stops when this limit is reached (similar to UIImagePickerController.videoMaximumDuration).
I'm assuming I need to register for some notification as the capture is recording, and to check the timestamp in this callback. I looked through the AV Foundation Programming Guide and did a bit of Googling, and I can't find a way to retrieve the elapsed time of a AVCaptureSession, AVCaptureMovieFileOutput, or AVCaptureSomethingElse.
Any insight would help. Thanks!
You can set the maxRecordedDuration or maxRecordedFileSize. However, you need to make sure you handle the error correctly on the captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: delegate call to detect whether the recording stopped due to an error or due to reaching max duration/file size.
check the error code like:
if (([error code] == AVErrorMaximumDurationReached)) {
[delegate captureSessionMaxDurationReached];
}

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