sábado, 2 de marzo de 2013

Reutilizar metodos para reproducir varios sonidos o mostrar alertas diferentes.

Tutoriales XCode.



En este pequeño tutorial vamos a ver como usar un metodo para reproducir varios sonidos diferentes y tambien otro metodo para mostrar varias alertas diferentes.

Al hacer la llamada al método le pasamos en una variable el texto que queremos que nos muestre y en el otro caso el nombre del archivo de audio que queremos que reproduzca.











Archivo de cabecera ViewController.h.


//
//  ViewController.h
//  PruebasVarias
//
//  Created by Francisco on 02/03/13.
//  Copyright (c) 2013 Francisco. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> //Hay que importar el Framework de sonido

@interface ViewController : UIViewController
@property (strong,nonatomic) AVAudioPlayer *reproductor;
@property (strong,nonatomic) NSString * idAudio;
@property (strong,nonatomic) UIAlertView * alerta;
@property (strong,nonatomic) NSString * idAlerta;
@property (strong,nonatomic) NSString * sonido;
- (IBAction)mostrarAlerta:(id)sender;
- (IBAction)reproducirSonido:(id)sender;


-(void)nombreSonido: (NSString *)recibeDatos;
-(void)textoAlertas: (NSString*)textoQueMuestra;

- (IBAction)alerta2:(id)sender;
- (IBAction)alerta3:(id)sender;
- (IBAction)sonido2:(id)sender;
- (IBAction)sonido3:(id)sender;


@end



Archivo de implementación ViewController.m.

//
//  ViewController.m
//  PruebasVarias
//
//  Created by Francisco on 02/03/13.
//  Copyright (c) 2013 Francisco. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize reproductor,idAlerta,idAudio,alerta,sonido;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)mostrarAlerta:(id)sender
{
    [self textoAlertas:@"Alerta Uno"];
}

- (IBAction)reproducirSonido:(id)sender {
    [self nombreSonido:@"Alerta"];
    
    
}

-(void)nombreSonido: (NSString *)recibeDatos
{
  
    NSError *error;
    NSString *ruta =[[NSBundle mainBundle]pathForResource:recibeDatos ofType:@"mp3"];
    NSURL *url =[[NSURL alloc]initFileURLWithPath:ruta];
    self.reproductor=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
        
       [self.reproductor prepareToPlay];
        [self.reproductor play];
}
-(void)textoAlertas: (NSString*)textoQueMuestra
{
    alerta =[[UIAlertView alloc]initWithTitle:textoQueMuestra message:@"Texto Mostrado" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alerta show];
}

- (IBAction)alerta2:(id)sender
{
    [self textoAlertas:@"Alerta Dos"];
}

- (IBAction)alerta3:(id)sender
{
    [self textoAlertas:@"Alerta Tres"];
}

- (IBAction)sonido2:(id)sender
{
    
    [self nombreSonido:@"Alerta2"];
}

- (IBAction)sonido3:(id)sender
{
    [self nombreSonido:@"Tap"];
}

@end



Bueno espero les guste y les sirva para algo, para cualquier duda o sugerencia dejen sus comentarios.

gracias.