Xcode中如何制作只要按住按钮就循环播放声音?

发布于 2024-12-05 04:59:30 字数 91 浏览 1 评论 0原文

那么,如何让声音文件仅在(iOS)中按住某个按钮时播放和循环?当松开按钮时我还需要它停止吗?我尝试搜索谷歌,只找到了有人提问的论坛,但没有关于如何做到这一点的真正答案。

So, how would I make a sound file play and loop only while a certain button is held in (iOS) ? I would also need it to stop when the button was let go? I have tried searching google and have only found forums with people asking but no real answers on how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2024-12-12 04:59:30

使用按钮控制事件 touchdown、touchupinside 和 touchupoutside:

UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(40.0, 40.0, 200.0, 30.0)];
[theButton setTitle:@"button" forState:UIControlStateNormal];
[theButton setBackgroundColor:[UIColor redColor]];
[theButton addTarget:self action:@selector(startMusic) forControlEvents:UIControlEventTouchDown];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpInside];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpOutside];

编辑:
意识到您的问题可能更多是关于播放声音文件?!

有不同的框架,您可以使用 AVAudioPlayer:

导入框架并定义播放器,

#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *newPlayer;

为主文件中的按钮创建 ibactions:

-(IBAction)startMusic{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySoundFile"
                                                          ofType: @"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    newPlayer.numberOfLoops = -1; // Loop indefinately
    [newPlayer play];
}

-(IBAction)stopMusic{
    if(newPlayer isPlaying){
        [newPlayer stop];
    }
    [newPlayer release];
}

use a buttons control events touchdown, touchupinside and touchupoutside:

UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(40.0, 40.0, 200.0, 30.0)];
[theButton setTitle:@"button" forState:UIControlStateNormal];
[theButton setBackgroundColor:[UIColor redColor]];
[theButton addTarget:self action:@selector(startMusic) forControlEvents:UIControlEventTouchDown];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpInside];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpOutside];

edit:
realized your question may have been more about playing sound files?!

there's different frameworks, you could use AVAudioPlayer:

import framework and define a player

#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *newPlayer;

create your ibactions for buttons in the main file:

-(IBAction)startMusic{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySoundFile"
                                                          ofType: @"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    newPlayer.numberOfLoops = -1; // Loop indefinately
    [newPlayer play];
}

and

-(IBAction)stopMusic{
    if(newPlayer isPlaying){
        [newPlayer stop];
    }
    [newPlayer release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文