Skip to content

Commit

Permalink
Voicing selected words and phrases from Longman #62: Create AVAudioNe…
Browse files Browse the repository at this point in the history
…tPlayer to play network audio.
  • Loading branch information
filimo committed Jan 21, 2020
1 parent 6f401c4 commit ceceda9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 75 deletions.
16 changes: 8 additions & 8 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@
F06EE7CB23AFF33E00CF989E /* ConnectionView_Bookmarks.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06EE7CA23AFF33E00CF989E /* ConnectionView_Bookmarks.swift */; };
F06F21B923D421480032E756 /* LongmanRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06F21B823D421480032E756 /* LongmanRepresenter.swift */; };
F073289323A6C7A0008CB539 /* BookmarksView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F073289223A6C7A0008CB539 /* BookmarksView.swift */; };
F074D42D23D5B80D00492922 /* AVAudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioPlayer.swift */; };
F074D42E23D5B80D00492922 /* AVAudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioPlayer.swift */; };
F074D42F23D5B80D00492922 /* AVAudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioPlayer.swift */; };
F074D42D23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioNetPlayer.swift */; };
F074D42E23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioNetPlayer.swift */; };
F074D42F23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F074D42C23D5B80D00492922 /* AVAudioNetPlayer.swift */; };
F075443A234479DA00E1D88E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0754439234479DA00E1D88E /* AppDelegate.swift */; };
F075443E234479DB00E1D88E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F075443D234479DB00E1D88E /* Assets.xcassets */; };
F0754441234479DB00E1D88E /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F0754440234479DB00E1D88E /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -545,7 +545,7 @@
F06EE7CA23AFF33E00CF989E /* ConnectionView_Bookmarks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionView_Bookmarks.swift; sourceTree = "<group>"; };
F06F21B823D421480032E756 /* LongmanRepresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongmanRepresenter.swift; sourceTree = "<group>"; };
F073289223A6C7A0008CB539 /* BookmarksView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BookmarksView.swift; sourceTree = "<group>"; };
F074D42C23D5B80D00492922 /* AVAudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVAudioPlayer.swift; sourceTree = "<group>"; };
F074D42C23D5B80D00492922 /* AVAudioNetPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVAudioNetPlayer.swift; sourceTree = "<group>"; };
F0754437234479DA00E1D88E /* ReaderTranslatorMac.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ReaderTranslatorMac.app; sourceTree = BUILT_PRODUCTS_DIR; };
F0754439234479DA00E1D88E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F075443D234479DB00E1D88E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1232,7 +1232,7 @@
F0C02D3523B26A3800B393A5 /* OSLog.swift */,
F064B4A323CC569400F28314 /* CGFloat.swift */,
F064B4A923CC598400F28314 /* NumberFormatter.swift */,
F074D42C23D5B80D00492922 /* AVAudioPlayer.swift */,
F074D42C23D5B80D00492922 /* AVAudioNetPlayer.swift */,
);
path = Extentions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1762,7 +1762,7 @@
F0039EC323447E24002F3F95 /* SharedContainer.swift in Sources */,
F0C53179234B3EE1003174B5 /* WKCoordinator.swift in Sources */,
F087370823D62CA4001CFBE3 /* MerriamWebsterView.swift in Sources */,
F074D42D23D5B80D00492922 /* AVAudioPlayer.swift in Sources */,
F074D42D23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */,
F064B4A423CC569400F28314 /* CGFloat.swift in Sources */,
F0505C552360D475004F2D50 /* LongmanRepresenter.swift in Sources */,
F0D2E335234BA49000D95994 /* Safari.swift in Sources */,
Expand Down Expand Up @@ -1842,7 +1842,7 @@
F099423323AD4529003CF1EB /* WKScriptsSetup.swift in Sources */,
F099424823AD4623003CF1EB /* CircleButton.swift in Sources */,
F099420D23AD4283003CF1EB /* LongmanStore.swift in Sources */,
F074D42F23D5B80D00492922 /* AVAudioPlayer.swift in Sources */,
F074D42F23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */,
F099423B23AD458F003CF1EB /* SharedContainer.swift in Sources */,
F099422223AD442D003CF1EB /* SafariView.swift in Sources */,
F099422723AD4488003CF1EB /* Stack.swift in Sources */,
Expand Down Expand Up @@ -1915,7 +1915,7 @@
F0AB12B0233F5968005B9F2A /* StatusBarView_Voice.swift in Sources */,
F0AB12B2233F597B005B9F2A /* StatusBarView_Voice_Select.swift in Sources */,
F0EE0A162347BF09004A5EAD /* WKPageView.swift in Sources */,
F074D42E23D5B80D00492922 /* AVAudioPlayer.swift in Sources */,
F074D42E23D5B80D00492922 /* AVAudioNetPlayer.swift in Sources */,
F0F256C2233E6C4C00C9D719 /* FavoriteVoiceName.swift in Sources */,
F0369E3223A2D92C00C33139 /* StatusBarView_Sync.swift in Sources */,
F0C36A9D2359C78A001E396C /* Array.swift in Sources */,
Expand Down
49 changes: 49 additions & 0 deletions ReaderTranslator/Extentions/AVAudioNetPlayer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// AVAudioPlayer.swift
// ReaderTranslator
//
// Created by Viktor Kushnerov on 20/1/20.
// Copyright © 2020 Viktor Kushnerov. All rights reserved.
//

import AVFoundation

class AVAudioNetPlayer {
public var delegate: AVAudioNetPlayerDelegate?
private var player: AVAudioPlayer?

func play(url: URL) {
URLSession.shared.dataTask(with: url, completionHandler: self.load).resume()
}

private func load(data: Data?, response: URLResponse?, error: Error?) {
guard let data = data else {
Logger.log(value: (response?.url, error))
self.delegate?.audioPlayerLoadErrorDidOccur()
return
}
self.delegate?.audioPlayerLoadDidFinishDidOccur()
createAVAudioPlayer(data: data)
}

private func createAVAudioPlayer(data: Data) {
do {
self.player = try AVAudioPlayer(data: data)
if let player = self.player {
player.delegate = self.delegate
self.delegate?.audioPlayerCreateSuccessOccur(player: player)
}
} catch {
Logger.log(value: error)
self.delegate?.audioPlayerCreateErrorDidOccur()
}
}
}

protocol AVAudioNetPlayerDelegate: AVAudioPlayerDelegate {
func audioPlayerLoadDidFinishDidOccur()
func audioPlayerLoadErrorDidOccur()

func audioPlayerCreateSuccessOccur(player: AVAudioPlayer)
func audioPlayerCreateErrorDidOccur()
}
37 changes: 0 additions & 37 deletions ReaderTranslator/Extentions/AVAudioPlayer.swift

This file was deleted.

48 changes: 18 additions & 30 deletions ReaderTranslator/Stores/LongmanStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SwiftSoup
import SwiftUI

private var cancellableSet: Set<AnyCancellable> = []
private var player: AVAudioPlayer?
private var player: AVAudioNetPlayer?

struct LongmanSentence: Hashable {
static let empty = Self(text: "No sentences", url: URL.empty)
Expand Down Expand Up @@ -111,24 +111,11 @@ extension LongmanStore {

extension LongmanStore {
private func play(url: URL) {
URLSession.shared.dataTask(with: url) { data, _, _ in
guard let data = data else { return }
do {
if AudioStore.shared.isEnabled {
player = nil
player = try AVAudioPlayer(data: data)
if let player = player {
player.delegate = self
player.enableRate = true
player.rate = self.audioRate
player.volume = AudioStore.shared.volume
player.play()
}
}
} catch {
self.audioPlayerCreateErrorDidOccur(error: error)
}
}.resume()
if AudioStore.shared.isEnabled {
player = AVAudioNetPlayer()
player?.delegate = self
player?.play(url: url)
}
}

func next() {
Expand All @@ -137,18 +124,19 @@ extension LongmanStore {
}
}

extension LongmanStore: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_: AVAudioPlayer, successfully _: Bool) {
next()
}
extension LongmanStore: AVAudioNetPlayerDelegate {
func audioPlayerLoadDidFinishDidOccur() {}

func audioPlayerDecodeErrorDidOccur(_: AVAudioPlayer, error _: Error?) {
next()
func audioPlayerCreateSuccessOccur(player: AVAudioPlayer) {
player.enableRate = true
player.rate = audioRate
player.volume = AudioStore.shared.volume
player.play()
}

func audioPlayerCreateErrorDidOccur(error: Error) {
Logger.log(value: error)
next()
}
}
func audioPlayerLoadErrorDidOccur() { next() }
func audioPlayerCreateErrorDidOccur() { next() }

func audioPlayerDidFinishPlaying(_: AVAudioPlayer, successfully _: Bool) { next() }
func audioPlayerDecodeErrorDidOccur(_: AVAudioPlayer, error _: Error?) { next() }
}

0 comments on commit ceceda9

Please sign in to comment.