Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

xplat (#4439) #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions react-native-pytorch-core/ios/Audio/Audio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@
* LICENSE file in the root directory of this source tree.
*/

import Foundation
import AVFoundation
import Foundation

@objc(PTLAudio)
public class Audio: NSObject, IAudio {

private var mPlayer: AVAudioPlayer?
private var mData: Data
private var mPlayer: AVAudioPlayer?
private var mData: Data

private static let DEFAULTVOLUME: Float = 1.0
private static let DEFAULTVOLUME: Float = 1.0

@objc
public init(audioData: Data) {
self.mData = audioData
}
@objc
public init(audioData: Data) {
self.mData = audioData
}

public func getData() -> Data {
return self.mData
}
public func getData() -> Data {
return self.mData
}

@objc
public func play() {
do {
if self.mPlayer == nil {
mPlayer = try AVAudioPlayer(data: mData)
}
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
mPlayer?.volume = Audio.DEFAULTVOLUME
mPlayer?.play()
} catch {
print("Error while playing the audio. \(error)")
}
@objc
public func play() {
do {
if self.mPlayer == nil {
mPlayer = try AVAudioPlayer(data: mData)
}
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
mPlayer?.volume = Audio.DEFAULTVOLUME
mPlayer?.play()
} catch {
print("Error while playing the audio. \(error)")
}
}

@objc
public func pause() {
if (mPlayer?.isPlaying) != nil {
mPlayer?.pause()
}
@objc
public func pause() {
if (mPlayer?.isPlaying) != nil {
mPlayer?.pause()
}
}

@objc
public func stop() {
if let mPlayer = mPlayer, mPlayer.isPlaying {
mPlayer.stop()
mPlayer.currentTime = 0
}
@objc
public func stop() {
if let mPlayer = mPlayer, mPlayer.isPlaying {
mPlayer.stop()
mPlayer.currentTime = 0
}
}

@objc
public func getDuration() -> Int {
do {
if self.mPlayer == nil {
mPlayer = try AVAudioPlayer(data: mData)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
}
return Int(mPlayer!.duration * 1000)
} catch {
print("Error while fetching duration of audio. \(error)")
return -1
}
@objc
public func getDuration() -> Int {
do {
if self.mPlayer == nil {
mPlayer = try AVAudioPlayer(data: mData)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
}
return Int(mPlayer!.duration * 1000)
} catch {
print("Error while fetching duration of audio. \(error)")
return -1
}
}
}
Loading