Pergunta

Eu estou usando o framework AVAudioPlayer, e eu tenho vários sons que desempenham um de cada vez. Quando um som é terminar de jogar, eu quero o aplicativo para fazer alguma coisa. Eu tentei usar audioPlayerDidFinishPlaying para fazer a ação no final do primeiro som, mas eu não poderia usar isso para o segundo som, porque eu tenho um erro de redefinição. Posso usar NSTimeInterval para obter a duração de um som?

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

}
}
Foi útil?

Solução

Quando um som é jogo acabado, eu quero o aplicativo para fazer alguma coisa.

Em seguida, definir-se como delegado e responder aos audioPlayerDidFinishPlaying:successfully:.

No trecho de código que você mostrou, você fez o passo 2, mas não passo 1: Você não está definindo-se como o delegado

.

Eu tentei usar applicationDidFinishLaunching para fazer a ação no final do primeiro som ...

O cérebro fart? Esse método não tem nada que eu possa ver a ver com reprodução de sons.

..., mas eu não poderia usar isso para o segundo som, porque eu tenho um erro de redefinição.

Huh? Você implementar o método duas vezes ou algo em vez de simplesmente comparar o objeto jogador no corpo do método?

Seria bom se você mostrou a mensagem de erro exata que você tem.

Outras dicas

Swift 3:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        print("sound file finished")
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top