美文网首页
iOS 播放短音效

iOS 播放短音效

作者: 114105lijia | 来源:发表于2019-10-03 13:35 被阅读0次
    SystemSoundID _soundID;
    NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"coin" ofType:@"mp3"]];
     AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundUrl, &_soundID);
    AudioServicesPlaySystemSound(_soundID);  
    //播放完释放
    AudioServicesDisposeSystemSoundID(_soundID);
    
    #import <AudioToolbox/AudioToolbox.h>
    #import <AVFoundation/AVFoundation.h>
    @property (nonatomic, strong) AVAudioPlayer *audioPlayer;
    
    NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"coin" ofType:@"mp3"]];
        
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    if (self.audioPlayer)
     {
          if ([self.audioPlayer prepareToPlay])
            {
                // 播放时,设置喇叭播放否则音量很小
                AVAudioSession *playSession = [AVAudioSession sharedInstance];
                [playSession setCategory:AVAudioSessionCategoryPlayback error:nil];
                [playSession setActive:YES error:nil];
            }
      }
    
    [self.audioPlayer play];
    

    相关文章

      网友评论

          本文标题:iOS 播放短音效

          本文链接:https://www.haomeiwen.com/subject/hussyctx.html