xcode添加背景音乐/音效
xcode添加音效:http://www.cnblogs.com/jiayongqiang/p/5625886.html
背景音乐:
ios播放音乐时会用到一个叫做AVAudioPlayer的类,这个类用于播放手机本地的音乐文件。需要注意的是
(1)该类(AVAudioPlayer)只能用于播放本地音频。
(2)时间比较短的(音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(音乐)使用AVAudioPlayer类。
步骤:
1.导入导入AVFoundation框架。
2.导入头文件#import <AVFoundation/AVFoundation.h>
3.导入背景音乐.
4.书写代码:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> @interface ViewController ()
@property (nonatomic,strong) AVAudioPlayer *player;
@end @implementation ViewController
-(AVAudioPlayer *)player{
if (!_player) {
//1.创建音乐路径
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"五环之歌" ofType:@"mp3"];
NSURL *musicURL = [[NSURL alloc]initFileURLWithPath:musicFilePath]; //2.创建播放器
_player = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:nil]; }
return _player;
}
- (void)viewDidLoad {
[super viewDidLoad];
//3.预播放
[self.player prepareToPlay];
//4.设置播放次数.-1为循环播放
self.player.numberOfLoops = -;
//5.开始播放
[self.player play]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//6.暂停播放, dispatch_after 延迟加载方法. 这里设置为2s,也可以设置其他的延迟秒数
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2* NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
// [self.player stop];
// }); [self.player stop]; }
AVAudioPlayer 崩溃问题:
xcode添加背景音乐/音效的更多相关文章
- xcode添加音效
xcode添加背景音乐/音效 背景音乐:http://www.cnblogs.com/jiayongqiang/p/5476069.html 音效: 一.介绍: 又称“短音频”,通常在程序中的播放时长 ...
- 005-unity3d 添加背景音乐、音效 以及 天空盒子
一.基础知识 1.项目中需要有AudioListener,播放器中播放的声音就是AudioListener组件坐在的位置听到的声音.默认AudioListener是放到Main Camera上.没有A ...
- Xcode添加摄像机访问权限<转>
转帖地址:http://www.manew.com/thread-97708-1-1.html ============================================== ios系统 ...
- IOS开发效率之为Xcode添加常用的代码片段
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
- xcode添加build phase
[xcode添加build phase] xcode版本:5.0.2,找了半天,终于找到add build phase的方法,如下图.
- h5页面添加背景音乐
[需求]h5页面添加背景音乐,支持微信.QQ.各种APP. [实现思路]1.通过audio标签,设置自动播放,是一种方法,但是此方法只适合微信.QQ,并不兼容我司的APP,需要主动触发下播放事件. 2 ...
- Java代码添加背景音乐
太心塞!弄了很久才终于把Java添加背景音乐实现了.不过还是很Happy! 这次介绍的办法,是只要一打开Java Application,便可直接听到背景音乐.代码保存,方便以后再次利用. packa ...
- vue添加背景音乐
vue添加背景音乐需要用到HTML中的标签 参考手册:http://www.w3school.com.cn/tags/html_ref_audio_video_dom.asp *在iOS端autopl ...
- vue 页面 添加背景音乐
背景音乐 添加背景音乐 并有单击事件 循环播放 <template> <div id="page"> <div style="width ...
随机推荐
- yii 之数据库关联查询
<?php namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerC ...
- 程序防止SqlServer使用SqlServer Profiler跟踪
思路: 1.使用默认函数(fn_trace_getinfo)查询跟踪列表: 2.调用系统存储过程(sp_trace_setstatus)修改跟踪状态. 相关Sql : declare @default ...
- Python知识图谱
一.Python全栈图谱 2.Python语言高级 Python 全栈工程师前端 Python全栈工程师后端 Python Linux运维自动化开发 Python KaliLinux信息安全开发和使用 ...
- Spring Boot使用Schedule实现定时任务
适用的工具是:Schedule 集成步骤: 1.开启Schedule支持 package com.jsoft.springboottest.springboottest1; import org.sp ...
- PYTHON 源码
http://www.wklken.me/index2.html http://blog.csdn.net/dbzhang800/article/details/6683440
- 【原】理解javascript中的闭包(***********************************************)
阅读目录 什么是闭包? 闭包的特性 闭包的作用: 闭包的代码示例 注意事项 总结 闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 回到顶 ...
- java判断字符串中是否含有汉字
原文:http://www.open-open.com/code/view/1426332240717 判断字符串中是否含有汉字: String str = "test中文汉字"; ...
- ios实现下载图片的裁减和显示
使用如下的方法可以裁减的同时保证了不丢失像素. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // Set ...
- HDU1800 Flying to the Mars 【贪心】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- python远程访问hive
#!/usr/bin/pythonimport syssys.path.append('/home/zhoujie/Downloads/hive-0.7.0-cdh3u0/lib/py')from h ...