Phone 播放 midi文件

本文详细介绍了如何在iPhone上利用FMOD库播放MIDI文件的过程,包括下载安装FMOD API,配置项目属性,处理DLS文件,并通过编写代码实现MIDI播放。

iPhone 播放 midi 文件方法是本文要介绍的内容,吸纳来看内容。iPhone 默认不带播放MIDI的框架,所以播放MIDI还得寻求第3方的库帮忙。这里使用的库就是大名鼎鼎的 FMOD ,许多火爆游戏使用的都是这个库。开发者可以免费下载使用。

首先下载安装 FMOD API FOR IPHONE:http://www.fmod.org/index.php/release/version/fmodapi42607iphone- installer.dmg。

安装后可以在目录中看到不少示范代码,可惜没有MIDI

自己写一个:)感谢强大的api,写起来异常轻松。

新建一个基于view项目

修改项目属性,添加 Other Linker Flags 为 -lfmodexL_$PLATFORM_NAME

添加 Header Search Paths :/Developer/FMOD Programmers API iPhone/api/inc (默认是这个位置,修改成自己FMOD安装的目录)

添加 Library Search Paths :/Developer/FMOD Programmers API iPhone/api/lib (同上)

把 appDelegate 修改成 .mm 的后缀

MIDI 播放需要一个 DLS 文件, 在osx 下没找到,这里使用了xp 自带的 gm.dls 文件(3M 有点大~),拷贝到项目中。

修改ViewController 代码如下 ,随便在xib文件中链接两个按钮action上即可

运行(真机有效)

主要代码

  1. //  
  2. // PlayMidiDemoViewController.m  
  3. // PlayMidiDemo  
  4. //  
  5. // Created by xhan on 9/9/09.  
  6. // Copyright In-Blue 2009. All rights reserved.  
  7. //  
  8. #import "PlayMidiDemoViewController.h"  
  9. @implementation PlayMidiDemoViewController  
  10. @synthesize status;  
  11. @synthesize time;  
  12. void ERRCHECK(FMOD_RESULT result)  
  13. {  
  14. if (result != FMOD_OK)  
  15. {  
  16. fprintf(stderr, "FMOD error! (%d) %s ", result, FMOD_ErrorString(result));  
  17. exit(-1);  
  18. }  
  19. }  
  20. - (void)viewDidLoad {  
  21. [super viewDidLoad];  
  22. system = NULL;  
  23. sound1 = NULL;  
  24. sound2 = NULL;  
  25. channel = NULL;  
  26. }  
  27. - (void)didReceiveMemoryWarning {  
  28. // Releases the view if it doesn't have a superview.  
  29.  
  30. [super didReceiveMemoryWarning];  
  31. // Release any cached data, images, etc that aren't in use.  
  32. }  
  33. - (void)viewDidUnload {  
  34. // Release any retained subviews of the main view.  
  35. // e.g. self.myOutlet = nil;  
  36. }  
  37. - (void)dealloc {  
  38. [status release], status = nil;  
  39. [time release], time = nil;  
  40. [super dealloc];  
  41. }  
  42. - (void)viewWillAppear:(BOOL)animated  
  43. {  
  44. FMOD_RESULT result = FMOD_OK;  
  45. char buffer[200] = {0};  
  46. unsigned int version = 0;  
  47. /*  
  48. Create a System object and initialize  
  49. */  
  50. result = FMOD::System_Create(&system);  
  51. ERRCHECK(result);  
  52. result = system->getVersion(&version);  
  53. ERRCHECK(result);  
  54. if (version < FMOD_VERSION)  
  55. {  
  56. fprintf(stderr, "You are using an old version of FMOD %08x. This program requires %08x ", version, FMOD_VERSION);  
  57. exit(-1);  
  58. }  
  59. result = system->init(32, FMOD_INIT_NORMAL | FMOD_INIT_ENABLE_PROFILE, NULL);  
  60. ERRCHECK(result);  
  61. // set up DLS file  
  62. FMOD_CREATESOUNDEXINFO soundExInfo;  
  63. memset(&soundExInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));  
  64. soundExInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);  
  65. char dlsName[200] = {0};  
  66. [[NSString stringWithFormat:@"%@/gm.dls", [[NSBundle mainBundle] resourcePath]] 
  67. getCString:dlsName maxLength:200 encoding:NSASCIIStringEncoding];  
  68. soundExInfo.dlsname = dlsName;  
  69. // midi one  
  70. [[NSString stringWithFormat:@"%@/Bass_sample.mid", [[NSBundle mainBundle] resourcePath]] 
  71. getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];  
  72. result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound1);  
  73. // ERRCHECK(result);  
  74.  
  75. result = sound1->setMode(FMOD_LOOP_OFF);  
  76. // ERRCHECK(result);  
  77. // midi two  
  78. [[NSString stringWithFormat:@"%@/Drum_sample.mid", [[NSBundle mainBundle] resourcePath]]
  79.  getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];  
  80. result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound2);  
  81. result = sound2->setMode(FMOD_LOOP_OFF);  
  82. // timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES];  
  83. }  
  84. - (IBAction)playSound1:(id)sender  
  85. {  
  86. FMOD_RESULT result = FMOD_OK;  
  87. result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);  
  88. ERRCHECK(result);  
  89. }  
  90. - (IBAction)playSound2:(id)sender  
  91. {  
  92. FMOD_RESULT result = FMOD_OK;  
  93. result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);  
  94. ERRCHECK(result);  
  95. }  
  96. - (void)timerUpdate:(NSTimer *)timer  
  97. {  
  98. }  
  99. @end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值