سؤال

أنا أستخدم إطار avaudioplayer، ولدي العديد من الأصوات التي تلعب واحدة في وقت واحد. عند انتهاء الصوت، أريد أن أفعل التطبيق شيئا ما. حاولت استخدام Audioplayerdiddidfinishplaying للقيام بالإجراء في نهاية الصوت الأول، لكنني لم أستطع استخدام ذلك للصوت الثاني لأنني حصلت على خطأ في إعادة التعريف. هل يمكنني استخدام 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 للقيام بالعمل في نهاية الصوت الأول ...

ضرطة الدماغ؟ هذه الطريقة لا تحتوي على أي شيء أستطيع أن أرى القيام به مع تشغيل الأصوات.

...، لكنني لم أستطع استخدام ذلك للصوت الثاني لأنني حصلت على خطأ إعادة التعريف.

هاه؟ هل قمت بتنفيذ الطريقة مرتين أو شيء ما بدلا من مقارنة كائن اللاعب في جسم الطريقة؟

سيساعد ذلك إذا أظهرت رسالة الخطأ الدقيقة التي حصلت عليها.

نصائح أخرى

SWIFT 3:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        print("sound file finished")
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top