The example analyzes the application of system sound and custom sound in the development of iOS

  • 2020-05-10 22:58:54
  • OfStack

1. Access sound services

Add the framework AudioToolBox and the sound file to play, and import the interface file of the framework in the class that implements the sound service:
#import < AudioToolbox/AudioToolbox.h >

To play the system sound, you need two functions, AudioServicesCreateSystemSoundID and AudioServicesPlaySystemSound, and you need to declare a variable of type SystemSoundID, which represents the sound file to use.


-(IBAction) playSysSound:(id)sender { 
         
        SystemSoundID sourceID;
        // call NSBundle Methods of a class mainBundle return 1 a NSBundle Object that corresponds to the current program executable 2 The directory to which the base file belongs
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
        //1 That points to the location of the file CFURLRef The objects and 1 A point to which to set SystemSoundID Pointer to variable
        AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
        AudioServicesPlaySystemSound(soundID); 
    }

2. Sound and vibration

1. Reminder sound

And the system sound difference:

If the phone is silent, the sound will automatically trigger a vibration.

The function needed to play the alert is AudioServicesPlayAlertSound instead of AudioServicesPlaySystemSound.

2, vibration

Simply call the AudioServicesPlaySystemSound() method and pass in the kSystemSoundID_Vibrate constant.

If the device does not support vibration (iPad 2, for example), that is fine, but it does not.

3. AVFoundation framwork

The AVAudioPlayer class can be used for compressed Audio files, or audio files that are longer than 30 seconds.

1. AVAudioPlayer also needs to know the path of audio files;

2. The AVAudioPlayerDelegate corresponding to this class has two delegate methods:

1) audioDidFinishPlaying:successfully: triggered when the audio is played;

2) audioPlayerEndInterruption: triggered when the program is interrupted by the outside of the application and returned to the application.

4. MediaPlayer framwork

You can use MPMoviePlayerController to play movie files (like H.264, MPEG-4 Part2 video), and you can also play video files from the Internet.

5. Call and customize the sound effect instance instance
Requirements can be broadly divided into three categories:
1. The vibration
2. System sound (no need to provide audio files)
3. Custom sound effects (audio files are required)


Encapsulation of my tool class:


// 
//  WQPlaySound.h 
//  WQSound 
// 
//  Created by Read her on 12-7-20. 
//  Copyright (c) 2012 years __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
 
@interface WQPlaySound : NSObject 

    SystemSoundID soundID; 

 
/**
 *  @brief  Class for playback vibration effect
 *
 *  @return self
 */ 
-(id)initForPlayingVibrate; 
 
/**
 *  @brief  Initializes sound effects for the playback system ( No audio file is required )
 *
 *  @param resourceName System sound name
 *  @param type System sound type
 *
 *  @return self
 */ 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type; 
 
/**
 *  @brief  Initialization for playback of specific audio files (audio files are required)
 *
 *  @param filename Audio file name (added to project)
 *
 *  @return self
 */ 
-(id)initForPlayingSoundEffectWith:(NSString *)filename; 
 
/**
 *  @brief  Play sound
 */ 
-(void)play; 
 
@end 


// 
//  WQPlaySound.m 
//  WQSound 
// 
//  Created by Read her on 12-7-20. 
//  Copyright (c) 2012 years __MyCompanyName__. All rights reserved. 
// 
 
#import "WQPlaySound.h" 
 
@implementation WQPlaySound 
 
-(id)initForPlayingVibrate 

    self = [super init]; 
    if (self) { 
        soundID = kSystemSoundID_Vibrate; 
    } 
    return self;     

 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type 

    self = [super init]; 
    if (self) { 
        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type]; 
        if (path) { 
            SystemSoundID theSoundID; 
            OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID); 
            if (error == kAudioServicesNoError) { 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
         
    } 
    return self; 

 
-(id)initForPlayingSoundEffectWith:(NSString *)filename 

    self = [super init]; 
    if (self) { 
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 
        if (fileURL != nil) 
        { 
            SystemSoundID theSoundID; 
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 
            if (error == kAudioServicesNoError){ 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
    } 
    return self; 

 
-(void)play 

    AudioServicesPlaySystemSound(soundID); 

 
-(void)dealloc 
{  
    AudioServicesDisposeSystemSoundID(soundID); 

@end 

Call method steps:
1. Add AudioToolbox.framework to the project
2. Call the WQPlaySound utility class
2.1 shake

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate]; 
[sound play]; 

2.2 system sound, taking Tock as an example

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"]; 
[sound play]; 

2.3 customize the sound effects and add tap.aif audio files to the project

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"]; 
[sound play]; 


Related articles: