play audio in a given time range in ios - ios

I'm using AVAudioPlayer to play a mp3 file. instead of playing whole mp3, I want to specify play time range, like from 2 seconds to 5 seconds. how to specify the play time range?

You can either add an observer to playback and stop it when currentTime will become the time you want or you can cut your mp3 file to specified time and play it. There are various ways for doing this thing.

You might be able to do it using AVPlayer, AVPlayerItem and AVAsset. The AVPlayerItem is able to seek in an audio file.
I haven't tested it, but it might look something like this.
AVAsset *asset = [AVAsset assetWithURL:URL_TO_YOUR_MP3];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
/* start playback at 2 seconds */
[playerItem seekToTime:CMTimeMake(2.0, 1.0) completionHandler:^(BOOL finished) {
/* Instantiate player */
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
/* Start playback */
[player play];
/* Pause player in 3 seconds */
[player performSelector:#selector(pause) withObject:nil afterDelay:3.0];
}];

Related

Play next video on button click in AVPlayer

I have 2 videos. I am using AVPlayer to play videos. The first one is loaded when App starts, and when I click on the next video, it should start playing that video from the start. How can it be done?
I am using the following code in attempt to switch to the next video
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithURL:url];
[self pause];
[self.moviePlayer replaceCurrentItemWithPlayerItem:playerItem];
[self.moviePlayer seekToTime:kCMTimeZero];
[self play];

How to play audio in iOS with UI of music player?

I am trying to play audio in iOS using AVPlayer but i am not able to see any Deafault UI fro playing the audio. Here is my code
NSURL *url = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
player = [AVPlayer playerWithURL:url];
[player play];
Audio os played but not any audio player UI is diplayed as in case of MPMoviePlayerController UI is shown while playing the video.Please tell me the solution here?
iOS is not providing any native kind of the UI for Playing Audio for that.you create own UI and that manage by the developer thru AVAudioPlayer class.
Here is tutorial for the custom Playing Audio with AVAudioPlayer

How do I control playback of a video in an iOS Sprite Kit SKVideoNode?

I load a video into my Sprite Kit SKVideoNode, but how do I stop and restart the playback or go to a specific time in the video? All I see are play and pause methods.
// AVPlayer
NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"HowToPlay" ofType:#"mp4"]];
_avPlayer = [AVPlayer playerWithURL:fileURL];
// SKVideoNode
_videoNode = [SKVideoNode videoNodeWithAVPlayer:_avPlayer];
[_videoNode play];
This turned out to work for me.
Restart:
[_videoNode removeFromParent];
_videoNode = [SKVideoNode videoNodeWithVideoFileNamed:#"video.mp4"];
_videoNode.position = ...
_videoNode.size = ...
[self addChild:_videoNode];
[_videoNode play];
Pause:
[_videoNode pause];
_videoNode.paused = YES;
Play:
[_videoNode play];
_videoNode.paused = NO;
All you can do with a SKVideoNode is documented here. play and pause are the only supported playback methods.
There is no stop because its equivalent to pause in this context - what should "stop" do, render a black texture? If the SKVideoNode should be removed on "stop" then just send it the removeFromParent message, that'll "stop" it.
Rewind is unnecessary because you can simply run the play method again or create a new SKVideoNode.
I assume jumping to a specific timeframe isn't supported because this can't be instant, so again there's the problem of what the node should display in the time until the video playback position was moved to the specified timestamp?
If you keep a reference to the AVPlayer object used to initialize the video node then you can control playback with its methods

How do I configure AVPlayer to play a video that was recorded at 120fps?

I have a simple app that records video at 120fps, saves it to the documents directory, and then uses an instance of AVPlayer to play it back.
If I set the playback rate to 1.0 (for the AVPlayer instance), the playback is a bit choppy (probably more along the lines of 60fps). However, if I interrupt the playback by pressing the "play" button (which then sets the playback rate to 1.0 again!), it plays back very smoothly.
For instance, this is what occurs when the user presses the play button (and yes, I observe the 'status' property of AVPlayer before allowing the user to play the video):
- (IBAction)playButtonPressed:(id)sender
{
[self.videoPlayer seekToTime:kCMTimeZero];
[self.videoPlayer setRate:1];
self.videoPlayer.volume = 1;
}
And this is my simple player-setup code:
- (void)setUpAVPlayer
{
self.videoURL = [[NSURL alloc] initFileURLWithPath:self.videoURL.path];
self.videoPlayer = [AVPlayer playerWithURL:self.videoURL];
[self.previewLayer setPlayer:self.videoPlayer];
// observe player status
[self.videoPlayer addObserver:self
forKeyPath:#"status"
options:0
context:nil];
}
There really is nothing unusual about what I'm doing. It plays back video fine. It's only the case where the video was recorded at 120fps that there is a playback issue.
(Also, this is running on my iPhone5s since it is the only device that supports 120fps.)

Play audio when already playing

My audio won't play unless its finished playing even if I stop it then play it.So if you play the sound and play it again before the sound clip reaches the end, won't play again. How can I make it play and start from the beginning even if its still playing.
-(void)playSound{
[avplayer stop];
[avPlayer play];
}
As per the documents -
The stop method does not reset the value of the currentTime property to 0. In other words, if you call stop during playback and then call play, playback resumes at the point where it left off.
Set the currentTime property to 0.0 before playing again.
-(void)playSound{
[avplayer stop];
avplayer.currentTime = 0.0;
[avPlayer play];
}

Resources