objective c - Audio player works in simulator but not on iOS device -
i have following code initializing:
nsstring *path = [nsstring stringwithformat:@"%@/test.mp3", [[nsbundle mainbundle] resourcepath]]; nsurl *soundurl = [nsurl fileurlwithpath:path]; _audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:soundurl error:nil];
then button starts player
[_audioplayer play];
so works fine when on simulator doesn't work on device.
the proper way path of file in app bundle this:
nsstring *path = [[nsbundle mainbundle] pathforresource:@"test" oftype:@"mp3"];
but if want nsurl
, use following directly:
nsurl *soundurl = [[nsbundle mainbundle] urlforresource:@"test" withextension:@"mp3"];
also, simulator isn't case sensitive real device is. make sure file named test.mp3
, not test.mp3
. update name in code match actual filename.
use debugger make sure soundurl
isn't nil
. if not audio player still doesn't work, use error
parameter see why:
nserror *error = nil; _audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:soundurl error:&error]; if (!_audioplayer) { nslog(@"unable create audio player: %@", error); }