资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

震动ios开发,ios持续震动

iOS开发中对音效和音乐播放的简单实现

一、简单介绍

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站建设、龙口网络推广、成都小程序开发、龙口网络营销、龙口企业策划、龙口品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供龙口建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

简单来说,音频可以分为2种

(1)音效

又称“短音频”,通常在程序中的播放时长为1~2秒

在应用程序中起到点缀效果,提升整体用户体验

(2)音乐

比如游戏中的“背景音乐”,一般播放时间较长

框架:播放音频需要用到AVFoundation.framework框架

二、音效的播放

1.获得音效文件的路径

复制代码 代码如下:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

2.加载音效文件,得到对应的音效ID

复制代码 代码如下:

SystemSoundID soundID = 0;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), soundID);

3.播放音效

复制代码 代码如下:

AudioServicesPlaySystemSound(soundID);

注意:音效文件只需要加载1次

4.音效播放常见函数总结

加载音效文件

复制代码 代码如下:

AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

释放音效资源

复制代码 代码如下:

AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效

复制代码 代码如下:

AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效带点震动

复制代码 代码如下:

AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

三、程序示例

先导入需要依赖的框架

导入需要播放的音效文件素材

说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。

代码示例:

复制代码 代码如下:

YYViewController.m文件

//

// YYViewController.m

// 14-音效播放

//

// Created by apple on 14-8-8.

// Copyright (c) 2014年 yangyong. All rights reserved.

//

#import "YYViewController.h"

#import

@interface YYViewController ()

@end

复制代码 代码如下:

@implementation YYViewController

- (void)viewDidLoad

{

[super viewDidLoad];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

//1.获得音效文件的全路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"buyao.wav" withExtension:nil];

//2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)

SystemSoundID soundID=0;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, soundID);

//把需要销毁的音效文件的ID传递给它既可销毁

//AudioServicesDisposeSystemSoundID(soundID);

//3.播放音效文件

//下面的两个函数都可以用来播放音效文件,第一个函数伴随有震动效果

AudioServicesPlayAlertSound(soundID);

//AudioServicesPlaySystemSound(#systemsoundid)

}

@end

说明:点击屏幕可以播放音效文件。

音乐的播放

一、简单说明

音乐播放用到一个叫做AVAudioPlayer的`类,这个类可以用于播放手机本地的音乐文件。

注意:

(1)该类(AVAudioPlayer)只能用于播放本地音频。

(2)时间比较短的(称之为音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(称之为音乐)使用AVAudioPlayer类。

二、代码示例

AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。

导入必要的,需要播放的音频文件到项目中。

代码示例:

复制代码 代码如下:

//

// YYViewController.m

// 15-播放音乐

//

#import "YYViewController.h"

#import

@interface YYViewController ()

@end

复制代码 代码如下:

@implementation YYViewController

- (void)viewDidLoad

{

[super viewDidLoad];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

//1.音频文件的url路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)

AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.缓冲

[audioPlayer prepareToPlay];

//4.播放

[audioPlayer play];

}

@end

代码说明:运行程序,点击模拟器界面,却并没有能够播放音频文件,原因是代码中创建的AVAudioPlayer播放器是一个局部变量,应该调整为全局属性。

可将代码调整如下,即可播放音频:

复制代码 代码如下:

#import "YYViewController.h"

#import

@interface YYViewController ()

@property(nonatomic,strong)AVAudioPlayer *audioplayer;

@end

复制代码 代码如下:

@implementation YYViewController

- (void)viewDidLoad

{

[super viewDidLoad];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

//1.音频文件的url路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)

self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.缓冲

[self.audioplayer prepareToPlay];

//4.播放

[self.audioplayer play];

}

@end

注意:一个AVAudioPlayer只能播放一个url,如果想要播放多个文件,那么就得创建多个播放器。

三、相关说明

新建一个项目,在storyboard中放三个按钮,分别用来控制音乐的播放、暂停和停止。

程序代码如下:

复制代码 代码如下:

#import "YYViewController.h"

#import

@interface YYViewController ()

@property(nonatomic,strong)AVAudioPlayer *player;

- (IBAction)play;

- (IBAction)pause;

- (IBAction)stop;

@end

@implementation YYViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//1.音频文件的url路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)

self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.缓冲

[self.player prepareToPlay];

}

- (IBAction)play {

//开始播放/继续播放

[self.player play];

}

- (IBAction)pause {

//暂停

[self.player pause];

}

- (IBAction)stop {

//停止

//注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题

[self.player stop];

}

@end

注意:如果点了“停止”,那么一定要播放器重新创建,不然的话会出现莫名其妙的问题。

点击了stop之后,播放器实际上就不能再继续使用了,如果还继续使用,那么后续的一些东西会无法控制。

推荐代码:

复制代码 代码如下:

#import "YYViewController.h"

#import

@interface YYViewController ()

@property(nonatomic,strong)AVAudioPlayer *player;

- (IBAction)play;

- (IBAction)pause;

- (IBAction)stop;

@end

复制代码 代码如下:

@implementation YYViewController

#pragma mark-懒加载

-(AVAudioPlayer *)player

{

if (_player==Nil) {

//1.音频文件的url路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)

self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.缓冲

[self.player prepareToPlay];

}

return _player;

}

- (void)viewDidLoad

{

[super viewDidLoad];

}

- (IBAction)play {

//开始播放/继续播放

[self.player play];

}

- (IBAction)pause {

//暂停

[self.player pause];

}

- (IBAction)stop {

//停止

//注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题

[self.player stop];

self.player=Nil;

}

@end

四、播放多个文件

点击,url,按住common建查看。

可以发现,这个url是只读的,因此只能通过initWithContentsOfUrl的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。

那么如何实现播放多个音频文件呢?

可以考虑封装一个播放音乐的工具类,下一篇文章将会介绍具体怎么实现。

iOS中震动反馈(UIFeedbackGenerator)与系统震动详解

Taptic Engine

先了解一个概念——Taptic Engine

Taptic Engine 是苹果产品上推出的全新震动模块,该元件最早出现在 Apple Watch 中。iPhone 6s 和 iPhone 6s Plus 中,也同样内置了Taptic Engine,在设计上有所升级。

Taptic Engine 振动模块为 Apple Watch 以及 iPhone 6s、iPhone 7 提供了 Force Touch 以及 3D Touch,不同的屏幕操作,可以感受到不同的振动触觉效果,带来更好的用户体验。

震动反馈(UIFeedbackGenerator)

震动反馈是iOS 10之后出的新特性,相比于之前的系统震动

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

要友好得多,没有声音,震动幅度适中,不需要设置里“响铃模式震动”打开。这也是Apple更推荐开发者使用的反馈震动。

e.g. Switch控件滑动,时钟里选时间滑动,产生的震动都是UIFeedbackGenerator特性的。

现在“震动反馈”的应用是非常广的 —— 下拉刷新;点击重要的Button;选择器等等。都可以加上反馈。

Apple文档(UIFeedbackGenerator)

//

// UIImpactFeedbackGenerator.h

// UIKit

//

// Copyright © 2016 Apple Inc. All rights reserved.

//

#import UIKit/UIFeedbackGenerator.h

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {

UIImpactFeedbackStyleLight,

UIImpactFeedbackStyleMedium,

UIImpactFeedbackStyleHeavy

};

// UIImpactFeedbackGenerator is used to give user feedback when an impact between UI elements occurs

UIKIT_CLASS_AVAILABLE_IOS_ONLY(10_0) @interface UIImpactFeedbackGenerator : UIFeedbackGenerator

- (instancetype)initWithStyle:(UIImpactFeedbackStyle)style;

/// call when your UI element impacts something else

- (void)impactOccurred;

@end

想要用震动反馈也特别简单:

UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];

[feedBackGenertor impactOccurred];

注意: “UIImpactFeedbackGenerator' is only available on iOS 10.0 or newer”,使用的时候加上版本限制。**

手机 -- 设置 -- 声音与触感 -- 系统触感反馈(打开)

此前系统震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

在iOS 10之前,系统震动采用的是震动+铃声的模式,目前看来是及其不友好的,首先震动略大,其次带声音,体验并不好。但这种的方式可以自定义音效。

Apple文档(AudioServicesPlaySystemSound)

#import AudioToolbox/AudioToolbox.h

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

注意:手机 -- 设置 -- 声音与触感 -- 响铃模式震动(打开)

ios 怎么通过代码让手机震动

不能 js是运行在浏览器的代码,他所能操作的范围只限定在浏览器范围内 要调用手机硬件,只能通过手机系统提供的api来调用 例如通过ios的,或安卓的系统接口来调用设备硬件

ios10怎么设置震动 ?

点击打开设置-声音-电话铃声-震动

简介:

iOS 10是苹果公司研发的新的操作系统。

2016年6月,苹果系统iOS 10正式亮相,苹果为iOS 10带来了十大项更新。2016年6月13日,苹果开发者大会WWDC在旧金山召开,会议宣布iOS 10的测试版在2016年夏天推出,正式版将在秋季发布。2016年9月7日,苹果发布iOS 10。2016年9月14日,全新的操作系统iOS 10将正式上线,iOS 10注重安全引入欺诈电话拦截软件。

2016年10月25日,苹果公司正式推送iOS 10.1系统。它修复了一些之前版本的漏洞,并给iPhone 7 Plus镜头加入了人像模式。即背景虚化相机功能。

iOS简单实现震动

简单实现手机震动,首先导入 AudioToolBox.framework ,在需要震动的文件中 #import AudioToolbox/AudioToolbox.h 。

调用震动的方法有2个

第一个

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);```

两个方法都可以使用,直接调用就可以实现简单的震动。


标题名称:震动ios开发,ios持续震动
网站路径:http://cdkjz.cn/article/dsdsddd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220