audio - iOS Xode : AVAudioPlayer is not playing WAV file correctly -
i have wav file tones @ around 18khz. audio 16-bit pcm mono.
i using following function play file:
func playsound(name: string) { let documents = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0] let path = documents.appending("/").appending(name) let url = nsurl.fileurl(withpath: path) { player = try avaudioplayer(contentsof: url) guard let player = player else { return } player.preparetoplay() player.play() } catch let error { print(error.localizeddescription) } }
when play on ios 10.2 device hear series of tones between 1000hz 10000hz. i've analyzed rendered audio capturing , frequency plot shows original content @ 18khz there, there tones present between 1000hz 10000hz. when play same wav file vlc or other desktop audio player, don't hear tones (which expected since they're located around 18khz). suspect code above isn't loading data correctly or player isn't initialized, need seasoned ios veteran can tell me i'm doing wrong.
thank in advance.
this 1 work //
import uikit import avfoundation
class viewcontroller: uiviewcontroller {
var audioplayer = avaudioplayer() override func viewdidload() { super.viewdidload() var alertsound = nsurl(fileurlwithpath: nsbundle.mainbundle().pathforresource("button-09", oftype: "wav")) println(alertsound) // removed deprecated use of avaudiosessiondelegate protocol avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) avaudiosession.sharedinstance().setactive(true, error: nil) var error:nserror? audioplayer = avaudioplayer(contentsofurl: alertsound, error: &error) audioplayer.preparetoplay() audioplayer.play() }
}strong text
Comments
Post a Comment