1、聲音播放
聲音播放 NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &soundID); AudioServicesPlaySystemSound (soundID);
2、音頻播放+震動
1. #import2. #include 3. 4. @interface PlaySoundDemo1ViewController : UIViewController { 5. IBOutlet UISwitch *swcallback; 6. IBOutlet UIPickerView *soundPicker; 7. NSArray *soundData; 8. SystemSoundID soundFileObject; 9. } 10. 11. @property (nonatomic,retain) UISwitch *swcallback; 12. @property (nonatomic,retain) UIPickerView *soundPicker; 13. @property (nonatomic,retain) NSArray *soundData; 14. @property (readonly) SystemSoundID soundFileObject; 15. 16. static void completionCallback (SystemSoundID mySSID, void* myself) ; 17. - (IBAction) playSystemSound; 18. - (IBAction) playAlertSound; 19. - (IBAction) vibrate; 20. -(IBAction) stopplaysound; 21. -(void) GetPlaysound; 22. 23. @end 複製代碼 PlaySoundDemo1ViewController.m 1. //----------- 限制 ------------ 2. //聲音長度要小於 30 秒 3. // In linear PCM 或者 IMA4 (IMA/ADPCM) 格式的 4. //需為 .caf, .aif, 或者 .wav 的檔案 5. //不能控制播放的進度 6. //調用方法後立即播放聲音 7. //沒有循環播放和立體聲控制 8. //參考資料: 9. //http://blog.xiang.li/2009/02/iphone-dev-audio-play/ 10. //https://developer.apple.com/iphone/library/samplecode/SysSound/ 11. 12. #import "PlaySoundDemo1ViewController.h" 13. 14. @implementation PlaySoundDemo1ViewController 15. 16. @synthesize swcallback,soundPicker,soundData,soundFileObject; 17. 18. //停止當前音效的播放 19. -(IBAction) stopplaysound{ 20. AudioServicesRemoveSystemSoundCompletion (self.soundFileObject); 21. } 22. 23. //建立聲音物件 24. -(void) GetPlaysound{ 25. [self stopplaysound]; 26. 27. //取出使用者所選擇的音效項目 28. NSInteger row=[ soundPicker selectedRowInComponent:0]; 29. 30. //指定不同的音效檔 31. NSString *soundfilename; 32. switch (row) { 33. case 0: 34. soundfilename=@"sound1.aiff"; 35. break; 36. case 1: 37. soundfilename=@"sound2.aiff"; 38. break; 39. case 2: 40. soundfilename=@"sound3.aiff"; 41. break; 42. default: 43. break; 44. } 45. 46. 47. NSString *Path=[[NSBundle mainBundle] bundlePath]; 48. NSURL *soundfileURL=[NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]]; 49. 50. //建立音效物件 51. AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject); 52. 53. //判斷是否連續播放 54. if ([swcallback isOn]){ 55. // Add sound completion callback 56. AudioServicesAddSystemSoundCompletion (soundFileObject, NULL, NULL,completionCallback,(void*) self); 57. } 58. } 59. 60. //當音效播放完畢後的處理方式,這裡設定為再一次播放 61. static void completionCallback (SystemSoundID mySSID, void* myself) { 62. AudioServicesPlaySystemSound(mySSID); 63. } 64. 65. - (IBAction) playSystemSound{ 66. [self GetPlaysound]; 67. AudioServicesPlaySystemSound (self.soundFileObject); 68. // AudioServicesDisposeSystemSoundID (self.soundFileObject); 69. } 70. 71. -(IBAction) playAlertSound{ 72. [self GetPlaysound]; 73. //播放音效 74. AudioServicesPlayAlertSound (self.soundFileObject); 75. } 76. - (IBAction) vibrate{ 77. //震動 78. AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); 79. } 80. 81. - (void)viewDidLoad { 82. [super viewDidLoad]; 83. 84. //建立音效清單陣列 85. NSArray *array=[[NSArray alloc] initWithObjects:@"音效1",@"音效2",@"音效3",nil]; 86. self.soundData=array; 87. [array release]; 88. } 89. - (void)dealloc { 90. [super dealloc]; 91. AudioServicesDisposeSystemSoundID (self.soundFileObject); 92. } 93. #pragma mark - 94. #pragma mark soundPicker Data Soure 95. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 96. return 1; 97. } 98. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 99. return [soundData count]; 100. } 101. #pragma mark - 102. #pragma mark soundPicker Delegate 103. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 104. return [soundData objectAtIndex:row]; 105. } 106. 107. AudioToolbox.frameworkAudioToolbox.framework
3、MP3 播放
1)、背景音樂播放 支持mp3格式 循環播放長音樂 這種播放音樂的方式導入框架#import ; NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //創建音樂文件路徑 NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath]; AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil]; // 創建播放器 self.myBackMusic = thePlayer; //賦值給自己定義的類變量 [musicURL release]; [thePlayer release]; [myBackMusic prepareToPlay]; [myBackMusic setVolume:1]; //設置音量大小 myBackMusic.numberOfLoops = -1;//設置音樂播放次數 -1為一直循環 if (mainMusicStatus) { [myBackMusic play]; //播放 } 2)、按鈕播放聲音 需要導入框架#import NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"]; //創建音樂文件路徑 CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath]; AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID); //變量SoundID與URL對應 AudioServicesPlaySystemSound(sameViewSoundID); // 播放SoundID聲音
4、視頻播放
視頻播放 #import#import @interface MoveTestViewController : UIViewController { NSURL *movieURL; MPMoviePlayerController *moviePlayer; } @property(nonatomic,retain)NSURL *movieURL; @property(nonatomic,retain)MPMoviePlayerController *moviePlayer; -(void)initAndPlayMovie:(NSURL *)movieURL; -(NSURL *)localMovieURL; -(IBAction)ok; @end // // MoveTestViewController.m // MoveTest // // Created by zhen hu on 10/20/10. // Copyright ww 2010. All rights reserved. // #import "MoveTestViewController.h" @implementation MoveTestViewController @synthesize movieURL; @synthesize moviePlayer; -(IBAction)ok{ //獲得工程中電影文件的路徑 NSURL *url=[self localMovieURL]; //播放 [self initAndPlayMovie:url]; } -(void)initAndPlayMovie:(NSURL *)movUrl { // 創建播放器 MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movUrl]; if (mp)//如果不為空 { self.moviePlayer = mp; [mp release]; [self.moviePlayer play];//播放對象 //4.0專用 self.moviePlayer.view.frame = CGRectMake(0, 0, 320, 300); [self.view addSubview:moviePlayer.view]; } } //-(void)initAndPlayNetMovie:(NSURL *)movieURL //{ // // MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; // if (mp) // { // self.musicPlayer=mp; // [mp release]; // // musicPlayer.view.frame = CGRectMake(0, 340, 320, 5); // [self.view addSubview:musicPlayer.view]; // // } // //} -(NSURL *)localMovieURL { if (movieURL == nil) { NSBundle *bundle = [NSBundle mainBundle]; if (bundle) {//得到文件絕對路徑地址 NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"]; //NSString *netPath=@"http://www.sina.com.cn/xx.m4v"; if (moviePath) {// 把路徑轉換為NSURL對象 self.movieURL = [NSURL fileURLWithPath:moviePath]; } } } return self.movieURL; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end