iPhone教程 播放声音文件

移动开发 iOS
本文介绍的是iPhone教程 播放声音文件,本文的目的是要让你能够在自己的游戏中使用音乐,来看详细内容。

iPhone教程 播放声音文件是本文要介绍的内容,本文演示如何使用Objective-C开发播放mp3文件的iPhone程序,当然本文目的不是要让你做一个iPhone版的播放器,因为这根本用不着你,iPod程序已经很好了。本文的目的是要让你能够在自己的游戏中使用音乐。

效果图如下:

1.打开xcode,创建一个名为TalkingDemo的View-based Application类型的iPhone程序。

2.如果要使用播放声音的功能,一定要引入AVFoundation库,右击项目中的Frameworkds目录,从菜单中选择Add->Existing Frameworkd,下图所示:

iPhone教程 播放声音文件

此操作将打开浏览库的对话框,我们选择名为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教程 播放声音文件得到内容介绍我那了,希望本文对你有所帮助。

本文转自:http://plter.com/?p=354

责任编辑:zhaolei 来源: 互联网
相关推荐

2012-12-27 14:29:38

Android开发流媒体

2013-04-08 09:31:05

iOS开发系统声音服务

2013-06-14 17:28:11

Windows PhoWP开发播放声音

2010-01-11 17:30:40

VB.NET播放声音

2011-08-08 10:23:41

iPhone 流播放 文件

2009-09-22 14:20:39

C#播放声音

2018-08-17 22:00:22

Linux输出设备音频

2011-08-08 18:19:09

iPhone音频播放

2018-08-16 14:40:53

Linux输出设备播放声音

2013-05-21 14:10:11

Android游戏开发SoundPool类同时多音效

2011-07-08 20:32:57

iPhone midi

2011-08-03 17:44:57

iPhone App 文件

2010-01-15 18:50:25

VB.NET资源文件

2022-06-01 08:21:24

Java内存API

2011-08-22 12:01:38

iPhone开发文件

2011-07-27 17:24:31

iPhone NSXMLParse XML

2011-08-10 11:12:33

iPhone文件

2011-08-10 15:58:58

iPhone视频

2011-11-25 13:52:05

Windows Pho声音播放

2009-04-22 08:57:25

Windows 7微软操作系统
点赞
收藏

51CTO技术栈公众号