Play A Song On App Open?

Discussion in 'iOS Development' started by iPodTouchDev, Jul 22, 2009.

  1. iPodTouchDev

    iPodTouchDev New Member

    Joined:
    Jan 18, 2009
    Messages:
    74
    Likes Received:
    0
    Device:
    iPod touch
    Is there a way to make a certain song play when an app opens? And then make it loop?
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Look at AVFoundation, Audio Toolbox and Core Audio, they're all audio frameworks.
  3. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    You want to be spoon fed?
  5. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    yes please,

    H.file

    [OBJC]#import <AudioToolbox/AudioServices.h>

    SystemSoundID chickenID;

    @property(nonatomic) SystemSoundID chicken;[/OBJC]


    M. File

    [OBJC]- (void)viewDidLoad {
    [super viewDidLoad];

    NSString * chickenPath = [[NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "chicken4eveSound" ofType

    Please Register or Log in to view images

    "mp3"];
    CFURLRef chickenURL = (CFURLRef ) [NSURL fileURLWithPath:chickenPath];
    AudioServicesCreateSystemSoundID (chickenURL, &chickenID);
    }[/OBJC]

    place this where you want the sound...

    [OBJC]AudioServicesPlaySystemSound (chickenID);[/OBJC]


    this is a noob question but how do you add syntax highlighting ?
    and hay presto i got fed...CHICKEN
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    You can't use CoreAudio on the iPhone, the audio daemon hogs the sound card and you can't get to it without killing it. That, of course, would disrupt all the sound on the system.

    To answer the question, use AVFoundation.
  7. iPodTouchDev

    iPodTouchDev New Member

    Joined:
    Jan 18, 2009
    Messages:
    74
    Likes Received:
    0
    Device:
    iPod touch
    [OBJC]error: 'chickenID' undeclared (first use in function)[/OBJC]

    Also - I want to play a FULL song. Not just a short clip.
  8. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
  9. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Ah thanks, I didn’t know that. I'll keep it in mind.
  10. iPodTouchDev

    iPodTouchDev New Member

    Joined:
    Jan 18, 2009
    Messages:
    74
    Likes Received:
    0
    Device:
    iPod touch
    Ok I got it to work. But I have a new problem. I'm trying to play a random song when the app opens.

    [OBJC]srand ( time(NULL) );
    int random = (rand() % 100);
    if(!!(random%2))
    {
    song = @"SongOne";
    }
    else
    {
    song = @"SongTwo";
    }

    if(song == SongOne)
    {
    NSString *path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongOne" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    else
    {
    NSString *path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongTwo" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];[/OBJC]

    It compiles fine, yet it crashes on launch.

    Update: I changed it to:


    [OBJC]srand ( time(NULL) );
    int random = (rand() % 100);
    if(!!(random%2))
    {
    song = @"SongOne";
    }
    else
    {
    song = @"SongTwo";
    }

    if(song == SongOne)
    {
    path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongOne" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    else
    {
    path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongTwo" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];[/OBJC]

    and it opens, yet each time only SongOne plays.

    Also - it seems which ever one has "==" is the one that gets played...

    [OBJC] srand ( time(NULL) );
    int random = (rand() % 100);
    if(!!(random%2))
    {
    song = @"SongOne";
    }
    else
    {
    song = @"SongTwo";
    }

    if(song == SongOne)
    {
    NSString *path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongOne" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    else if(song = SongTwo)
    {
    NSString *path = [[ NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "SongTwo" ofType

    Please Register or Log in to view images

    "mp3" ];
    }

    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];[/OBJC]

    will result in SongOne being the one played. Yet, when both are set to "=" then the app crashes?

Share This Page