2015-03-11 14 views

Odpowiedz

18

Najprostszym sposobem jest importować ramy AudioToolbox jak tak #import <AudioToolbox/AudioToolbox.h> to wszystko co musisz zrobić to:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Beep" ofType:@"mp3"]; 
SystemSoundID soundID; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID); 
AudioServicesPlaySystemSound(soundID); 

To jest najlepsza dla bardzo krótkich dźwięków podobnych dźwięków itp bo przyzwyczajenie mieć większą kontrolę nad brzmi jak byś z AVAudioPlayer.

+1

Dzięki. Wydaje się, że dla krótkich dźwięków jest to najlepsza metoda. – Kyle

+0

Bardzo ładna odpowiedź :-) – Josef

+1

mp3 przerywa inne dźwięki i nie można grać z innymi. Zamiast tego możesz potrzebować caf. – durazno

10

Zastosowanie AVAudio - więcej kodować że używając AudioToolbox ale jest również bardziej elastyczne (jeśli jest to potrzebne w przyszłości)

0.

#import <AVFoundation/AVFoundation.h> 

1.

//conform to delegate and make a property 
@interface ViewController() <AVAudioPlayerDelegate> 
@property (nonatomic, strong) AVAudioPlayer *audioplayer; //the player 
@end 

2.

//have a lazy property for the player! where you also tell it to load the sound 
#define YourSound @"sound.caf" 
- (AVAudioPlayer *)audioplayer { 
    if(!_audioplayer) { 
     NSURL *audioURL = [[NSBundle mainBundle] URLForResource:YourSound.stringByDeletingPathExtension withExtension:YourSound.pathExtension]; 
     NSData *audioData = [NSData dataWithContentsOfURL:audioURL]; 
     NSError *error = nil; 
     // assing the audioplayer to a property so ARC won't release it immediately 
     _audioplayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error]; 
     _audioplayer.delegate = self; 
    } 
    return _audioplayer; 
} 

3.

//play 
- (void)action { 
    [self.audioplayer play]; 
} 
+0

Dzięki, to działa dla mnie. Ale na krótkie audio spróbuję funkcji C. – Kyle

+0

dziękuję za pracę dla mnie – BoSoud

2
#Import Avfoundation.Framework 

AVAudioPlayer *_audioPlayer; //Declare Globally 

Nsstring *path =[Nsstring stringwithformat:@"%@/Beep.mp3",[[Nsbundle mainBundle] resourcePath]];   //Give Your Local Path 

Nsurl *soundUrl= [Nsurl FileUrlWithPath:path]; 

_audioPlayer=[[AvAudioPlayer alloc] initwithContents ofurl:soundUrl error:nil]; 

_audioPlayer.volume=1.0; //Give Volume 

_audioPlayer.numberOfLoops=loop; //Give number of loop for repeating sound 

_audioPlayer.enableRate = Yes; 

_audioPlayer.rate=2.0f; //Give for fast playing sound. default value is 1.0f; 

[_audioPlayer Play]; 
Powiązane problemy