当声音在 AVAudioPlayer 中播放完毕时执行操作吗?

2024-01-03

我正在使用 AVAudioPlayer 框架,并且我有几种声音一次播放一种。当声音播放完毕后,我希望应用程序执行一些操作。我尝试使用audioPlayerDidFinishPlaying 在第一个声音结束时执行操作,但我无法将其用于第二个声音,因为我遇到了重新定义错误。我可以使用 NSTimeInterval 来获取声音的持续时间吗?

//  ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface ViewController : UIViewController {  
UIButton        *begin;
UIWindow        *firstTestWindow;
UIWindow        *secondTestWindow;
AVAudioPlayer   *player;
NSTimeInterval  duration;
}  

@property (nonatomic, retain) IBOutlet UIButton     *begin;
@property (nonatomic, retain) IBOutlet UIWindow     *firstTestWindow;
@property (nonatomic, retain) IBOutlet UIWindow     *secondTestWindow;
@property (nonatomic, retain)         AVAudioPlayer   *player;
@property (readonly)                   NSTimeInterval  duration;


- (IBAction)begin:(id)sender;
- (IBAction)doTapScreen:(id)sender;

@end



//ViewController.m
#import "ViewController.h"

@implementation ViewController

@synthesize begin, player, firstTestWindow, secondTestWindow;

//first sound
- (IBAction)begin:(id)sender; {

    [firstTestWindow makeKeyAndVisible];
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: @"Tone1"
                                    ofType: @"mp3"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: nil];
    [fileURL release];
    self.player = newPlayer;
    [newPlayer release];

    [player prepareToPlay];
    [self.player play];

}

//If user does not do anything by the end of the sound go to secondWindow
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
                    successfully: (BOOL) flag {
                               if (flag==YES) {
    [secondWindow makeKeyAndVisible];
    }
}

//second sound
- (IBAction)doTapScreen:(id)sender {
    //When user touches the screen, either play the sound or go to the next window
    if (self.player.playing) {
    [self.player stop];
    [thirdWindow makeKeyAndVisible];
    }

    else {
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: @"Tone2"
                ofType: @"mp3"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: nil];
    [fileURL release];
    self.player = newPlayer;
    [newPlayer release];

    [player prepareToPlay];
    [self.player play];

}
}

当声音播放完毕后,我希望应用程序执行一些操作。

然后将自己设置为代表并回复audioPlayerDidFinishPlaying:successfully:.

在您显示的代码片段中,您已完成步骤 2,但未完成步骤 1:您没有将自己设置为委托人。

我尝试使用applicationDidFinishLaunching在第一个声音结束时执行动作...

脑放屁?我认为该方法与播放声音无关。

…,但我无法将其用于第二个声音,因为我遇到了重新定义错误。

啊?您是否实现了该方法两次或其他内容,而不是仅比较方法主体中的玩家对象?

如果您显示收到的确切错误消息,将会有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当声音在 AVAudioPlayer 中播放完毕时执行操作吗? 的相关文章

  • 为什么我们需要一个临时对象?

    正如我在许多示例中看 到的那样 首先它们为临时对象分配内存 然后将同一对象分配给 self 例如 我这里有一个代码片段 void viewDidLoad super viewDidLoad Movie newMovie Movie allo
  • 不使用 ARC 时替换 __weak

    我有这行代码 weak NSBlockOperation weakOperation operation 这是触发此编译器错误 weak attribute cannot be specified on automatic variable
  • 通过 UDP 从服务器向 iPhone 发送大量实时处理的数据

    我正在实施一个远程应用程序 服务器将实时处理和渲染数据作为动画 准确地说 是一系列图像 每次渲染图像时 都会通过 UDP 将图像传输到接收 iPhone 客户端 我研究了一些UDP 我知道以下几点 UDP 的最大大小约为 65k 不过 iP
  • 制作我自己的UIControlEvent,并触发它?

    我在视图控制器中创建了一个自定义的 UIView 类 我想让这个类可重用 所以不想将其构建为仅与这 1 个视图控制器一起使用 正因为如此 我think告诉我的视图控制器用户已与该类交互的最好方法是以某种方式创建我自己的 UIControlE
  • IOS 低功耗蓝牙连接间隔

    我正在为蓝牙低功耗设备构建一个应用程序 我想弄清楚如何设置iPhone端的连接间隔 我在外围设备上定义了最小和最大连接间隔 但是 我希望 iPhone 应用程序使用尽可能长的连接间隔 有什么方法可以在应用程序中设置连接间隔或者至少有一种方法
  • iPhone5 兼容性标签栏位于屏幕底线上方 88 点

    我通过添加以下内容添加了 iPhone 5 兼容性 电子邮件受保护 cdn cgi l email protection但是 我的应用程序中的选项卡栏并未按预期位于底部 该标签栏是通过 mainWindow xib 中的 tabbarCon
  • 当具有 PagingEnabled 的 UIScrollView 更改页面时如何更新 UIPageControl?

    我该如何做到这一点 以便当用户滚动到 UIScrollView 中的新页面时 UIPageControl 更新 我以前用过这个 看起来效果很好 请务必将 UIScrollView delegate 设置为 self pragma mark
  • 使用线程安全单例初始化代码时代码执行停止

    为了利用全局变量和方法 我实现了 Singleton 作为一种健康的编码实践 我跟着苹果文档 http www johnwordsworth com 2010 04 iphone code snippet the singleton pat
  • iOS 5.0中不调用UINavigationBar的drawRect

    我已经覆盖 放置在类别中 或混合 UINavigationBar 的drawRect 以显示自定义背景 在 iOS 5 中它不起作用 我应该怎么办 为导航栏设置自定义背景以支持 iOS 5 和 iOS 4 http www mladjana
  • 不使用 MFMailComposeViewController 发送邮件

    我想从 iPhone 应用程序发送邮件而不显示MFMailComposeViewController 我还希望从用户的默认邮件帐户发送此邮件 是否有可能做到这一点 iPhone SDK 不支持这一点 可能是因为 Apple 不希望您这样做
  • 导出 iPhone 地址簿数据库的可能方法

    我想将 Phone s Address Book sqlite db 导出到我的 iPhone 应用程序中 我在网上搜索过 但一切似乎都在 ABAddressBook 上迭代 但我想知道是否可以以编程方式将 Phone s Address
  • 将 NSDecimalNumber 转换为 NSString

    我正在从如下所示的对象中检索密钥 po obj TypeID 3 TypeName Asset 键值的检索方式如下 NSString typeId NSString obj objectForKey TypeID typeId 不是 NSS
  • iPhone——是否可以在 Xcode 调试器中检查 UIView 的框架?

    当调试器在断点处停止时 我在那里找不到任何 UIView 的框架 是否有可能做到这一点 编辑 由于缺乏回应而开始赏金 需要明确的是 我正在寻找一种在不添加额外调试代码的情况下查看框架的方法 另外 如果答案是 不 你不能这样做 赏金将用于解释
  • 进入/退出编辑模式时重绘 UITableViewCell

    我有一个表格视图 其中根据表格是否正在编辑 单元格的构建方式有所不同 具体来说 处于编辑模式时选择样式为无 非编辑模式时选择样式为蓝色 当我从一个单元转换到另一个单元时 我注意到某些单元格没有更新 快速的日志记录告诉我 即使单元格的外观发生
  • 在 iOS 模拟器主屏幕或锁屏中设置壁纸(背景图像)

    当我正在绞尽脑汁思考某件事时 我的脑海中突然出现了这个想法 我们可以将模拟器的主屏幕背景图像设置为我们选择的图像吗 Xcode 3 2 2 模拟器 3 1 3 就是一个很好的例子 Certain versions of the simula
  • AVAudioPlayer只能在模拟器中播放,但不能在设备中播放,为什么?! (iPhone-SDK)

    我有以下播放声音的简单代码 NSString soundPath NSBundle mainBundle pathForResource sound ofType wav player AVAudioPlayer alloc initWit
  • 创建类似于邮件应用程序菜单的 iPhone 弹出菜单

    当您想要回复消息时 我想创建一个类似于邮件应用程序中的弹出菜单 我在多个应用程序中看到过这一点 所以我不确定框架中是否内置了某些内容或一些示例代码 在 Swift 中创建操作表 代码已使用 Swift 5 进行测试 从 iOS 8 开始 U
  • iPhone iOS 保存从 UIImageJPEGRepresentation() 获得的数据第二次失败:ImageIO: CGImageRead_mapData 'open' failed

    我的 UIImage 操作遇到了一个奇怪的问题 我正在进行保管箱同步 并且必须将我的图像存储为本地文件 为此 我使用以下命令保存它们UIImagePNGRepresentation image or UIImageJPEGRepresent
  • 如何建立辅助NSSortDescriptor排序键?

    我已成功按排序键对数据进行排序lastName 但我想知道如何排序lastName 然后由firstName 这是我用来排序的代码lastName NSSortDescriptor sortDescriptor NSSortDescript
  • iPhone 中的 NSNotification

    我正在将 NSSNotifcation 发送到 iPhone 应用程序中的另一个视图控制器 但它的观察者方法收到两次通知 这可能如何 任何人都可以指导我 我已经使用此代码来发布通知 NSNotificationCenter defaultC

随机推荐