您的位置:首页 > 移动开发 > IOS开发

iOS 设置音频倍速播放

2013-11-18 17:23 309 查看
ViewController.h

//
//  ViewController.h
//  iOSAudio
//
//  Created by 杜甲 on 13-11-18.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <***Foundation/***Foundation.h>

@interface ViewController : UIViewController<***AudioPlayerDelegate>
@property (strong , nonatomic) ***AudioPlayer* player;

@end


ViewController.m

//
//  ViewController.m
//  iOSAudio
//
//  Created by 杜甲 on 13-11-18.
//  Copyright (c) 2013年 杜甲. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self playBackgroundSoundEffect];
}
-(void)playBackgroundSoundEffect
{
    NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"nan" ofType:@"mp3"];
    NSLog(@"%@",soundFilePath);
   // NSString* fileStr = [soundFilePath stringByAddingPercentEscapesUsingEncoding:<#(NSStringEncoding)#>];
    
    NSURL* fileUrl = [NSURL fileURLWithPath:soundFilePath ];
    NSLog(@"%@",fileUrl);
    
    ***AudioPlayer* newPlayer = [[***AudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
    
    self.player = newPlayer;
    [self.player prepareToPlay];
    self.player.delegate =self;
    self.player.volume = 1;
    //newPlayer.numberOfLoops = -1;
    //开启倍速
    self.player.enableRate = true;
    //设置倍速参数
     self.player.rate = 2;
    
    [self.player play];
   
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(getTime) userInfo:nil repeats:YES];
    
    
}
-(void)getTime
{
    NSLog(@"%f",self.player.duration);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


代码下载例子: http://pan.baidu.com/s/1raY8J
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: