iPhone播放mp3文件的例子
本文演示如何使用Objective-C開發播放mp3文件的iPhone程序,當然本文目的不是要讓你做一個iPhone版的mp3播放器,因為這根本用不著你,iPod程序已經很好了。本文的目的是要讓你能夠在自己的游戲中使用音樂。
效果圖如下:
1.打開xcode,創建一個名為PlayMedia的View-based Application類型的iPhone程序(如果你還不會創建Xcode工程,請看iPhone按鈕的使用)。
2.如果要使用播放聲音的功能,一定要引入AVFoundation庫,右擊項目中的Frameworkds目錄,從菜單中選擇Add->Existing Frameworks,下圖所示:
此操作將打開浏覽庫的對話框,我們選擇名為AVFoundation.framework的庫,並把它添加進來。
3.修改PlayMediaViewController.h文件內容如下:
//
// PlayMediaViewController.h
// PlayMedia
//
// Created by jiaqiu on 10-8-3.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface PlayMediaViewController : UIViewController {
AVAudioPlayer *player;
IBOutlet UIButton *btn;
IBOutlet UITextView *txt;
}
-(IBAction)btnPressed:(id)sender;
@end