How do you make an iPhone beep?

前端 未结 2 1748
误落风尘
误落风尘 2021-01-16 09:52

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

相关标签:
2条回答
  • Well it depends on what kind of sound you want.

    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];
    
    }
    
    0 讨论(0)
  • 2021-01-16 10:39

    AudioServicesPlaySystemSound is one thing you can do for a simple sound.

    0 讨论(0)
提交回复
热议问题