你如何让iPhone发出哔哔声? [英] How do you make an iPhone beep?

查看:220
本文介绍了你如何让iPhone发出哔哔声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么代码允许我在iPhone上产生标准的哔声?

What code allows me to produce a standard beep sound on the iPhone?

推荐答案

那取决于什么样的声音你想要的。

Well it depends on what kind of sound you want.

以下是使用AVFoundation音频框架播放声音的方法。

Here's how to play a sound using the AVFoundation audio framework.

#import <UIKit/UIKit.h>

        @class AVAudioPlayer;

        @interface AudioPlayer : UIViewController {
          IBOutlet UIButton *playButton;
          IBOutlet UIButton *stopButton;
          AVAudioPlayer *audioPlayer;
        }

        @property (nonatomic, retain) IBOutlet UIButton *playButton;
        @property (nonatomic, retain) IBOutlet UIButton *stopButton;
        @property (nonatomic, retain) AVAudioPlayer *audioPlayer;

        -(IBAction)play;
        -(IBAction)stop;

        @end

    - (void)viewDidLoad {
      [super viewDidLoad];

      // Get the file path to the song to play.
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TNG_Theme"
                                                           ofType:@"mp3"];

      // Convert the file path to a URL.
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

      //Initialize the AVAudioPlayer.
      self.audioPlayer = [[AVAudioPlayer alloc]
                               initWithContentsOfURL:fileURL error:nil];

      // Preloads the buffer and prepares the audio for playing.
      [self.audioPlayer prepareToPlay];

      [filePath release];
      [fileURL release];

    }

-(IBAction)play {

  // Make sure the audio is at the start of the stream.
  self.audioPlayer.currentTime = 0;

  [self.audioPlayer play];

}

-(IBAction)stop {

  [self.audioPlayer stop];

}

这篇关于你如何让iPhone发出哔哔声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆