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

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

Related

Play audio from video when phone is on silent mode

I am building an application that plays certain video files. However, when the phone is on vibrate the audio still plays. Apps like youtube still play the audio form their videos when the phone is on vibrate. How can I do this? I am using avplayer to play the video. Below is my code for playing the video
_player = [[AVPlayer alloc]initWithPlayerItem:pi];
AVPlayerViewController *playerController = [[AVPlayerViewController alloc]init];
playerController.player = _player;
[self presentViewController:playerController animated:YES completion:nil];
playerController.view.frame = self.view.frame;
[_player setMuted:false];
[self addChildViewController:playerController];
Found the solution. Adding this code allows whatever video is playing to produce audio even on silent mode.
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];

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 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

play audio in a given time range in 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];
}];

Movieplayercontroller using a m4v with two audio tracks

I need use in my App a m4v video with two audio tacks. But when I play the video, it starts with the two audio tracks at the same time. I need to load the audio track with the device's default language (English by default).
Here is my code:
_moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[_moviePlayer shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
_moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:_moviePlayer];
MPMoviePlayerController just as well as the wrapping MPMoviePlayerViewController do not support the selection of audio tracks.

Resources