Hello Everyone
I'm trying to implement background music in my app with avfoundation. Everything is working fine, until I go back to the ViewController, which loads my music file. So the songs starts again and overlaps the song, which is already playing. Somehow it seems clear that the music file loads every time I go back to that certain ViewController which loads the file. So how can I avoid that situation?
So that would be my first question. Second question would be, how to mute the song and unmute the song again. I implemented an UiButton which pauses the song and by repushing the button the song starts again. Now my question is, how can I mute and unmute the song with my method when I don't want the button to be in the same ViewController as the ViewController that loads my song?
Thanks in advance: Code in the same ViewController:
-(IBAction)playsound:(id)sender
{
if (![sound isPlaying])
{
[sound play];
}
else
{
[sound pause];
}
}
-(void)viewDidLoad
{
//BackgroundMusic
soundfile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"music"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc]initWithContentsOfURL:soundfile error:nil];
sound.delegate = self;
[sound play];
sound.numberOfLoops = -1;
}
thank you in advance....!
I'm trying to implement background music in my app with avfoundation. Everything is working fine, until I go back to the ViewController, which loads my music file. So the songs starts again and overlaps the song, which is already playing. Somehow it seems clear that the music file loads every time I go back to that certain ViewController which loads the file. So how can I avoid that situation?
So that would be my first question. Second question would be, how to mute the song and unmute the song again. I implemented an UiButton which pauses the song and by repushing the button the song starts again. Now my question is, how can I mute and unmute the song with my method when I don't want the button to be in the same ViewController as the ViewController that loads my song?
Thanks in advance: Code in the same ViewController:
-(IBAction)playsound:(id)sender
{
if (![sound isPlaying])
{
[sound play];
}
else
{
[sound pause];
}
}
-(void)viewDidLoad
{
//BackgroundMusic
soundfile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"music"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc]initWithContentsOfURL:soundfile error:nil];
sound.delegate = self;
[sound play];
sound.numberOfLoops = -1;
}
thank you in advance....!