ios - Cannot setup AVAudioPlayerDelegate in a Manager class -
i using manager class initialize global variable in appdelegate.swift class manage playing music in app. here's of code manager class
import foundation import avfoundation import mediaplayer /* protocol playerdelegate : class { func soundfinished(sender : anyobject) }*/ var quotelist = [quote]() var israndomquote = false enum alarmtypes { case song case quote case randomquote } var myavaudioplayer:avaudioplayer = avaudioplayer() var mpmediaplayer = mpmusicplayercontroller() public class mediamanager: nsobject, avaudioplayerdelegate{ //weak var delegate : playerdelegate? var mpmediaitem: mpmediaitemcollection! var quoteselected = quote() var alarmtype = alarmtypes.randomquote func selectrandom(){ self.alarmtype = alarmtypes.randomquote quoteselected = pickrandomquote() } func selectsong(mpmedia: mpmediaitemcollection){ self.alarmtype = alarmtypes.song mpmediaitem = mpmedia } func selectquote(quote: quote){ self.alarmtype = alarmtypes.quote quoteselected = quote } var isplaying:bool = false override init(){ super.init() myavaudiomanager.delegate = self }
however when go run code error: thread 1: exc_bad_access(code = 1, address = 0x8)
i have tried making other files delegate has not worked. there way make manager delegate can respond player when finishes playing song?
i think myavaudiomanager still nil when set delegate. try create new instance of avaudioplayer before.
Comments
Post a Comment