Avaudioplayer不响应停止命令



我当前正在尝试为avaudioplayer使用自定义类,并且所有这些都非常适合播放音频文件,但是在停止上述文件时,它只是跳过停止命令和在当前文件的顶部播放

如果有人对为什么会发生这种情况有任何想法,我真的很感谢您的帮助。

下面是我的AudioPlayer1.h文件:

#import <Foundation/Foundation.h>
#import "AVFoundation/AVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>
@interface AudioPlayer1 : NSObject <AVAudioPlayerDelegate> {
    AVAudioPlayer   *player;
    BOOL playing;
    NSTimeInterval duration;        
}
@property (nonatomic, assign) AVAudioPlayer *player;
@property(readonly, getter=isPlaying) BOOL playing;
@property (readonly) NSTimeInterval duration;

-(void)GetSoundFileDuration:(NSString *) sound_file_name;
-(void)play;
-(void)stop;
-(void)setVolume:(float) volume;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)PlaySoundFile:(NSString *) sound_file_name;
- (void)notPlaying;
@end

以下是AudiOplayer1.m文件的内容:

#import "AudioPlayer1.h"
@implementation AudioPlayer1
@synthesize player;
@synthesize duration;
@synthesize playing;
- (id)init
{
self = [super init];
if (self != nil)
{
    player = [[AVAudioPlayer alloc] init];
    [player initWithContentsOfURL:nil error:nil];
    player.delegate = self;
}
return self;
}
- (void) setVolume:(float)volume
{
[player setVolume:volume];
}
- (void)PlaySoundFile:(NSString *) sound_file_name
{
[player stop];
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:@"mp3"]];
[player initWithContentsOfURL:sound_file error:nil];
[player prepareToPlay];
playing = YES;
[sound_file release];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"audioPlayerDidFinishPlaying");
playing = NO;
}

- (void) play
{
NSLog(@"Play Called");
[player setVolume:0.1];
[player play];
playing = YES;
}
- (void) stop
{
NSLog(@"Stop Called");
[player setVolume:0.0];
[player stop];
playing = NO;
}

-(void)GetSoundFileDuration:(NSString *) sound_file_name
{
NSLog(@"%@",duration);
}
-(void) notPlaying
{
playing = NO;
}
@end

及以下是将启动音频的代码:

- (IBAction)WPNaritive01:(id)sender
{
AudioPlayer1 * player = [[AudioPlayer1 alloc] init ];
if (player.playing == YES) 
{
    [player stop];
}
if (player.playing == NO)
{
    [player PlaySoundFile:@"1"];
    [player setVolume:0.1];
    [player play];
}
}

我仍在学习时对代码的奇数布局表示歉意。我只有现在就发现了从阅读另一个问题的偶然地完成这些课程的信息。大声笑

好吧,我已经弄清楚了。

,而不是像我在上面的代码中那样一遍又一遍地创建对象,我应该是Alloc并在ViewDidload中启动播放器,而不是像我一样在IBACTION中进行。

这是我所做的,因为其他人都遇到了同样的问题:

lockonLocation.h

@interface LockOnLocation : UIViewController {
AudioPlayer1 *player;
...

lockonLocation.m

- (void)viewDidLoad
player = [[AudioPlayer1 alloc] init ];
...

我使用了与上面相同的代码播放音频,所以这是正确的。

如果有人对我或我想收到您的其他任何其他成员有其他建议...

最新更新