Question

J'utilise le cadre de AVAudioPlayer, et j'ai plusieurs sons qui jouent un à la fois. Lorsqu'un son est fini de jouer, je veux l'application de faire quelque chose. J'ai essayé d'utiliser audioPlayerDidFinishPlaying pour faire l'action à la fin du premier son, mais je ne pouvais pas l'utiliser pour le second son parce que je suis une erreur de redéfinition. Puis-je utiliser NSTimeInterval pour obtenir la durée d'un son?

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

}
}
Était-ce utile?

La solution

  

Quand un son est fini de jouer, je veux l'application de faire quelque chose.

Ensuite, placez-vous comme délégué et répondre à audioPlayerDidFinishPlaying:successfully:.

Dans le extrait de code que vous avez montré, vous avez fait l'étape 2, mais pas l'étape 1. Vous n'êtes pas vous fixer comme le délégué

  

J'ai essayé d'utiliser applicationDidFinishLaunching pour faire l'action à la fin du premier son ...

péter le cerveau? Cette méthode n'a rien que je peux voir à faire avec des sons de jeu.

  

..., mais je ne pouvais pas l'utiliser pour le second son parce que je suis une erreur de redéfinition.

Huh? Avez-vous mettre en œuvre la méthode deux fois ou quelque chose au lieu de simplement comparer l'objet de joueur dans le corps de la méthode?

Il serait utile que vous montriez le message d'erreur exact que vous avez.

Autres conseils

Swift 3:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        print("sound file finished")
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top