2017-05-11 6 views
0

Une question très commune, mais je ne peux pas comprendre ce que je fais mal à jouer un fichier audio simple dans iOS 10 et 9. J'ai le code suivant:Jouer un simple fichier audio dans iOS

#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h> 

    NSString *soundFilePath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"www/sounds/ringbacktone" ofType:@"mp3"]]; 
    [self playTone:soundFilePath Loop:YES]; 

-(void) playTone:(NSString *) soundFilePath Loop:(BOOL) loop{ 


    NSLog(@"Sound File Path: %@",soundFilePath); 


    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:soundFilePath]){ 

     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

     NSError *error = nil; 
     AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; 

     if (error){ 
      NSLog(@"Error creating the audio player: %@",error); 
     }else{ 
      if (loop == YES) 
       player.numberOfLoops = -1; //Infinite 

      [player setVolume:1.0]; 
      [player play]; 
     } 

    }else{ 
     NSLog(@"No sound will be played. The file doesn't exist."); 
    } 
} 

la sortie est

Sound File Path: /var/containers/Bundle/Application/E8CCA88C-B6AB-4C36-9426-EFBB94E1D509/myapp.app/www/sounds/myfile.mp3 

le fichier existe pour que le son doit jouer. J'ai essayé les fichiers wav, m4a et mp3 sans succès. J'utilise la fonction avant d'appeler la bibliothèque pjsip. Je ne sais pas si cela joue un rôle.

Des pensées? Y a-t-il un autre moyen de le déboguer davantage?

+0

Êtes-vous activé la session audio? Quelle catégorie utilisez-vous? –

+0

AVAudioSessionCategoryPlayAndRecord –

Répondre

0

vérifier cette solution, il peut fonctionner:

propriété 1.Créez ur lecteur audio:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

2.Write méthodes de délégué pour le joueur: AVAudioPlayerDelegate

3. mettre en œuvre ceci dans votre action:

yourSoundArray=[NSArray arrayWithObjects:@"sound1",@"sound2",@"sound3",@"sound4",@"sound5",@"sound6", nil];//dont include formate type 


    NSString *audioPath = [[NSBundle mainBundle] pathForResource: [yourSoundArray objectAtIndex:indexPath.row] ofType:@"mp3"]; 
        NSLog(@"%@",audioPath); 

        NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 

        NSError *audioError = [[NSError alloc] init]; 
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError]; 
        audioPlayer.delegate=self; 

       if (!audioError) { 
         NSLog(@"playing!"); 
         audioPlayer play]; 
        } 
        else { 
          NSLog(@"Error!"); 
        } 
  1. contrôle l'action joueur délégué audio

    -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
           { NSLog(@"%d",playing the audio); 
           } 
    
0

La réponse est une combinaison de ceux-ci: dans le fichier .h je devais hériter de AVAudioPlayerDelegate, à savoir

#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h> 
@interface scAudioManager : NSObject <AVAudioPlayerDelegate>{ 
@property (strong, nonatomic) AVAudioPlayer *player; 
} 

Et puis dans le fichier .m:

NSString *soundFilePath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"www/sounds/ringbacktone" ofType:@"mp3"]]; 
[self playTone:soundFilePath Loop:YES]; 

- (void) Playtone: (NSString *) soundFilePath Loop: boucle (BOOL) {

NSLog(@"Sound File Path: %@",soundFilePath); 


NSFileManager *fileManager = [NSFileManager defaultManager]; 
if ([fileManager fileExistsAtPath:soundFilePath]){ 

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    NSError *error = nil; 
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; 

    if (error){ 
     NSLog(@"Error creating the audio player: %@",error); 
    }else{ 
     if (loop == YES) 
      _player.numberOfLoops = -1; //Infinite 

     _player.volume=[[AVAudioSession sharedInstance] outputVolume]; 
     _player.delegate = self; 
     [_player prepareToPlay] 
     [_player play]; 
    } 

}else{ 
    NSLog(@"No sound will be played. The file doesn't exist."); 
    } 
}