• 网站首页
  • 关于我们
  • 产品中心
  • 项目案例
  • 技术支持
  • 开发问答
  • 联系我们
  • 当前位置
  • 网站首页 > iphone软件开发 >
  • 网站首页

    iPhone 播放音频声音文件

    • 字号
      特大 较大 标准 较小
    • 全屏阅读
    • 分享到

    iphone开发中播放声音文件主要使用AVAudioPlayer 类,它的功能非常强大支持播放音频的格式也非常的多,我们可以把它看成一个高级的音乐播放器,它支持的播放格式有

    ■ AAC

    ■ AMR(AdaptiveMulti-Rate, aformatforspeech)

    ■ ALAC(AppleLossless)

    ■ iLBC(internetLowBitrateCodec, anotherformatforspeech)

    ■ IMA4(IMA/ADPCM)

    ■ linearPCM(uncompressed)

    ■ µ-lawanda-law

    ■ MP3(MPEG-1audiolayer3

    今天主要介绍一下播放mp3 .

    AVAudioPlayer 是 AVFoundation.framework 中定义的一个类,所以使用要先在工程中引入AVFoundation.framework 如图所示点击”+”号将AVFoundation导入。

    将音频文件放入资源文件夹中

    下面我开始介绍代码中如何调用AVAudioPlayer 播放音频文件

    声明类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    //
    //  playSoundViewController.h
    //  playSound
    //
    //  Created by  宣雨松 on 11-7-10.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
     
    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    @interface playSoundViewController : UIViewController {
     
     IBOutlet UIButton * playSound;//播放音乐  
     IBOutlet UIButton * playPause;//播放暂停  
     IBOutlet UIButton * playStop;//播放停止  
     //定义一个声音的播放器
     AVAudioPlayer *player;
    }
     
    -(IBAction)playSoundPressed:(id)pressed;
    -(IBAction)playPausePressed:(id)pressed;
    -(IBAction)playStopPressed:(id)pressed;
    @end

    实现类

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    
    //
    //  playSoundViewController.m
    //  playSound
    //
    //  Created by  宣雨松 on 11-7-10.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
     
    #import "playSoundViewController.h"
     
    @implementation playSoundViewController
     
    - (void)dealloc
    {
     [super dealloc];
     //程序的严谨性 在显示对象关闭后把相应的对象清空
     //时刻谨记
     [playSound release];
     [player release];
    }
     
    - (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.
    }
     
    #pragma mark - View lifecycle
     
     
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
     [super viewDidLoad];
     //在这里实现声音的播放代码
     //找到mp3在资源库中的路径 文件名称为sound 类型为mp3
     NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
     //在这里判断以下是否能找到这个音乐文件
     if (path) {
     //从path路径中 加载播放器
     player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:nil];
     //初始化播放器
     [player prepareToPlay];
     
     //设置播放循环次数,如果numberOfLoops为负数 音频文件就会一直循环播放下去
     player.numberOfLoops = -1;
     
     //设置音频音量 volume的取值范围在 0.0为最小 0.1为最大 可以根据自己的情况而设置
     player.volume = 0.5f;
     
     NSLog(@"播放加载");
     }
     
    }
     
    -(void)playSoundPressed:(id)pressed
    {
     //点击按钮后开始播放音乐
     //当player有值的情况下并且没有在播放中 开始播放音乐
     if (player) 
     {
     if (![player isPlaying]) 
     {
     [player play];
     NSLog(@"播放开始");
     }
     }
    }
     
    -(void)playPausePressed:(id)pressed
    {
     //暂停播放声音
     if (player) {
     if ([player isPlaying]) {
     [player pause];
     NSLog(@"播放暂停");
     }
     }
    }
     
    -(void)playStopPressed:(id)pressed
    {
     //停止播放声音
     if (player) {
     if ([player isPlaying]) {
     [player stop];
     NSLog(@"播放停止");
     }
     }
    }
     
     
    - (void)viewDidUnload
    {
     [super viewDidUnload];
     // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
    }
     
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
     // Return YES for supported orientations
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
     
    @end

     


    iphone软件开发,ios软件开发,android软件开发,电子菜谱,手机软件开发,手机软件开发公司,安卓软件开发,无线时代
    分享到:
    手机软件开发 返回网站首页

    发表评论

    验证码: 点击验证码

    项目案例Case

    iphone软件开发
    android软件开发
    window phone开发
    我们的优势

    开发问答Question & answer

    • Android支持哪些分辨率?
    • 如何发布应用到app store?
    • 如何在Android的模拟器 SD卡中添加文件
    • 如何编写高效的Android代码?
    • Android中判断网络功能是否可用
    • Android中如何获取手机屏幕大小
    • Android如何使用命令行查看sqlite3
    • Android中模拟器如何访问本地服务器
    • Android NDK开发的相关问题

    相关推荐Recommend

    • 使用Makefile自动编译iPhone程序
    • iPhone应用程序的启动过程
    • iPhone应用程序/项目的构成
    • iphone开发代码片段备忘
    • 第一个IPHONE应用开发
    • 对App应用程序高效压缩
    • Corona新增4个全新的API
    • iPhone开发--removeFromSuperview当tag为0时不能正常工作
    • iPhone开发环境的要求
    ©2010 WAPERA.CN Co., Ltd. All rights reserved.
    Makingware