简体   繁体   English

AVAudioPlayer超过2个声音

[英]AVAudioPlayer more then 2 sounds

So i tried by all guides that provided here to play at same time 2 sounds by AVAudioplayer + AVA , AVA + System Sound and it didn't worked. 所以我尝试了所有在这里提供的指南同时播放AVAudioplayer + AVA,AVA + System Sound的2个声音,但它没有奏效。 Second sound never sounds. 第二声音听不到。

So i decided to stop main audio before i play 2. audio (the bell audio) and it didn't worked. 所以我决定在播放之前停止主音频2.音频(铃声音频)并没有奏效。

I need to play 2 sounds at the same time or when 1. audio will be paused.(I have delegate in my h file) 我需要同时播放2个声音或者1.音频将被暂停。(我的h文件中有委托)

Here is code , this method called by a NSTimer every 0.5 seconds: 这是代码,这个方法每隔0.5秒由NSTimer调用:

if(!(bellint == -0.5)){
    bellint = bellint+0.5;
}
if(!(self.loaderAudioView.fakeDuration == secondsTime)){
    secondsTime = secondsTime +0.5;
    self.loaderAudioView.fakeTimer = secondsTime ;
    NSLog(@"fakedur %f , secondsOfTimer : %f" ,self.loaderAudioView.fakeTimer,secondsTime);
}
else{
    self.loaderAudioView.fakeTimer = secondsTime = 0 ;
    [self.timer invalidate];
    self.playOrPauseButton.on = NO;
}
if((int)secondsTime % self.bellsInt == 0.0){
    [self.loaderAudioView pausePlayer];
     bellint = bellint+0.5;

    NSURL *path = [[NSBundle mainBundle] URLForResource:@"bell_start" withExtension:@"mp3"];
    // I've putted here AVAudioplayer initialization cause you can see. It's in another method realized.Here I just call [objAudio play];

    AVAudioPlayer* objAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];

    objAudio.delegate = self;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [objAudio prepareToPlay];
    [objAudio play];

    NSLog(@"BANG");
}
if(bellint == 5.0){
    [self.loaderAudioView playPlayer];

    bellint = -0.5;
}

So after i placed my objAudio to h file and initialized 在我把我的objAudio放到h文件并初始化之后

self.objAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];

Everything became as should be. 一切都变得如此。


I don't know why it didn't worked like that. 我不知道为什么它没那样起作用。
Is it bug or there a certain reason. 它是错误还是有某种原因。
If anyone knows why or have a theory I welcome. 如果有人知道为什么或有一个理论我欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM