views:

50

answers:

2

Hello everyone i need to convert this code to a function with 2 parameters in this case the first parameter is News and the second one aif can u do this ??

-(IBAction)news
{
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Get the URL to the sound file to play
soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
                                             CFSTR ("News"),
                                             CFSTR ("aif"),
                                             NULL);

// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

AudioServicesPlaySystemSound (soundFileObject);

return;

}

A: 

If this is meant to be an action received by a class from a user using a control, then the only thing you can do is -(IBAction)nameOfAction:(id)sender;. Otherwise, then you need to write the command to take the two arguments you require and call that from an IBAction.

Philip Regan
+3  A: 

If the CFSTR thing is the problem, then the solution is:

-(void) playNews: (NSString*) news type: (NSString*) type
{
    CFURLRef        soundFileURLRef;
    SystemSoundID   soundFileObject;
    CFBundleRef mainBundle;
    mainBundle = CFBundleGetMainBundle ();

    // Get the URL to the sound file to play
    soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
            (CFStringRef)news,
            (CFStringRef)type,
            NULL);

    // Create a system sound object representing the sound file
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

    AudioServicesPlaySystemSound (soundFileObject);
}

But the (IBAction) declaration makes no sense here because this method cannot be directly used from Interface Builder.

Codo
hello thanks for the answer and this is what i want but When I write PlayNews:@"news1" ofType:@"wav"; the xcode give me an error: expected ';' before 'type' can you help me sovle that
Bobj-C
You need to put it in angular brackets: [obj playNews: @"news" ofType: @"wav"];
Codo