zoukankan      html  css  js  c++  java
  • iOS 播放30s内的短音频

    /
    //  ViewController.h
    //  播放短音频
    //
    //  ViewController.m
    //  播放短音频
    //
    //  Created by 王耀 on 16/3/30.
    //  Copyright © 2016年 Mgs. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
    }
    - (IBAction)startPlay:(UIButton *)sender
    {
        [self playSoundEffect:@"shake.wav"];
    }
    /**
     *  播放音效文件
     *
     *  @param name 音频文件名称
     */
    -(void)playSoundEffect:(NSString *)name
    {
        NSString *audioFile=[[NSBundle mainBundle] pathForResource:name ofType:nil];
        NSURL *fileUrl=[NSURL fileURLWithPath:audioFile];
        //1.获得系统声音ID
        SystemSoundID soundID=0;
        /**
         * inFileUrl:音频文件url
         * outSystemSoundID:声音id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID)
         */
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
        //如果需要在播放完之后执行某些操作,可以调用如下方法注册一个播放完成回调函数
        AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
        //2.播放音频
        AudioServicesPlaySystemSound(soundID);//播放音效
        //    AudioServicesPlayAlertSound(soundID);//播放音效并震动
    }
    /**
     *  播放完成回调函数
     *
     *  @param soundID    系统声音ID
     *  @param clientData 回调时传递的数据
     */
    void soundCompleteCallback(SystemSoundID soundID,void * clientData)
    {
        NSLog(@"播放完成...");
    }
    
    
    @end
    
    
    
    //  Copyright © 2016年 Mgs. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import <AudioToolbox/AudioToolbox.h>
    @interface ViewController : UIViewController
    
    
    @end

      需要导入AudioToolbox.framework框架

  • 相关阅读:
    nginx配置文件详解
    centos 小知识
    nginx 常见问题
    centos7.5 安装nginx
    tomact 配置远程登录
    Centos7 离线安装 mariaDB
    Crontab详细用法-定时任务详解
    新项目push/pull到github
    GIT的基本操作
    hive的安装
  • 原文地址:https://www.cnblogs.com/Mgs1991/p/5337709.html
Copyright © 2011-2022 走看看