How to avoid overlapping background music, everytime the viewController loads…?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • How to avoid overlapping background music, everytime the viewController loads…?

    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....!
  • Wenn du die Soundkontrolle unabhängig von einem View haben willst, dann packe sie doch einfach woanders hin als in einen ViewController. Pack' deine Soundverwaltung doch einfach in eine separate Klasse und hänge sie z.B. an das Application Delegate.

    Was anderes: Wäre es zu viel verlangt, die Frage auf deutsch zu schreiben anstatt das Ganze per C&P von Stackoverflow rüberzukopieren? Das würde wahrscheinlich bei einigen hier die Motivation zu helfen enorm steigern. Die meisten hier verstehen Englisch, aber es ist nunmal ein deutschsprachiges Forum und es macht deutlich, dass du spezifisch hier fragst - was soll sich jemand die Mühe machen, deine Frage zu beantworten, wenn du dir keine Mühe dabei machst, die Frage zu stellen?
    Multigrad - 360°-Produktfotografie für den Mac