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
Kirill Kraynov
2,760 PointsPassing audio from tableview to button in swift
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
1 Answer
Greg Kaleka
39,021 PointsHi Kirill,
I'm a bit rusty on table views, and I've never worked with audio, so this is going to be a bit high-level. I'll take a look at some code from one of my projects tonight and update this answer if necessary.
If you know how to pass other data between table and detail view controllers, this will be no different. You could either pass the audio object (if that's how you're storing it), or just the string that represents the path to the audio file.
If you're passing the path, for example, you could very simply just do something like this:
import MediaPlayer
var mediaPlayer: MPMoviePlayerController = MPMoviePlayerController()
mediaPlayer.contentURL = NSURL(string: "url/passed/to/detail/view")
@IBAction func hitPlay(sender: UIButton) {
mediaPlayer.play()
}
This assumes you know how to pass data from one view to another. Let me know if you need help on how to do that. I'm also not super confident about how to actually play the sound...
Happy coding!
-Greg