I am new to Xcode,please help me out. I added AVPlayer in View Controller in IOS app.It displays Video in view But without audio. Any help anybody
Advance thank you
the reason you audio is mute because you might have not initialize AVAudioSession Check this out ..
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
UIView *myView = previewView;
videoController = [[MPMoviePlayerViewController alloc]initWithContentURL:videoPlayUrl];
videoController.view.frame = myView.bounds;
[videoController.view setBounds:previewView.bounds];
videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
videoController.moviePlayer.scalingMode = MPMovieScalingModeFill;
videoController.moviePlayer.shouldAutoplay = NO;
videoController.view.clipsToBounds = YES;
[previewView.layer addSublayer:videoController.view.layer];
[videoController.moviePlayer prepareToPlay];
Related
I am using the MPMoviePlayerController to play video stored in main bundle.
However sometime when I play video it plays the video but shows the black screen in that case seek bar also works properly.
At that time if I switch to fullscreen mode and get back to normal mode(or send app in background mode and bring it in foreground mode) then it plays(resumes) video properly(did not show the black screen).
Please refer the below screenshot of an App:
UPDATE:
Below is the code used to play video:
- (void)playVideo:(NSString *)filepath {
NSLog(#"get audiosession");
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil ];
NSLog(#"finish audiosession");
appDelegate.currentCourseNoExt = [filepath stringByDeletingPathExtension];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#.mp3",appDelegate.currentCourseNoExt]];
NSError *error;
NSLog(#"get avaudioplayer");
AVAudioPlayer *avPlayerTemp = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
NSLog(#"finish avaudioplayer");
avPlayer = avPlayerTemp;
NSLog(#"copy avaudioplayer");
// get frame
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.height -= 0;
NSLog(#"self.view.frame - %#",NSStringFromCGRect(self.view.frame));
appDelegate.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
appDelegate.moviePlayer.view.frame = frame;
appDelegate.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:appDelegate.moviePlayer.view];
[self.view sendSubviewToBack:(appDelegate.moviePlayer.view)];
imgBottomAdvertisement.frame = CGRectMake(0, appDelegate.moviePlayer.view.frame.size.height - imgBottomAdvertisement.frame.size.height, imgBottomAdvertisement.frame.size.width, imgBottomAdvertisement.frame.size.height);
[appDelegate.moviePlayer.backgroundView addSubview:imgBottomAdvertisement];
appDelegate.moviePlayer.currentPlaybackRate= 1.0;
appDelegate.moviePlayer.currentPlaybackTime = appDelegate.SMile.doubleValue;
[avPlayer setRate:0];
[appDelegate.moviePlayer play ];
[avPlayer play];
}
The above method is called from viewDidAppear method of ViewController.
I am getting video url of youtube from this API https://www.googleapis.com/youtube/v3/videos?id=ynMk2EwRi4Q&part=player&key=***************************.
But the problem is i am getting vimeo video which will give the message ‘This video contains content from VEVO.It is restricted from playback on certain sites.Watch on Youtube’.I download HCYoutubeParser-master where it will convert where it will play youtube videos.But it is unable to play Vimeo Videos.
This is my code:
mp1 = [[MPMoviePlayerController alloc]initWithContentURL:_urlToLoad];
[mp1 prepareToPlay];
self.mc3=mp1;
mp1.view.frame = self.view.bounds; //Set the size
[self.view addSubview:mp1.view]; //Show the view
[mp1 play]; //Start playing
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
if (![session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&setCategoryError]) {
// handle error
}
I am streaming music file using AVAudioPlayer, It plays fine in background if i use nib file but when i try the same code using storyboard, The music playing stoped. I can't understand the reason why its stop playing in background when i using storyboard.
i am using this code for it
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:0.1];
[audioPlayer play];
Can any help me to play it in background using storyboard. Thanks in advance.
Let's try:
#implementation YourViewController
{
AVAudioPlayer *audioPlayer;
}
// if your are using ARC, after exiting this method, system will kill your audioPlayer. This is the reason why you can not play the music.
// in your method:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
I'm using MPMoviePlayerController to play a video clip that loops. In the app delegate, I set my AVAudioSession's category to AVAudioSessionCategoryAmbient so that my video clip doesn't interrupt iPod audio.
This works great when playing iPod audio on the device, but when using AirPlay, the audio is briefly interrupted every time my looping video starts over, which is pretty frequent (and annoying).
In AppDelegate.m:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:#"AVAudioSessionCategoryAmbient" error:nil];
In my video view controller:
self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.videoPlayer.useApplicationAudioSession = YES;
self.videoPlayer.controlStyle = MPMovieControlStyleNone;
self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
I've scoured the net and can't seem to find answers. Any advice would be great. Thanks!
Problem solved! I just had to use AVPlayer instead of MPMoviePlayerController.
self.avPlayer = [AVPlayer playerWithURL:url];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.avPlayerLayer];
[self.avPlayer play];
And to make it loop:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
...
- (void)movieDidFinish:(NSNotification *)notification {
[self.avPlayer seekToTime:kCMTimeZero];
}
I am running into trouble with playing an audio stream when the application enters background.
I use the code to start the stream:
NSURL *mediaURL = [NSURL URLWithString:#"http://url.to.my.stream"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setFullscreen:YES];
[self.view addSubview:[mp view]];
[mp prepareToPlay];
[mp play];
It works perfect. But allthough I set the flag 'App plays audio' in the property list when the app enters background the stream stops playing.
How do I make my application play the audio stream in the background?
Best regards and thanks a lot for help!
I didn't tried it myself but this looks promising: iOS Multitasking: Background Audio
Once the project has been created go to APP-Info.plist and add
UIBackgroundModes as a new row. It should then create the array.
Open the array and to the right of Item 0 set it to audio.
EDIT
Is your AVAudioSession set up properly?
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
This code worked for me, first you must to give your app permissions to keep playing music in the background (In your .plis), after that go to the wished class and implement this code, first the imports and the the method to play the music.
#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import <MediaPlayer/MPMediaItem.h>
#import <AVFoundation/AVFoundation.h>
---- o ----
-(void) playMusic{
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *myErr;
// Initialize the AVAudioSession here.
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
// Handle the error here.
NSLog(#"Audio Session error %#, %#", myErr, [myErr userInfo]);
}else{
// Since there were no errors initializing the session, we'll allow begin receiving remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//initialize our audio player
audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.cocoanetics.com/files/Cocoanetics_031.mp3"]];
[audioPlayer setShouldAutoplay:NO];
[audioPlayer setControlStyle: MPMovieControlStyleEmbedded];
audioPlayer.view.hidden = YES;
[audioPlayer prepareToPlay];
[audioPlayer play];
}//end playmusic