Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shawn Bolour
2,423 PointsPlaying a song or sound in Swift
I am trying to take the Algorhythm application a step further and add music to the playlist.
I am not sure why my the simulator is not playing the music. I followed the code found in the following link : http://www.reddit.com/r/swift/comments/28izxl/how_do_you_play_a_sound_in_ios/
The only thing I can think of is that the simulator's volume is off. Could this be the issue? If so, then how do I turn it on?
The file AviciiWakeMeUp is located on my desktop.
Your help is much appreciated.
import UIKit import AVFoundation
class PlaylistDetailViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
var playlist: Playlist?
@IBOutlet weak var playlistCoverImage: UIImageView!
@IBOutlet weak var playlistTitle: UILabel!
@IBOutlet weak var playlistDescription: UILabel!
@IBOutlet weak var playlistArtist0: UILabel!
@IBOutlet weak var playlistArtist1: UILabel!
@IBOutlet weak var playlistArtist2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if playlist != nil {
playlistCoverImage.image = playlist!.largeIcon
playlistCoverImage.backgroundColor = playlist!.backgroundColor
playlistTitle.text = playlist!.title
playlistDescription.text = playlist!.description
playlistArtist0.text = playlist!.artists[0]
playlistArtist1.text = playlist!.artists[1]
playlistArtist2.text = playlist!.artists[2]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playA(sender: AnyObject) {
if let path = NSBundle.mainBundle().pathForResource("AviciiWakeMeUp", ofType: "mp3") {
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), fileTypeHint: "mp3", error: nil)
if let sound = audioPlayer {
sound.prepareToPlay()
sound.play()
}
}
}
}
3 Answers
Pasan Premaratne
Treehouse TeacherThe audio file has to be copied to the application. Right click on the group and select "Add to Files". Select the file from your desktop and make sure the box "Destination: Copy items if needed" is checked.
Pasan Premaratne
Treehouse TeacherI opened up your app and hit the button and it worked! Didn't have to do anything on my end :/
Shawn Bolour
2,423 PointsWow! It works! I think I just needed to restart Xcode. Thank you!!!
Kirill Kraynov
2,760 PointsPasan, hello! Could you help me? Hello, guys! I've got two view controllers. The first one is the tableviewcontroller, the second one is detailviewcontroller with play button. So, I can't realize how to pass audio to button. I'm total newbie, but I really need the solution. Please, help me.
It should look like this:
Cell1 -> click -> open detail view, click BUTTON -> play sound 1
Cell2 -> click -> open detail view, click BUTTON -> play sound 2
Cell3 -> click -> open detail view, click BUTTON -> play sound 3
Cell4 -> click -> open detail view, click BUTTON -> play sound 4
Michiko Perry
3,617 PointsIf sound files is in the dictionary, then how to connect each of them it to each image? Is there any place that I can learn this?
Shawn Bolour
2,423 PointsShawn Bolour
2,423 PointsPasan Premaratne,
The legend himself!! Thank you for the response!!
I actually tried to do this. See the following link for a screenshot of what I did:
https://www.dropbox.com/s/9yba3jnh42lrmfb/Screen%20Shot%202015-03-25%20at%207.00.21%20AM.png?dl=0
(Side note questions: Can we add screen shots or pictures to a thread to the treehouse forum without having to reference an external website like Dropbox?)
The "button" on the Main.storyboard is where the IBAction is linked to.
Where do you think I am going wrong because the simulator is still not playing the song when I click the button?
Thank you very much.
Shawn
Pasan Premaratne
Treehouse TeacherPasan Premaratne
Treehouse TeacherShawn Bolour
lol at "legend". Would you mind sending me a zipped file of your project or linking to github if you have it up? I can poke around and see whats going on.
Also, unfortunately you can't upload images directly.
Shawn Bolour
2,423 PointsShawn Bolour
2,423 PointsPasan Premaratne
I realized I did not add the comment to your comment loll sorry.
Here is the message response again:
You can find the zipped file of my project here:
https://www.dropbox.com/sh/2o1gz0jhzrpc41v/AAAuMLIuFqbxQ6h5Ln19BCtQa?dl=0
I do not know how to use Github, but I should probably start learning because I hear it can be very helpful for collaboration
Thank you for taking the time to help.
Shawn
Pasan Premaratne
Treehouse TeacherPasan Premaratne
Treehouse TeacherShawn Bolour,
The entire folder needs to be zipped and not just the Xcode project file. The project file doesn't contain the actual code and resources
Shawn Bolour
2,423 PointsShawn Bolour
2,423 PointsPasan Premaratne
Oh I see. The following link should be the entire folder...
https://www.dropbox.com/s/ddbmks70ei89dxe/Algorhythm2.zip?dl=0
Thank you!
Shawn