gpt4 book ai didi

ios - 使用 AudioToolbox.framework 循环播放音频

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:56 25 4
gpt4 key购买 nike

我想在我的应用程序启动时播放音频,但首先,音频无法播放!不管我怎么编码。其次,如果可以的话,我很想循环播放它。

@implementation ViewController

-(void)awakeFromNib
{
SystemSoundID soundID;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:@"ScaryMusic" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);

AudioServicesPlaySystemSound(soundID);
NSLog(@"Sound Played");

}

最佳答案

试试这个:

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"ScaryMusic" withExtension:@"mp3"];
if (soundFileURL != nil)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
}

[player play];

这对 mp3 等更好。 SystemSound 主要用于短 .wav 类型的声音。

关于ios - 使用 AudioToolbox.framework 循环播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17605602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com