【问题标题】:Repeating Audio playback with AVAudioPlayer使用 AVAudioPlayer 重复音频播放
【发布时间】:2020-04-05 14:31:48
【问题描述】:

我正在使用 AVAudioPlayer,并希望用户可以在循环播放后重新开始播放。对于用户反馈,我使用了 UIAlertController,但我仍然很难使用 AVAudioPlayer 完成重播。我希望用户可以随心所欲地选择重播,直到他选择否。

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

    // 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];

感谢您的帮助!

【问题讨论】:

    标签: ios objective-c avaudioplayer uialertcontroller


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2017-03-30
      相关资源
      最近更新 更多