一、工程图
二、代码
rootviewcontroller.h
1
2
3
4
5
6
7
8
9
10
11
12
|
#import <uikit/uikit.h>
//加入avfoundation.framework头文件
#import <avfoundation/avfoundation.h>
@interface rootviewcontroller : uiviewcontroller
<avaudioplayerdelegate>
{
//播放器player
avaudioplayer *avaudioplayer;
}
@end
|
rootviewcontroller.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#import "rootviewcontroller.h"
//导入avfoundation.framework
#import <avfoundation/avfoundation.h>
@interface rootviewcontroller ()
@end
@implementation rootviewcontroller
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
self = [super initwithnibname:nibnameornil bundle:nibbundleornil];
if (self) {
// custom initialization
}
return self;
}
- ( void )viewdidload
{
[super viewdidload];
// do any additional setup after loading the view.
}
-( void )initplay
{
//从budle路径下读取音频文件
nsstring *string = [[nsbundle mainbundle] pathforresource:@ "母亲" oftype:@ "mp3" ];
//把音频文件转换成url格式
nsurl *url = [nsurl fileurlwithpath:string];
//初始化音频类 并且添加播放文件
avaudioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil];
//开始进行播放
[avaudioplayer play];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。