Adcolony is a video advert platform integrated into Xcode to play ads to users in video and sound,
One of the requirements is to block audio when the add starts playing,
This is the audio:
//Audio Sound On - uncomment
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"soundTrack"
ofType:#"mp3"]];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
it starts as soon as the game starts, in view controller,
The triggerVideo action is place in PlayViewController and needs to be set off when ad is triggered,
- (IBAction)TriggerVideo:(id)sender {
[AdColony playVideoAdForZone:#"ZONE-NUMBER-HERE" withDelegate:nil];
}
I've left this aside for a while now, wondering if anyone knows how to employ this function?
add these methods to your viewController that's the delegate for adcolony, which should also be the viewcontroller in which you initialize the AVAudioPlayer. I probably don't need to state this, but i'm assuming you have an #Property for your audioPlayer
- (void) onAdColonyAdStartedInZone:(NSString *)zoneID{
self.audioPlayer.volume = 0.0f;
}
- (void) onAdColonyAdAttemptFinished:(BOOL)shown inZone:(NSString *)zoneID{
self.audioPlayer.volume = 1.0f;
}
Related
Hello my sounds play but if I keep hitting multiple sounds rapidly I get a crash EXC_BAD_ACCESS(code=1). Been trying for a long time to figure this one out. I tried using properties as well but no luck. Anybody ever seen this happen when playing multiple simultaneous sounds?
#interface SoundBoardScene : SKScene<AVAudioPlayerDelegate>
AVAudioPlayer *Player1;
AVAudioPlayer *Player2;
AVAudioPlayer *Player3;
AVAudioPlayer *Player4;
AVAudioPlayer *Player5;
AVAudioPlayer *Player6;
AVAudioPlayer *Player7;
AVAudioPlayer *Player8;
-(AVAudioPlayer*) PlayAudioFile:(AVAudioPlayer*)player withAudioFileName:(NSString*)audioFileName withExtensionType: (NSString*)audioFileExtension withLoopCount:(uint)NumberOfLoops
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:audioFileName
ofType:audioFileExtension]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = NumberOfLoops;
return player;
}
-(void) initSounds
Player1 = [animalObject PlayAudioFile:Player1 withAudioFileName:#"Sound1" withExtensionType:#"mp3" withLoopCount:0];
Player1.delegate=self;
Player2 = [animalObject PlayAudioFile:Player2 withAudioFileName:#"Sound2" withExtensionType:#"mp3" withLoopCount:0];
Player2.delegate=self;
etc
etc
}
if([node.name isEqualToString:#"Sound1"])
{
[Player1 play];
}
if([node.name isEqualToString:#"Sound2"])
{
[Player2 play];
}
there is no need to use avplayer for play sound file.
try this code
[self runAction:[SKAction repeatAction:[SKAction playSoundFileNamed:#"pew1.wav" waitForCompletion:NO] count:count]];
Hi I'm playing the audio file in one view controller when i going the another view controller its still playing the audio how to pause the audio when we going back to another view controller.
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
Please tell where i have to put the pause code for the audio when I'm going for another view controller.
Thanks.
in your viewController dealloc method add code to stop your audioPlayer
- (void)dealloc {
if ([self.audioPlayer isPlaying])
[self.audioPlayer stop];
[super dealloc]; // call if you are not using ARC
}
You can also put the stop audio code in ViewWillDisappear method if you are going deeper into heirarchy
Whenever i play an audio file like below, it is taking few seconds to start playing the audio. I have seen from some forums and understand that, AVAudioPlayer will take few seconds to start playing if we allocate the object there itself. I am thinking to allocate this object much earlier (may be Appdelegate itself), before when i want to play, so that when i want to play it, it will play immediately.
NSURL *audioPathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:audioName ofType:#"wav"]];
audioP = [[AVAudioPlayer alloc]initWithContentsOfURL:audioPathURL error:NULL];
[appDelegate.audioP play];
but, i am passing audio url dynamically at run time, so i would like to know how can i allocate the object earlier and then be able to pass dynamic audio url path later?
Please note, my question is different from this question Slow start for AVAudioPlayer the first time a sound is played
In the existing question, they are telling about one audio file and play it when required. But, i am having many different audio file and url path is generated randomly and at run time to play, so i do not know what the actual url path to set and play. So, the answer mentioned here->Slow start for AVAudioPlayer the first time a sound is played
won't help for my question.
I can't use prepareToPlay, because audio path url is set at run time and not just one audio is being used for all the times to play, there will more than 20 audio file randomly chosen and set to play one at a time. So, i need the right answer for it, it is not a duplicate question.
Thank you!
For a single file here the solution:
in your file.h
#import <AVFoundation/AVFoundation.h>
#interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> {
}
To use with button add - (IBAction)Sound:(id)sender; in your file.h
Now in your file.m
//this is a action but you can use inside a void to start auto
#implementation AudioPlayer {
SystemSoundID soundID;
}
- (IBAction)Sound:(id)sender {
AudioServicesDisposeSystemSoundID(soundID);
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
//you can use here extension .mp3 / .wav / .aif / .ogg / .caf / and more
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"sound" ,CFSTR ("mp3") , NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
If you want use your code to play audio from URL you can use this:
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface AudioPlayer : UIViewController <AVAudioPlayerDelegate> {
AVPlayer *mySound;
}
#property (strong, nonatomic) AVPlayer *mySound;
#end
And file.m
- (IBAction)playPause:(id)sender {
if (mySound.rate == 0){
NSURL *url = [NSURL URLWithString:#"http://link.mp3"];
mySound = [[AVPlayer alloc] initWithURL:url];
[mySound setAllowsExternalPlayback:YES];
[mySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[mySound play];
} else {
[mySound pause];
}
}
Hope this can help you!
One thing you can do to reduce the latency of the first load+play is to "spin up" the underlying audio system before you need it by loading and playing a dummy file. For example, in application:didFinishLaunchingWithOptions: add some code like this:
// this won't effect the problem you're seeing, but it's good practice to explicitly
// set your app's audio session category
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *audioURL = [[NSBundle mainBundle] URLForResource:#"dummy.wav" withExtension:nil];
AVAudioPlayer *dplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
// we don't want to hear it (alternatively, the audiofile you use for this purpose can be silence.)
dplayer.volume = 0.f;
[dplayer prepareToPlay];
[dplayer play];
[dplayer stop];
Once this code completes, subsequent instantiations and playback of AVAudioPlayer instances should have pretty low latency (certainly well under 1 second)
In a view controller I have a video playing and also some background music. If the background music is playing, the video freezes when it goes to loop. If the background music is not playing the video loops normally as it should.
Here's how I'm setting up the video player:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getBundleClip:#"MyVideo"]];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
The audio is playing via this Sound Manager. Any idea why the video would be freezing?
Try to use play music in background AVAudioPlayer and play same time MPMoviePlayerController like below code for play the AVAudioplayer in repeat mode:
first initialize the AVAudioplayer object in your header file (.h) like below:
#interface Yourviewcontroller : UIViewController
{
//your some codes after
AVAudioPlayer *audioPlayer;
}
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
after implement play audio file in background like below code:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"]] error:NULL];
audioPlayer.numberOfLoops =-1;//it's play audio repeatedly
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
after play audio any part of project like below code:
[audioPlayer play];
you want stop the audio player like below code:
[audioPlayer stop];
In my app I use AVAudioPlayer to play some mp3. My code is:
- (void) play{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
[arraysound objectAtIndex:appDelegate.indexSound]];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
[soundID prepareToPlay];
[soundID play];
}
but I have 40 sounds to play; I use everytime soundID to play my sounds
example I have sound1.mp3, sound2.mp3, sound3.mp3......
How can I release my soundId?
When my sound change I call "play" method but this method alloc a new AVAudioPlayer everytime
I Know
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
but I don't understand how to release my sound id inside this delegate method, because if I release inside soundId my app crash
after some minutes that I use my app I have with crash this message "shm_open failed: "Apple Audio Queue"
please help me...thanks
You don't want to release soundID in your delegate method. You want to release player. Judging from your code, soundID is an ivar. When the delegate gets called, soundID may have been set to another value. But the player parameter will be the AVAudioPlayer that has just finished playing.