Skip to content

Commit

Permalink
play music
Browse files Browse the repository at this point in the history
  • Loading branch information
cjiong committed May 22, 2016
1 parent 06aa4d5 commit 6272095
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
19AD6FB61CF1E50C00900ECB /* Project_09___MusicPlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19AD6FB51CF1E50C00900ECB /* Project_09___MusicPlayerTests.swift */; };
19AD6FC11CF1E50C00900ECB /* Project_09___MusicPlayerUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19AD6FC01CF1E50C00900ECB /* Project_09___MusicPlayerUITests.swift */; };
19AD6FCF1CF1E52C00900ECB /* Fun. - All Alone.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 19AD6FCE1CF1E52C00900ECB /* Fun. - All Alone.mp3 */; };
19AD6FD11CF207E700900ECB /* CustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19AD6FD01CF207E700900ECB /* CustomButton.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -49,6 +50,7 @@
19AD6FC01CF1E50C00900ECB /* Project_09___MusicPlayerUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Project_09___MusicPlayerUITests.swift"; sourceTree = "<group>"; };
19AD6FC21CF1E50C00900ECB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
19AD6FCE1CF1E52C00900ECB /* Fun. - All Alone.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "Fun. - All Alone.mp3"; sourceTree = "<group>"; };
19AD6FD01CF207E700900ECB /* CustomButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -100,6 +102,7 @@
isa = PBXGroup;
children = (
19AD6FA01CF1E50B00900ECB /* AppDelegate.swift */,
19AD6FD01CF207E700900ECB /* CustomButton.swift */,
19AD6FA21CF1E50B00900ECB /* ViewController.swift */,
19AD6FA41CF1E50B00900ECB /* Main.storyboard */,
19AD6FA71CF1E50B00900ECB /* Assets.xcassets */,
Expand Down Expand Up @@ -262,6 +265,7 @@
files = (
19AD6FA31CF1E50B00900ECB /* ViewController.swift in Sources */,
19AD6FA11CF1E50B00900ECB /* AppDelegate.swift in Sources */,
19AD6FD11CF207E700900ECB /* CustomButton.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "COA.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Pause_1300px_1180711_easyicon.net.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Play_1300px_1180713_easyicon.net.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "976634ca87197104d199c205d1e51470.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,7 +16,6 @@
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,13 +15,93 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="rr9-tw-2HS" userLabel="bgImage">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<constraints>
<constraint firstAttribute="width" constant="375" id="cSB-vF-tQn"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="cSB-vF-tQn"/>
</mask>
</variation>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="JR0-Yo-lrR" userLabel="albumImage">
<rect key="frame" x="87" y="111" width="200" height="200"/>
<constraints>
<constraint firstAttribute="height" constant="200" id="81G-EA-h5f"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0YO-Z4-BJg" userLabel="song">
<rect key="frame" x="107" y="356" width="160" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="LLA-VK-8W2"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eR3-kU-h16" userLabel="artist">
<rect key="frame" x="147" y="404" width="80" height="40"/>
<constraints>
<constraint firstAttribute="width" constant="80" id="9yi-wy-zii"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NwL-KG-a0a" userLabel="play" customClass="CustomButton" customModule="Project_09___MusicPlayer" customModuleProvider="target">
<rect key="frame" x="150" y="491" width="75" height="75"/>
<constraints>
<constraint firstAttribute="height" constant="75" id="KAK-od-lb2"/>
<constraint firstAttribute="width" constant="75" id="paG-Ax-zpG"/>
</constraints>
<state key="normal" title="Button"/>
<connections>
<action selector="playMusicButtonDidTouch:" destination="BYZ-38-t0r" eventType="touchUpInside" id="wvq-l0-tsS"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="eR3-kU-h16" firstAttribute="top" secondItem="0YO-Z4-BJg" secondAttribute="bottom" constant="8" symbolic="YES" id="05l-Te-fLd"/>
<constraint firstItem="rr9-tw-2HS" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="73m-iM-6cy"/>
<constraint firstItem="JR0-Yo-lrR" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="91" id="7MJ-mk-jRS"/>
<constraint firstItem="0YO-Z4-BJg" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="91" id="8Z1-FA-Eys"/>
<constraint firstItem="0YO-Z4-BJg" firstAttribute="centerX" secondItem="eR3-kU-h16" secondAttribute="centerX" id="9Tt-ZG-E8E"/>
<constraint firstItem="rr9-tw-2HS" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="Dwk-mi-EQ2"/>
<constraint firstItem="JR0-Yo-lrR" firstAttribute="centerX" secondItem="0YO-Z4-BJg" secondAttribute="centerX" id="JAL-ew-S8y"/>
<constraint firstItem="NwL-KG-a0a" firstAttribute="top" secondItem="eR3-kU-h16" secondAttribute="bottom" constant="47" id="KEo-cY-Xn1"/>
<constraint firstItem="rr9-tw-2HS" firstAttribute="centerX" secondItem="eR3-kU-h16" secondAttribute="centerX" id="MD6-Qb-vMC"/>
<constraint firstItem="NwL-KG-a0a" firstAttribute="centerX" secondItem="eR3-kU-h16" secondAttribute="centerX" id="MQ7-m4-D4n"/>
<constraint firstItem="JR0-Yo-lrR" firstAttribute="centerY" secondItem="rr9-tw-2HS" secondAttribute="centerY" id="QDb-x6-tO7"/>
<constraint firstItem="0YO-Z4-BJg" firstAttribute="top" secondItem="JR0-Yo-lrR" secondAttribute="bottom" constant="45" id="Td7-Gs-Nu2"/>
<constraint firstItem="JR0-Yo-lrR" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="71" id="Ww6-eu-4sA"/>
<constraint firstAttribute="trailing" secondItem="rr9-tw-2HS" secondAttribute="trailing" id="cmq-Lb-JrQ"/>
<constraint firstItem="rr9-tw-2HS" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="nn3-uT-CMu"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="NwL-KG-a0a" secondAttribute="bottom" constant="101" id="q4v-CC-BlI"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="QDb-x6-tO7"/>
</mask>
</variation>
</view>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
<connections>
<outlet property="albumImage" destination="JR0-Yo-lrR" id="Iv7-dh-TXo"/>
<outlet property="artistLabel" destination="eR3-kU-h16" id="osI-0n-CI9"/>
<outlet property="bgImage" destination="rr9-tw-2HS" id="eEx-vP-j1R"/>
<outlet property="playButon" destination="NwL-KG-a0a" id="bog-l2-o8D"/>
<outlet property="songLabel" destination="0YO-Z4-BJg" id="3oq-S9-0W2"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="142.5" y="269.5"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// CustomButton.swift
// Project 09 - MusicPlayer
//
// Created by 陈炯 on 16/5/22.
// Copyright © 2016年 Jiong. All rights reserved.
//

import UIKit

class CustomButton: UIButton {

var isPlay: Bool = true
let imgPlay: UIImage = UIImage(named: "Play")!
let imgPause: UIImage = UIImage(named: "Pause")!

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addTarget(self, action: "onClick", forControlEvents: UIControlEvents.TouchUpInside)
}

func onClick() {
isPlay = !isPlay
if isPlay {
self.setImage(imgPause, forState: UIControlState.Normal)
} else {
self.setImage(imgPlay, forState: UIControlState.Normal)
}
}

func onPlay() {
isPlay = true
self.setImage(imgPause, forState: UIControlState.Normal)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,76 @@
//

import UIKit
import AVFoundation

class ViewController: UIViewController {


@IBOutlet weak var playButon: CustomButton!
@IBOutlet weak var bgImage: UIImageView!
@IBOutlet weak var songLabel: UILabel!
@IBOutlet weak var artistLabel: UILabel!
@IBOutlet weak var albumImage: UIImageView!

var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//背景模糊效果
bgImage.image = UIImage(named: "COA")
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
bgImage.addSubview(blurEffectView)


//歌曲信息
albumImage.image = UIImage(named: "fun")
albumImage.layer.cornerRadius = 100

songLabel.textColor = UIColor.grayColor()
songLabel.text = "All Alone"
artistLabel.textColor = UIColor.grayColor()
artistLabel.text = "Fun."

playButon.setImage(UIImage(named: "Play"), forState: UIControlState.Normal)

}

//播放音乐
@IBAction func playMusicButtonDidTouch(sender: CustomButton) {

let song = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Fun. - All Alone", ofType: "mp3")!)

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOfURL: song)

audioPlayer.prepareToPlay()
audioPlayer.play()
}
catch let playError as NSError {
print(playError)
}
if sender.isPlay {
audioPlayer.play()
} else {
audioPlayer.stop()
}
}

//点击播放(暂停)按钮的方法
func onPlay(button: CustomButton) {
if button.isPlay {
audioPlayer.play()
} else {
audioPlayer.stop()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


Expand Down

0 comments on commit 6272095

Please sign in to comment.