Play Audio iOS Objective-C

后端 未结 7 1259
南旧
南旧 2020-11-30 00:25

I\'m trying to get an audio file playing in my iOS app. This is my current code

NSString *soundFilePath = [NSString stringWithFormat:@\"%@/test.m4a\", [[NSBu         


        
相关标签:
7条回答
  • 2020-11-30 00:44

    First import this framework to your file

    #import <AVFoundation/AVFoundation.h>
    

    Then declare an instance of AVAudioPlayer.

    AVAudioPlayer *audioPlayer;
    
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" 
                                                              ofType:@"type of your audio file example: mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    audioPlayer.numberOfLoops = -1;
    [audioPlayer play];
    
    0 讨论(0)
提交回复
热议问题