自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone教程 播放聲音文件

移動(dòng)開(kāi)發(fā) iOS
本文介紹的是iPhone教程 播放聲音文件,本文的目的是要讓你能夠在自己的游戲中使用音樂(lè),來(lái)看詳細(xì)內(nèi)容。

iPhone教程 播放聲音文件是本文要介紹的內(nèi)容,本文演示如何使用Objective-C開(kāi)發(fā)播放mp3文件的iPhone程序,當(dāng)然本文目的不是要讓你做一個(gè)iPhone版的播放器,因?yàn)檫@根本用不著你,iPod程序已經(jīng)很好了。本文的目的是要讓你能夠在自己的游戲中使用音樂(lè)。

效果圖如下:

1.打開(kāi)xcode,創(chuàng)建一個(gè)名為T(mén)alkingDemo的View-based Application類(lèi)型的iPhone程序。

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

iPhone教程 播放聲音文件

此操作將打開(kāi)瀏覽庫(kù)的對(duì)話框,我們選擇名為AVFoundation.framework的庫(kù),并把它添加進(jìn)來(lái)。

3.修改TalkingDemoViewController.h文件內(nèi)容如下:

  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文件打開(kāi)InterfaceBuilder,拖入一個(gè)Round Rect Button組件,并將這個(gè)組件分別綁定為btn(如果你還不會(huì)綁定InterfaceBuilder組件到Objective-C代碼,請(qǐng)看iPhone按鈕的使用),然后將按鈕的標(biāo)簽修改為“播放音樂(lè)”

5.修改TalkingDemoViewController.m文件的內(nèi)容如下所示:

  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.此代碼將播放一個(gè)名為 “intro.caf”的文件,請(qǐng)將這個(gè)文件加入到資源文件夾(Resources)中.

7.按Command+R運(yùn)行此程序,嘗試點(diǎn)擊“播放音樂(lè)”按鈕,就可以聽(tīng)到播放的聲音了。

源代碼:http://easymorse-android.googlecode.com/svn/trunk/TalkingDemo/

小結(jié):iPhone教程 播放聲音文件得到內(nèi)容介紹我那了,希望本文對(duì)你有所幫助。

本文轉(zhuǎn)自:http://plter.com/?p=354

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2012-12-27 14:29:38

Android開(kāi)發(fā)流媒體

2013-04-08 09:31:05

iOS開(kāi)發(fā)系統(tǒng)聲音服務(wù)

2013-06-14 17:28:11

Windows PhoWP開(kāi)發(fā)播放聲音

2010-01-11 17:30:40

VB.NET播放聲音

2011-08-08 10:23:41

iPhone 流播放 文件

2009-09-22 14:20:39

C#播放聲音

2011-08-08 18:19:09

iPhone音頻播放

2018-08-17 22:00:22

Linux輸出設(shè)備音頻

2011-08-03 17:44:57

iPhone App 文件

2018-08-16 14:40:53

Linux輸出設(shè)備播放聲音

2013-05-21 14:10:11

Android游戲開(kāi)發(fā)SoundPool類(lèi)同時(shí)多音效

2011-07-08 20:32:57

iPhone midi

2011-07-27 17:24:31

iPhone NSXMLParse XML

2011-08-10 11:12:33

iPhone文件

2011-08-22 12:01:38

iPhone開(kāi)發(fā)文件

2022-06-01 08:21:24

Java內(nèi)存API

2010-01-15 18:50:25

VB.NET資源文件

2011-08-10 15:58:58

iPhone視頻

2011-11-25 13:52:05

Windows Pho聲音播放

2011-07-21 16:48:19

iPhone 游戲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)