加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Xcode中的循环音频

发布时间:2020-12-14 18:10:56 所属栏目:百科 来源:网络整理
导读:我在我的应用程序中播放声音,我想循环播放.我对此的研究并没有完全解决我的问题. 我的代码.m CFBundleRef mainBundle = CFBundleGetMainBundle();CFURLRef soundFileURLRef;soundFileURLRef = CFBundleCopyResourceURL(mainBundle,(CFStringRef) @"ar4",CFST
我在我的应用程序中播放声音,我想循环播放.我对此的研究并没有完全解决我的问题.

我的代码.m

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle,(CFStringRef) @"ar4",CFSTR 
                                          ("wav"),NULL);


UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundID);
AudioServicesPlaySystemSound (soundID);

我想我需要把这样的东西放进去

numberOfLoops = -1;

但不确定如何在我的代码中实现,因为我收到numberOfLoops的未声明错误.一些建议将非常感谢

解决方法

这样的事情应该可以解决你的问题:

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.mp3"];
    NSLog(@"Path to play: %@",resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                 [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@",[err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        player.delegate = self;
        [player play];
        player.numberOfLoops = -1;
        player.currentTime = 0;
        player.volume = 1.0;
    }

然后,如果你想阻止它:

[player stop];

或暂停:

[player pause];

并将其导入头文件中:

#import <AVFoundation/AVFoundation.h>.

你应该在你的标题中声明它,然后合成它.

//.h并添加粗体部分:

@interface ViewController:UIViewController< AVAudioPlayerDelegate> {

AVAudioPlayer *player;
}
@property (nonatomic,retain) AVAudioPlayer *player;

//.m

@synthesize player;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读