使用AVAudioPlayer重复播放音频 [英] Repeating Audio playback with AVAudioPlayer

查看:132
本文介绍了使用AVAudioPlayer重复播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVAudioPlayer,希望用户可以在播放循环后重新开始播放.对于用户反馈,我使用了UIAlertController,但是我仍然很难通过AVAudioPlayer完成重播.我希望用户可以随意选择重播,直到选择否"为止.

I'm using AVAudioPlayer and want that the user can restart the playback once the loop is played back. For the user feedback I used UIAlertController but I still struggle to get the replay done with AVAudioPlayer. I want that the user can choose replay as often as he like until he choose NO.

这是我到目前为止的代码,但是如果用户选择是,该怎么办...请参见下面的代码中的注释.

Here is my code I have so far, but what to do if the user select YES ... see comment in code below.

    // schedule the audio file
    [self->_playerNode scheduleFile:self->_file atTime:nil completionHandler:^{

          dispatch_async(dispatch_get_main_queue(), ^{

                UIAlertController * alert=   [UIAlertController
                                              alertControllerWithTitle:@"End of file"
                                              message:@"play again?"
                                              preferredStyle:UIAlertControllerStyleAlert];

               UIAlertAction* yes = [UIAlertAction
                                     actionWithTitle:@"YES"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     {

                                            // WHAT TO DO HERE????

                                            [alert dismissViewControllerAnimated:YES completion:nil];
                                     }];

                UIAlertAction* no = [UIAlertAction
                                     actionWithTitle:@"NO"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     {
                                            NSLog(@"Stop");
                                            [self->_engine stop];
                                            [session setActive:false error:nil];
                                            [alert dismissViewControllerAnimated:YES completion:nil];
                                     }];

                [alert addAction:yes];
                [alert addAction:no];

                [self presentViewController:alert animated:YES completion:nil];
            });
        }];

    // playback the audio file
    [self->_playerNode play];

感谢您的帮助!

推荐答案

由于没有其他建议,我将回答我自己的问题:

As there is no one with any other suggestion I will answer my own question:

我找不到递归加载播放器的方法,所以我读出了文件的长度并启动了计时器:

I could not find a way to recursivly load the player so I read out the lengths of the file and started a timer:

NSTimeInterval seconds = self->_file.length / SAMPLERATE;

您还需要处理应用程序进入后台模式并停止+恢复计时器的情况!

You will also need to handle the case the app goes to background mode and stop + resume the timer!

这篇关于使用AVAudioPlayer重复播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆