2017-05-11 4 views
0

Eine sehr häufige Frage, aber ich kann nicht herausfinden, was in der Wiedergabe eine einfache Audiodatei in iOS 10 und 9. mache mir falsch habe ich den folgenden Code:eine einfache Audio-Datei in iOS Spielen

#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."); 
    } 
} 

der Ausgang ist

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

die Datei existiert, so sollte der Ton spielen. Ich habe wav, m4a und mp3 Dateien ohne Erfolg versucht.

Ich benutze die Funktion vor dem Aufruf pjsip Bibliothek. Nicht sicher, ob es eine Rolle spielt.

Irgendwelche Gedanken? Gibt es eine andere Möglichkeit, es weiter zu debuggen?

+0

Sind Sie Audio-Sitzung aktiviert? Welche Kategorie verwenden Sie? –

+0

AVAudioSessionCategoryPlayAndRecord –

Antwort

0

Check diese Lösung funktioniert kann:

1.Erstellen Eigenschaft für ur Audio-Player:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

2.Write Delegatmethoden für Spieler: AVAudioPlayerDelegate

3. implementieren Sie dies in Ihrer Aktion:

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. Kontrolle der Audio-Player-Delegat Aktion

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

Die Antwort eine Kombination davon war: in .h-Datei ich erben musste AVAudioPlayerDelegate, dh

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

Und dann in .m-Datei:

- (void) playtone: (NSString *) soundFilePath Loop: (BOOL) Schleife {

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."); 
    } 
} 
Verwandte Themen