iOS 播放自定義或者系統(tǒng)聲音文件

Objective-C

#import <Foundation/Foundation.h>

@interface OTSAudioPlayer : NSObject


/**
 *  播放自定義或者系統(tǒng)聲音文件
 *
 *  @param aFileName    要播放的文件名
 *  @param aBundelName  存儲(chǔ)路徑
 *  @param ext          文件后綴
 *  @param alert        是否播放自定義或者系統(tǒng)文件
 *
 *  @return UIColor
 */
+ (void)playSoundWithFileName:(NSString *)aFileName
                   bundleName:(NSString *)aBundelName
                       ofType:(NSString *)ext
                     andAlert:(BOOL)alert;

@end
#import "OTSAudioPlayer.h"

#import <AudioToolbox/AudioToolbox.h>

#import "OTSLog.h"

@implementation OTSAudioPlayer

+ (void)playSoundWithFileName:(NSString *)aFileName bundleName:(NSString *)aBundleName ofType:(NSString *)ext andAlert:(BOOL)alert
{
    // 文件存儲(chǔ)路徑
    NSBundle *bundle = [NSBundle mainBundle];
    if (aBundleName.length) {
        bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:aBundleName withExtension:@"bundle"]];
    }
    
    if (!bundle) {
        OTSLogE(@"play sound cannot find bundle: [%@]", aBundleName);
        return;
    }
    
    // 文件路徑
    NSString *path = [bundle pathForResource:aFileName ofType:ext];
    
    if (!path) {
        OTSLogE(@"paly sound cannot find file [%@] in bundle [%@]", aFileName , aBundleName);
        return;
    }
    
    NSURL *urlFile = [NSURL fileURLWithPath:path];
    
    // 聲明需要播放的音頻文件ID[unsigned long];
    SystemSoundID ID;
    
    // 創(chuàng)建系統(tǒng)聲音,同時(shí)返回一個(gè)ID
    OSStatus err = AudioServicesCreateSystemSoundID((__bridge CFURLRef)urlFile, &ID);
    
    if (err != kAudioServicesNoError) {
        OTSLogE(@"play sound cannot create file url [%@]", urlFile);
        return;
    }
    
    // 根據(jù)ID播放自定義系統(tǒng)聲音
    if (alert) {
        AudioServicesPlayAlertSound(ID);
    } else {
        AudioServicesPlaySystemSound(ID);
    }
}

@end

Swift

import UIKit
import AudioToolbox

class OTSAudioPlayer: NSObject {
    
    /**
     *  播放自定義或者系統(tǒng)聲音文件
     *
     *  @param aFileName    要播放的文件名
     *  @param aBundelName  存儲(chǔ)路徑
     *  @param ext          文件后綴
     *  @param alert        是否播放自定義或者系統(tǒng)文件
     *
     *  @return UIColor
     */
    class func playSoundWithFileName(aFileName : String?, aBundleName : String?, ext : String?, alert : Bool) -> Void
    {
        // 文件存儲(chǔ)路徑
        var bundle : NSBundle?
        if (aBundleName! as NSString).length > 0 {
            let url: NSURL? = NSBundle.mainBundle().URLForResource(aBundleName, withExtension: "bundle")
            bundle = NSBundle.init(URL: url!)
        }
        
        if bundle == nil {
            print("play sound cannot find bundle: [\(aBundleName)]")
            return
        }
        
        // 文件路徑
        let path = bundle!.pathForResource(aFileName, ofType: ext)
        
        if path == nil {
            print("paly sound cannot find file [\(aFileName)] in bundle [\(aBundleName)]")
            return
        }
        
        let urlFile = NSURL.fileURLWithPath(path!)
        
        // 聲明需要播放的音頻文件ID[unsigned long];
        var ID : SystemSoundID = 0 // 強(qiáng)制初始化
        
        // 創(chuàng)建系統(tǒng)聲音,同時(shí)返回一個(gè)ID
        let err = AudioServicesCreateSystemSoundID(urlFile, &ID)
        
        if err != kAudioServicesNoError {
            print("play sound cannot create file url [\(urlFile)]");
            return;
        }
        
        // 根據(jù)ID播放自定義系統(tǒng)聲音
        if (alert) {
            AudioServicesPlayAlertSound(ID);
        } else {
            AudioServicesPlaySystemSound(ID);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容