代码之家  ›  专栏  ›  技术社区  ›  Evelyn

在avaudioplayer中播放完声音后执行操作?

  •  18
  • Evelyn  · 技术社区  · 16 年前

    我正在使用avaudioplayer框架,我有几个声音一次播放一个。当声音播放完后,我希望应用程序做一些事情。我试着用audioplayerdifinishplaying在第一个声音结束时做这个动作,但是我不能用它来播放第二个声音,因为我有一个重新定义错误。我可以用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];
    
    }
    }
    
    2 回复  |  直到 8 年前
        1
  •  24
  •   Peter Hosey    16 年前

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

    然后将自己设定为代表并响应 audioPlayerDidFinishPlaying:successfully: .

    在您展示的代码片段中,您已经完成了步骤2,但没有完成步骤1:您没有将自己设置为代理。

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

    脑部放屁?这个方法和我演奏声音没有任何关系。

    _但我不能将其用于第二个声音,因为我有一个重新定义错误。

    呵呵?您是否实现了该方法两次或其他方法,而不仅仅是比较方法体中的player对象?

    如果你显示了你得到的准确的错误信息,这会有所帮助。

        2
  •  2
  •   Alex Bailey Bogdan Farca    8 年前

    斯威夫特3:

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