【问题标题】:Do an action when a sound has finished playing in AVAudioPlayer?在 AVAudioPlayer 中播放完声音后执行操作?
【发布时间】:2010-11-09 20:20:32
【问题描述】:

我正在使用 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];

}
}

【问题讨论】:

    标签: iphone cocoa-touch nsdate avaudioplayer


    【解决方案1】:

    当声音播放完毕后,我希望应用程序做一些事情。

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

    在您展示的代码 sn-p 中,您完成了第 2 步,但没有完成第 1 步:您没有将自己设置为代表。

    我尝试用applicationDidFinishLaunching做第一个声音结尾的动作……

    脑子放屁?该方法与播放声音没有任何关系。

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

    嗯?您是否实现了两次或其他方法,而不是仅比较方法主体中的播放器对象?

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

    【讨论】:

    • 哦!是的,我完全想说的是 audioPlayerDidFinishPlaying,而不是 applicationDidFinishLaunching。对不起。我将代理设置为 self,它适用于第一个声音。但问题不在于 audioPlayerDidFinishPlaying 不起作用,问题是我不知道如何再次发出第二声。我有两种不同的声音。
    • 顺便说一句,当我输入 [player setDelegate: self] 时,我收到警告:“类 'ViewController' 没有实现 'AVAudioPlayerDelegate' 协议。”有问题吗?
    • 您需要将自己设置为两个声音的代表。声音会在向您发送委托消息时识别自己,因此您可以将其与您手头的声音进行比较。
    • 至于警告:它不应该破坏任何东西,但无论如何都要修复每个警告,这样你就不会得到 20 个你一直忽略的警告,隐藏了一些非常重要的东西。您需要将您的类声明为符合该协议。
    • 哦,好吧!这确实有效。谢谢。 :) 不过,我希望我能为不同的声音做不同的动作。我只能使用该方法对两种声音执行相同的操作。 (我还有一个问题:有什么方法可以让我使用 NSTimeInterval 来获取声音的持续时间?因为这个值在其他函数中对我非常有用。)
    【解决方案2】:

    斯威夫特 3:

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
            print("sound file finished")
        }
    

    【讨论】:

      猜你喜欢
      • 2015-12-22
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多