Audio playback for movie despite in silent mode? - ios

sorry for my question but i´ve implemented a intro- video and despite of the hardware silent-switch of my iPad, the audio is playing.
I´m also using the AVAudioplayer within my app just for playing short sound samples. Within this class, its the only region where i´ve set up the "AVAudioSessionCategory".
But for all audio playback only, there´s nothing hearable. Its just for my intro-video.
Any help how to fix that "audio-bug" so the movie player is silent?
Thanks you
Here´s my Audio-class:
- (id)initWithSoundfileName:(NSString*) file
{
if ((self = [super init]))
{
NSString* filename = [file stringByDeletingPathExtension];
NSString* fileextension = [file pathExtension];
// get file path from bundle
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: filename ofType: fileextension];
NSLog(#"AudioPlayer init: %#", soundFilePath);
NSURL* fileurl = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSError* error = nil;
AVAudioPlayer* audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:&error ];
if (error) { NSLog(#"Error creating AVAudioPlayer %#", [error description]);}
// set audio policy
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];
self.player = audioplayer;
[self.player prepareToPlay];
[self.player setDelegate:self];
}
return self;
}
-(void) play{
[self.player play];
}
And here´s my video-playback method:
- (void)playIntroVideo
{
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"mymovie" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:movpath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayerController.useApplicationAudioSession = NO;
[self.moviePlayerController.view setFrame: self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayerController];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController play];
}

Like #Till mentioned:
When I change the MoviePlayerController property to useApplicationAudioSession=TRUE, it fixes my problem. Audio Playback is silent.

Related

iOS Background Video Loop GIF UIWebview or VideoPlayer

I need to write an application where there are different short video loops in the background of a View. These loops should typically have a length of 10-30 seconds. I was wondering what would be better in terms of performance and memory economics (the loops should be bundled with the app when downloaded from the appstore)
a) Use a UIWebview as Background where an animated .gif of the video is looped
b) Use some VideoPlayerView (AVPlayer) as Background with an m4v-like file
c) Some alternative that I haven't considered yet
There will also be audio, however audio is not linked to the video
- (void)playIntroVideo
{
// NSBundle *bundle = [NSBundle mainBundle];
// // NSString *moviePath = iPadDevice ? [bundle pathForResource:#"IntroVideo" ofType:#"mp4"] : [bundle pathForResource:#"IntroVideo_iPhone" ofType:#"mp4"];
// NSString *moviePath = [bundle pathForResource:#"IntroVideo" ofType:#"mp4"];
// NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
CGRect frame = self.movieView.frame;
frame.origin.x=-200;
frame.size.width+=200;
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:avAsset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *selectedImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
UIImageView *img = [[UIImageView alloc] initWithImage:selectedImage];
img.frame = frame;
[self.movieView addSubview:img];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResize];
if (iPhoneDevice)
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[avPlayerLayer setFrame:frame];
[self.movieView.layer addSublayer:avPlayerLayer];
[self.avplayer seekToTime:kCMTimeZero];
// Not affecting background music playing
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
// [self.avplayer setVolume:0.0f];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self playIntroVideo];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.avplayer play];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.avplayer pause];
// self.avplayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avplayer currentItem]];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}

itemFailedToPlayToEnd Error in iOS while playing video

-(void)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files array %#", filePathsArray);
NSString *fullpath;
for ( NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(#"vurl %#",vedioURL);
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer prepareToPlay];
[moviePlayer.view setFrame:self.view.bounds]; // player's frame must match parent's
//moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
moviePlayer.moviePlayer.fullscreen = YES;
[moviePlayer.moviePlayer play];
[self.view bringSubviewToFront:btnSkip];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:#selector(movieFinishedCallback:)
// name:MPMoviePlayerPlaybackDidFinishNotification
// object:moviePlayer];
}
Getting Following ERROR
_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; } error in iOS while playing video
in .h file create a new instance as
MPMoviePlayerController *player
in .m file
- (void) playMovieOffline:(UIView *)view
{
// Register to receive a notification when the movie has finished playing.
NSString *path = [[NSBundle mainBundle]pathForResource:
#"videofile_name" ofType:#"mp4"];
player = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setControlStyle:MPMovieControlStyleNone];
player.movieSourceType = MPMovieSourceTypeFile;
[player play];
[player.view setFrame:CGRectMake(0, 0, 320, 235)];
[view addSubview:player.view];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == player) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[player play];
}
}
}
Try this, i hope it helps

Memory issue with AVAudioPlayer

For some reason, the cleanupPlayer method is a little buggy and isn't releasing the audio player correctly. My application crashes from time to time, and I suspect it's due to a memory issue. Also, when I try to play an audio file twice (on button click), the second time the audio sometimes cuts out. I'm a bit of a newbie, so any help would be greatly appreciated!
Here is a snippet of code:
.h file:
#property (retain,nonatomic)AVAudioPlayer *player;
.m file:
-(void)playSound:(NSString *)fileName
{
// Play
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:kSoundFileType]];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
_player.delegate = self;
[_player prepareToPlay];
[_player play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self cleanupPlayer];
}
-(void)cleanupPlayer
{
if(_player != nil) {
[_player release];
}
try this...
-(void)playSound:(NSString *)fileName
{
if (_player.isPlaying) {
[_player stop];
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3"]];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
_player.delegate = self;
[_player prepareToPlay];
[_player play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self cleanupPlayer];
}
-(void)cleanupPlayer
{
_player =nil;
}

iOS playing video exc_bad_access

would you please help me with this code, I'm trying to play video but I'm having error Exc_bad_Access code = 1 and some times code = 2:
-(IBAction)BtnPressed:(id)sender{
self.videoview.hidden = false;
NSString *btnTag = [NSString stringWithFormat:#"%d",[sender tag]];
NSString *videofilename = [NSString stringWithFormat:#"%#%#_%#", selectedGender, btnTag, selectedVowel];
//Playing video
NSString *filepath = [[NSBundle mainBundle] pathForResource:videofilename ofType:#"mp4"];
NSLog(#"file name is %#",filepath);
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:btnTag ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller];
//[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//playercontroller.moviePlayer.scalingMode = MPMovieScalingModeNone;
playercontroller.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playercontroller.view setFrame:CGRectMake(30, 50, 150, 200)];
[self.videoview addSubview:playercontroller.view];
[playercontroller.moviePlayer prepareToPlay];
[playercontroller.moviePlayer play];
//playercontroller = nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Waiting for your advice
It looks like your MPMoviePlayerViewController isn't retained, so it is probably getting deleted.

Video playback disappears after a few seconds

i want to play a video within a UIView using MPMoviePlayerController.
After pressing a UIButton, the video appears and starts playing.
Unfortunately after 3-4 seconds, the video gets black, the audio is still playing.
Any ideas?
Thanks for all your time.
(Using Xcode 4.2.1)
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
player.movieSourceType = MPMovieSourceTypeFile;
[player.view setFrame:CGRectMake(20, 400, 300, 200)];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
- (void) moviePlaybackComplete:(NSNotification*) notification {
NSLog(#"videoviewcontroller complete: %#", notification);
MPMoviePlayerController *mymoviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mymoviePlayerController];
}
I´ve talked to an Apple Engineer. The solution is that the player would have to be an instance variable or property of the view controller. So its still accessible when view was being torn down.
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
// .....
}

Resources