iPhone播放mp3文件的例子

iPhone播放mp3文件的例子

原文连接:http://plter.com/?p=354

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文件内容如下:

Code block   
//
//  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

4.双击PlayMediaViewController.xib文件打开InterfaceBuilder,拖入一个Text View组件和一个Round Rect Button组件,并将这两个组件分别绑定为txt和btn(如果你还不会绑定InterfaceBuilder组件到Objective-C代码,请看iPhone按钮的使用),然后将按钮的标签修改为“播放音乐”

5.修改PlayMediaViewController.m文件的内容如下所示:

Code block   
//
//  PlayMediaViewController.m
//  PlayMedia
//
//  Created by jiaqiu on 10-8-3.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//
 
#import "PlayMediaViewController.h"
 
@implementation PlayMediaViewController
 
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/
 
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 
	if (player) {
		[player release];
	}
 
	NSString *soundPath=[[NSBundle mainBundle] pathForResource:@"羽毛" ofType:@"mp3"];
	txt.text=soundPath;
	NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:soundPath];
 
	player=[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
	[player prepareToPlay];
 
    [super viewDidLoad];
}
 
-(void)btnPressed:(id)sender{
	[player play];
}
 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
 
- (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 {
    [super dealloc];
}
 
@end

6.此代码将播放一个名为 “羽毛.mp3”的文件,请将这个文件加入到资源文件夹(Resources)中.

7.按Command+R运行此程序,尝试点击“播放音乐”按钮,就可以听到播放的声音了。

源文件下载:

转载于:https://www.cnblogs.com/jdwq/archive/2012/03/22/2410881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值