iPhone教程 播放音效檔

來源:互聯網
上載者:User

iPhone教程 播放音效檔是本文要介紹的內容,本文示範如何使用Objective-C開發播放mp3檔案的iPhone程式,當然本文目的不是要讓你做一個iPhone版的播放器,因為這根本用不著你,iPod程式已經很好了。本文的目的是要讓你能夠在自己的遊戲中使用音樂。

如下:

1.開啟xcode,建立一個名為TalkingDemo的View-based Application類型的iPhone程式。

2.如果要使用播放聲音的功能,一定要引入AVFoundation庫,右擊項目中的Frameworkds目錄,從菜單中選擇Add->Existing Frameworkd,所示:

此操作將開啟瀏覽庫的對話方塊,我們選擇名為AVFoundation.framework的庫,並把它添加進來。

3.修改TalkingDemoViewController.h檔案內容如下:

 
  1. #import <UIKit/UIKit.h>   
  2. #import <AVFoundation/AVFoundation.h>   
  3.  
  4. @interface TalkingDemoViewController : UIViewController {   
  5.     AVAudioPlayer *player;   
  6.  
  7. }   
  8.  
  9. -(IBAction)sayTalking:(id)sender;   
  10. @end 

4.雙擊TalkingDemoViewController.xib檔案開啟InterfaceBuilder,拖入一個Round Rect Button組件,並將這個組件分別綁定為btn如果你還不會綁定InterfaceBuilder組件到Objective-C代碼,請看iPhone按鈕的使用),然後將按鈕的標籤修改為“播放音樂”

5.修改TalkingDemoViewController.m檔案的內容如下所示:

 
  1. #import "TalkingDemoViewController.h"   
  2.  
  3. @implementation TalkingDemoViewController   
  4. // Implement viewDidLoad to do additiona    l setup after loading the view, typically from a nib.   
  5. - (void)viewDidLoad {   
  6.     if (player) {   
  7.         [player release];   
  8.     }   
  9.     NSString *soundPath=[[NSBundle mainBundle] pathForResource:@"intro" ofType:@"caf"];   
  10.     NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:soundPath];   
  11.     player=[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];   
  12.     [player prepareToPlay];   
  13.     [soundUrl release];   
  14.     [super viewDidLoad];   
  15. }   
  16.  
  17. -(IBAction)sayTalking:(id)sender   
  18. {   
  19.     NSLog(@"播放聲音");   
  20.     [player play];   
  21.  
  22. }   
  23. // Override to allow orientations other than the default portrait orientation.   
  24. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
  25.     return YES;   
  26. }   
  27.  
  28. - (void)didReceiveMemoryWarning {   
  29.     // Releases the view if it doesn’t have a superview.   
  30.     [super didReceiveMemoryWarning];   
  31.     // Release any cached data, images, etc that aren’t in use.   
  32. }   
  33.  
  34. - (void)viewDidUnload {   
  35.     // Release any retained subviews of the main view.   
  36.     // e.g. self.myOutlet = nil;   
  37. }   
  38.  
  39. - (void)dealloc {   
  40.     [player release];   
  41.     [super dealloc];   
  42. }   
  43. @end 

6.此代碼將播放一個名為 “intro.caf”的檔案,請將這個檔案加入到資源檔夾(Resources)中.

7.按Command+R運行此程式,嘗試驗擊“播放音樂”按鈕,就可以聽到播放的聲音了。

原始碼:http://easymorse-android.googlecode.com/svn/trunk/TalkingDemo/

小結:iPhone教程 播放音效檔得到內容介紹我那了,希望本文對你有所協助。

本文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.