Cannot play video audio via speakers - iOS - ios

I have an iOS 9 application and I have implemented the AVPlayerViewController into one of my view controllers. I am trying to play a video. One problem I have noticed is that if I plug in the headphones, the audio plays in the headphones fine. But if I unplug the headphones, the audio will play from the little ear speaker at the top of the iPhone and NOT from the main speaker. How can I change this? Here is my code:
// Setup the video player view.
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:pass_URL];
self.player_view = playerViewController;
self.player_view.view.frame = self.view.frame;
[self.view addSubview:playerViewController.view];
self.view.autoresizesSubviews = TRUE;
[self.player_view.player play];
I have imported the following frameworks into my Xcode project:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Thanks for your time, Dan.

You need to add a routing change observer (AVAudioSessionRouteChangeNotification) to your AVAudioSession. You can then detect the removal of the headphones and dictate the routing yourself (e.g. with overrideOutputAudioPort) if necessary.

Related

Is it possible to enable airplay mirroring programmatically?

I've looked around a lot regarding this question but all the ones I've found are pretty outdated.
example
this article is from 2012 and says that it's not possible
Basically I want to be able to start mirroring my iphone through airplay to another screen from within my app and I'm wondering if that's possible now. The articles I've found all say that it isn't possible, or if done, the app won't be accepted for the app store.
It's possible to implement own AirPlay picker view as of iOS 11. Please check AVRoutePickerView. It'll allow you to control connections to different AppleTVs/external screens within the app. See example:
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVRoutePickerView *routePickerView = [[AVRoutePickerView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, 30.0f, 30.0f)];
routePickerView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:routePickerView];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:#"https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8"]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:CGRectMake(0.0f, 40.0f, self.view.frame.size.width, self.view.frame.size.height - 40.0f)];
[self.view.layer addSublayer:playerLayer];
[player seekToTime:kCMTimeZero];
[player play];
}
#end
As a result by pressing on AVRoutePickerView you'll get something like this:
By pressing on available external screen you'll be able to playback content on it.
It's still not possible. There are no public APIs for enabling AirPlay (and private ones won't be submitted). It's a system level feature that can't be manipulated from an app.
The user must initiate AirPlay, but you could simply display a message suggesting the user turn on AirPlay to enhance the app.

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

avplayer plays video on simulator not on device?

i am having issues with playing a video through url using avplayer and avplayerviewcontroller
it plays fine on simulator but doesnt work on device.
Is it a problem with the static ip i have been using as follows -
https://ipaddress(111.111.11.11:443)/mc/images/wall/small.mp4
same video from the following url plays fine on device
http://techslides.com/demos/sample-videos/small.mp4
the code below i am using -
NSURL *url = [NSURL URLWithString:#"http://techslides.com/demos/sample-videos/small.mp4"];//#""];https://180.179.77.47:443/mc/images/wall/small.mp4
AVAsset *videoAsset = [AVAsset assetWithURL:url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:videoAsset];
playerVideo = [AVPlayer playerWithURL:url];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionModeMoviePlayback error:nil];
movieController = [[AVPlayerViewController alloc]init];
[movieController setAllowsPictureInPicturePlayback:YES];
movieController.player = playerVideo;
movieController.delegate = self;
movieController.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self presentViewController:movieController animated:YES completion:nil];
I was also facing the same issue short while ago, but just now observed that my device was on silent mode & hence it was running video without sound.
But it should not have happened. If I run YouTube app & plays video, it works fine even if device is on silent mode.
I found the answer. Following is the Swift 3 code. If you set the category to AVAudioSessionCategoryPlayback and if the device is on silent mode then the app plays video with sound.
If you set the category to AVAudioSessionCategoryAmbient, and if you run the app on device with silent mode on, then the app will play the video without sound.
let audioSession = AVAudioSession.sharedInstance()
do
{
try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
}
catch
{
print("AVAudioSession cannot be set")
}
I think your video and your device are not withing same network. Used common network for both or put it at remote location from where everyone can access it.
You can play video in simulator because your system (mac) is connected with common network on which video is available but i think you are using different network for mobile network.
Hope this will help :)

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

Is MPMusicPlayerController working properly on iPod 5g?

I am developing an application for iOS which works in two different modes, one of them plays music from the iPod library, and the other one synthesizes sounds (eg: pure tones).
In order to synthesize sounds I use an audio unit, so through its callback I can pass the desired signal. As far as sounds must be played at an specific volume, I set the device volume using iPodMusicPlayer.
The issue I see is that the first time the application is run on an iPod Touch 5g after turning it on, once the iPodMusicPlayer is used to set the volume of the device, applicationMusicPlayer will not respond to stop.
I came up with the following code. Runing it on an iPod Touch 5g after turning it on, music will not stop after touching stop.
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MPMusicPlayerController *mp = [MPMusicPlayerController iPodMusicPlayer];
mp.volume = 0.3;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playAction:(id)sender {
MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
MPMediaQuery *query = [MPMediaQuery songsQuery];
[mp setQueueWithQuery:query];
[mp play];
}
- (IBAction)stopAction:(id)sender {
MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
[mp stop];
}
#end
PS: It is also possible to reproduce the issue by entering the music app, the quitting it (double press on home button and quit music from the multitasking bar), and finally run the code above.
In the other devices I have tested so far (iPad 2nd and 3rd generation, iPod Touch 4 generation) the application works well.
Thanks in advance.

Resources