如何使用iPhone 播放 MIDI 文件方法
iPhone 播放 midi 文件方法是本文要介紹的內(nèi)容,吸納來(lái)看內(nèi)容。iPhone 默認(rèn)不帶播放MIDI的框架,所以播放MIDI還得尋求第3方的庫(kù)幫忙。這里使用的庫(kù)就是大名鼎鼎的 FMOD ,許多火爆游戲使用的都是這個(gè)庫(kù)。開(kāi)發(fā)者可以免費(fèi)下載使用。
首先下載安裝 FMOD API FOR IPHONE:http://www.fmod.org/index.php/release/version/fmodapi42607iphone- installer.dmg。
安裝后可以在目錄中看到不少示范代碼,可惜沒(méi)有MIDI。
自己寫(xiě)一個(gè):)感謝強(qiáng)大的api,寫(xiě)起來(lái)異常輕松。
新建一個(gè)基于view項(xiàng)目
修改項(xiàng)目屬性,添加 Other Linker Flags 為 -lfmodexL_$PLATFORM_NAME
添加 Header Search Paths :/Developer/FMOD Programmers API iPhone/api/inc (默認(rèn)是這個(gè)位置,修改成自己FMOD安裝的目錄)
添加 Library Search Paths :/Developer/FMOD Programmers API iPhone/api/lib (同上)
把 appDelegate 修改成 .mm 的后綴
MIDI 播放需要一個(gè) DLS 文件, 在osx 下沒(méi)找到,這里使用了xp 自帶的 gm.dls 文件(3M 有點(diǎn)大~),拷貝到項(xiàng)目中。
修改ViewController 代碼如下 ,隨便在xib文件中鏈接兩個(gè)按鈕action上即可
運(yùn)行(真機(jī)有效)
主要代碼
- //
- // PlayMidiDemoViewController.m
- // PlayMidiDemo
- //
- // Created by xhan on 9/9/09.
- // Copyright In-Blue 2009. All rights reserved.
- //
- #import "PlayMidiDemoViewController.h"
- @implementation PlayMidiDemoViewController
- @synthesize status;
- @synthesize time;
- void ERRCHECK(FMOD_RESULT result)
- {
- if (result != FMOD_OK)
- {
- fprintf(stderr, "FMOD error! (%d) %s ", result, FMOD_ErrorString(result));
- exit(-1);
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- system = NULL;
- sound1 = NULL;
- sound2 = NULL;
- channel = NULL;
- }
- - (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 {
- [status release], status = nil;
- [time release], time = nil;
- [super dealloc];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- FMOD_RESULT result = FMOD_OK;
- char buffer[200] = {0};
- unsigned int version = 0;
- /*
- Create a System object and initialize
- */
- result = FMOD::System_Create(&system);
- ERRCHECK(result);
- result = system->getVersion(&version);
- ERRCHECK(result);
- if (version < FMOD_VERSION)
- {
- fprintf(stderr, "You are using an old version of FMOD %08x. This program requires %08x ", version, FMOD_VERSION);
- exit(-1);
- }
- result = system->init(32, FMOD_INIT_NORMAL | FMOD_INIT_ENABLE_PROFILE, NULL);
- ERRCHECK(result);
- // set up DLS file
- FMOD_CREATESOUNDEXINFO soundExInfo;
- memset(&soundExInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
- soundExInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
- char dlsName[200] = {0};
- [[NSString stringWithFormat:@"%@/gm.dls", [[NSBundle mainBundle] resourcePath]]
- getCString:dlsName maxLength:200 encoding:NSASCIIStringEncoding];
- soundExInfo.dlsname = dlsName;
- // midi one
- [[NSString stringWithFormat:@"%@/Bass_sample.mid", [[NSBundle mainBundle] resourcePath]]
- getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
- result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound1);
- // ERRCHECK(result);
- result = sound1->setMode(FMOD_LOOP_OFF);
- // ERRCHECK(result);
- // midi two
- [[NSString stringWithFormat:@"%@/Drum_sample.mid", [[NSBundle mainBundle] resourcePath]]
- getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
- result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound2);
- result = sound2->setMode(FMOD_LOOP_OFF);
- // timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES];
- }
- - (IBAction)playSound1:(id)sender
- {
- FMOD_RESULT result = FMOD_OK;
- result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
- ERRCHECK(result);
- }
- - (IBAction)playSound2:(id)sender
- {
- FMOD_RESULT result = FMOD_OK;
- result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);
- ERRCHECK(result);
- }
- - (void)timerUpdate:(NSTimer *)timer
- {
- }
- @end
小結(jié):關(guān)于如何使用iPhone 播放 MIDI 文件方法介紹完了,希望本文讀你有所幫助!