AVPlayer current time is not working - iphone

OK. AvPlayer is working great with streaming audio. In my app I have UISlider that shows current seconds of the playing song. Now I'm trying to make audio seek with UISlider.
Here is the code
-(IBAction)slide{
Float64 curSec = mySlider.value;
int32_t tScale = 600;
CMTime mySec = CMTimeMakeWithSeconds(curSec, tScale);
player.currentTime = mySec; <- error is here
NSLog(#"%f",mySlider.value);
}
The error is "Setter method is needed to assign to object using property assignment syntax"
In .h file I have AVPlayer *player; and #property(nonatomic, retain)AVPlayer *player. Also in .m I have #synthesize player;
So what is wrong? THANK YOU!

According to the doc, it seems to me that you have to use seekToTime: method instead of setting time directly to the currentTime property.

float second = 30.0;
CMTime time = CMTimeMake(second, 1);
[player seekToTime:time];

Related

Move audio player 5 second fast-rewind & fast-forward

I know this is a duplicate question but I didn't find out the exact answer. I am working with AVAudioPlayer. Now I have two buttons forward & rewind. When the user will tap the forward button the audio will move 5 second forward & when the user will tap the rewind button the audio will move 5 second rewind. How can i do this exactly? Thanks in advance for any help.
Put below methods for forward and rewind player time
- (IBAction)btnForwardClicked:(id)sender
{
int currentTime = [player currentTime];
[player setCurrentTime:currentTime+5];
}
- (IBAction)btnBackwardClicked:(id)sender
{
int currentTime = [player currentTime];
[player setCurrentTime:currentTime-5];
}
here player is avaudio player's object
AVAudioPlayer *player;
- (IBAction)btnForwardClicked:(id)sender
{
NSTimeInterval *time = [player currentTime];
time+=SKIP_TIME;
//for reverse time-=SKIP_TIME
//SKIP_TIME is time which is jumped i-e 5 seconds
[player setCurrentTime:time];
}

UISlider value assigining issue ios

I'm using a UISlider programmatically in a MPMoviePlayerController and set its value with the movie current playback time. This doesn't work properly in some cases, the value of the slider remains zero not changed with the movie's current playback time. Can anyone help me please?
My code is set to fire after each second. Both labels work properly but the UISlider value doesn't get updated and remains zero.
float playbackTime = player.currentPlaybackTime;
float duration = player.duration;
timeLabel.text = [NSString stringWithFormat:#"%.0f / ",playbackTime];
durationlbl.text=[NSString stringWithFormat: #"%.0f",`duration];`
progressSlider.value = playbackTime;
you should probably do this:
progressSlider.minimumValue = 0.0;
progressSlider.maximumValue = player.duration;
you should do this not all the times that you update the slider but only when you initialize the slider or when you start a new video
i resolved this issue by stop the video on close action of the player before play other video.
this issue occur because of previous video state in the player due to which on launching new video slider valur disturbed.

iphone MPMoviePlayerViewController : extract total duration

how can i get the video'a total time, before it plays the video in MPMoviePlayerViewController?
To get total duration of movie you can use :
1). Use AVPlayerItem class and AVFoundation and CoreMedia framework. (I have used UIImagePickerController for picking the movie)
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAsset.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
NSLog(#"duration: %.2f", seconds);
}
2). MPMoviePlayerController has a property duration .Refer Apple Doc
To get the total Duration in MPMoviePlayerViewController
MPMoviePlayerViewController *mp;
float seconds =mp.moviePlayer.duration;
Note: Above code give the total Duration of related Media in seconds
If you don't want to get the total time from MPMoviePlayerViewController's duration property (because that brings up the movie player UI), you could instead create an AVAsset object with your video file passed in via a file URL and then check the duration property on that.
This trick would only work on iOS 5 (which is where AVAsset's assetWithURL: came in with).
YOu can get using this method in swift
func getMediaDuration(url: NSURL!) -> Float64{
var asset : AVURLAsset = AVURLAsset.assetWithURL(url) as AVURLAsset
var duration : CMTime = asset.duration
return CMTimeGetSeconds(duration)
}

How can you play a sound of a specified duration using AVAudioPlayer on iOS?

I want to play a specified duration within a sound file on IOS. I found a method in AVAudioPlayer that seeks to the begining of the playing (playAtTime:) but i cannot find a direct way to specify an end time before the end of the sound file.
Is there is a way to achieve this?
If you don't need much precision and you want to stick with AVAudioPlayer, this is one option:
- (void)playAtTime:(NSTimeInterval)time withDuration:(NSTimeInterval)duration {
NSTimeInterval shortStartDelay = 0.01;
NSTimeInterval now = player.deviceCurrentTime;
[self.audioPlayer playAtTime:now + shortStartDelay];
self.stopTimer = [NSTimer scheduledTimerWithTimeInterval:shortStartDelay + duration
target:self
selector:#selector(stopPlaying:)
userInfo:nil
repeats:NO];
}
- (void)stopPlaying:(NSTimer *)theTimer {
[self.audioPlayer pause];
}
Bear in mind that stopTimer will fire on the thread's run loop, so there will be some variability in how long the audio plays, depending on what else the app is doing at the time. If you need a higher level of precision, consider using AVPlayer instead of AVAudioPlayer. AVPlayer plays AVPlayerItem objects, which let you specify a forwardPlaybackEndTime.

How to get file size and current file size from NSURL for AVPlayer iOS4.0

self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:#"http://myurl.com/track.mp3"]] retain];
I am trying make a UIProgressView for the above track. How do I obtain the file size and current file size from that URL? Please help, thanks!
You need to start observing the loadedTimeRanges property of the current item, like this:
AVPlayerItem* playerItem = self.player.currentItem;
[playerItem addObserver:self forKeyPath:kLoadedTimeRanges options:NSKeyValueObservingOptionNew context:playerItemTimeRangesObservationContext];
Then, in the observation callback, you make sense of the data you're passed like this:
-(void)observeValueForKeyPath:(NSString*)aPath ofObject:(id)anObject change:(NSDictionary*)aChange context:(void*)aContext {
if (aContext == playerItemTimeRangesObservationContext) {
AVPlayerItem* playerItem = (AVPlayerItem*)anObject;
NSArray* times = playerItem.loadedTimeRanges;
// there is only ever one NSValue in the array
NSValue* value = [times objectAtIndex:0];
CMTimeRange range;
[value getValue:&range];
float start = CMTimeGetSeconds(range.start);
float duration = CMTimeGetSeconds(range.duration);
_videoAvailable = start + duration; // this is a float property of my VC
[self performSelectorOnMainThread:#selector(updateVideoAvailable) withObject:nil waitUntilDone:NO];
}
Then the selector on the main thread updates a progress bar, like so:
-(void)updateVideoAvailable {
CMTime playerDuration = [self playerItemDuration];
double duration = CMTimeGetSeconds(playerDuration);
_videoAvailableBar.progress = _videoAvailable/duration;// this is a UIProgressView
}
I think you do not want to know anything about file-sizes, but you're more interested in times.
Try self.player.currentItem.asset.duration for duration of currently playing item, self.player.currentTime for current time.
#"loadedTimeRange" is a KVO value for the AVPlayerItem class. You can find its definition in the AVPlayerItem.h file in the
#interface AVPlayerItem (AVPlayerItemPlayability)
category definition.