Skip to content

Commit

Permalink
Merge pull request #7 from kheniparth/feature/play-30-sec-song
Browse files Browse the repository at this point in the history
add audio player
  • Loading branch information
kheniparth committed Jul 30, 2017
2 parents 65fd492 + 5286cdc commit a4a9738
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -18,3 +18,5 @@ TechMuzzJsonParser
![Final App Screenshot](/screenshots/final1.png?raw=true "Table cells with thumbnails")

![Final App Screenshot](/screenshots/final2.png?raw=true "Track detail view")

![Final App Screenshot](/screenshots/final2.png?raw=true "Audio Player button")
71 changes: 71 additions & 0 deletions TechMuzzJsonParser/AudioVC.swift
Expand Up @@ -8,22 +8,93 @@

import Foundation
import UIKit
import AVFoundation

class AudioVC : UIViewController {

var image = UIImage()
var mainSongTitle = String()
var previewUrl = String()

@IBOutlet var background: UIImageView!

@IBOutlet var mainImageView: UIImageView!

@IBOutlet var songTitle: UILabel!

@IBOutlet var playPauseButton: UIButton!

override func viewDidLoad() {

songTitle.text = mainSongTitle
background.image = image
mainImageView.image = image
playPauseButton.setTitle("Pause", for: .normal)
playPauseButton.isHidden = false
if previewUrl != "" {
downloadFileFromUrl(url: URL(string: previewUrl)!)
} else {
playPauseButton.isHidden = true
self.showToast(message: "No Preview URL...")
}
}

func downloadFileFromUrl(url: URL) {
var downloadTask = URLSessionDownloadTask()
downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: {
customURL, response, error in
self.play(url: customURL!)
})
downloadTask.resume()
}

func play(url : URL) {
do {
player = try AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
player.play()
}
catch {
print(error)
}

}

@IBAction func playPause(_ sender: Any) {
if player.isPlaying {
player.pause()
playPauseButton.setTitle("Play", for: .normal)
} else {
player.play()
playPauseButton.setTitle("Pause", for: .normal)
}
}

override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)

if (self.isMovingFromParentViewController){
player.pause()
}
}

func showToast(message : String) {

let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}

}
13 changes: 11 additions & 2 deletions TechMuzzJsonParser/Base.lproj/Main.storyboard
Expand Up @@ -30,7 +30,6 @@
<!--Table View Controller-->
<scene sceneID="hOr-dl-K36">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="DJz-Uf-f9H" userLabel="First Responder" sceneMemberID="firstResponder"/>
<tableViewController id="P9y-gP-SO7" customClass="TableViewController" customModule="TechMuzzJsonParser" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="100" sectionHeaderHeight="28" sectionFooterHeight="28" id="ceM-UA-lZG">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
Expand All @@ -41,7 +40,7 @@
<rect key="frame" x="0.0" y="28" width="375" height="100"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="WbV-oM-gLv" id="P6A-Tr-Wli">
<rect key="frame" x="0.0" y="0.0" width="375" height="100"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="99"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView userInteractionEnabled="NO" tag="2" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Zrc-eW-ScS">
Expand Down Expand Up @@ -78,6 +77,7 @@
</tableView>
<navigationItem key="navigationItem" id="LlH-xO-TpV"/>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="DJz-Uf-f9H" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-143.19999999999999" y="204.64767616191907"/>
</scene>
Expand Down Expand Up @@ -117,6 +117,14 @@
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="FIE-5n-Sqz">
<rect key="frame" x="164" y="473" width="46" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<connections>
<action selector="playPause:" destination="hMi-gp-f15" eventType="touchUpInside" id="Dks-rB-bSk"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="sYw-RE-9S9" secondAttribute="trailing" constant="68" id="IHE-DF-r5e"/>
Expand Down Expand Up @@ -145,6 +153,7 @@
<connections>
<outlet property="background" destination="Yw3-T0-bnJ" id="cba-qX-XPr"/>
<outlet property="mainImageView" destination="VgS-cO-ddG" id="TFK-JL-KDl"/>
<outlet property="playPauseButton" destination="FIE-5n-Sqz" id="O4K-qM-Ybu"/>
<outlet property="songTitle" destination="sYw-RE-9S9" id="5vX-EY-PCN"/>
</connections>
</viewController>
Expand Down
8 changes: 7 additions & 1 deletion TechMuzzJsonParser/ViewController.swift
Expand Up @@ -8,10 +8,14 @@

import UIKit
import Alamofire
import AVFoundation

var player = AVAudioPlayer()

struct Post {
let mainImage : UIImage!
let name : String!
let previewUrl : String!
}

class TableViewController: UITableViewController {
Expand Down Expand Up @@ -46,6 +50,7 @@ class TableViewController: UITableViewController {
for i in 0..<items.count {
let item = items[i]
let name = item["name"] as! String
let previewUrl = item["preview_url"] as? String == nil ? "" : item["preview_url"] as! String

if let album = item["album"] as? JSONStandard {
if let images = album["images"] as? [JSONStandard] {
Expand All @@ -55,7 +60,7 @@ class TableViewController: UITableViewController {

let mainImage = UIImage(data: mainImageData as! Data)

posts.append(Post.init(mainImage: mainImage, name: name))
posts.append(Post.init(mainImage: mainImage, name: name, previewUrl: previewUrl))
self.tableView.reloadData()
}
}
Expand Down Expand Up @@ -87,6 +92,7 @@ class TableViewController: UITableViewController {

vc.image = posts[indexPath!].mainImage
vc.mainSongTitle = posts[indexPath!].name
vc.previewUrl = posts[indexPath!].previewUrl
}

override func didReceiveMemoryWarning() {
Expand Down
Binary file added screenshots/final3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a4a9738

Please sign in to comment.