Audio playback does not start

后端 未结 2 816
暗喜
暗喜 2021-01-15 16:21
NSError *err;

// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];

audioPlayer.delegate = self;
[audioPlayer          


        
2条回答
  •  长情又很酷
    2021-01-15 16:50

    Did you initialize a AVAudioSession?

    The following works fine for me. Might give you a hint of whats not working for you.

    Initializing:

    AVAudioSession* session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryAmbient error:nil];
    [session setActive:TRUE error:nil]; 
    

    Loading:

    AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
    audioPlayer.numberOfLoops = 0;
    [audioPlayer prepareToPlay];
    audioPlayer.volume = 1.0;
    

    Playing:

    [audioPlayer play];
    

    Update

    To find the right NSRUL for fileUrl, I used this code:

    NSURL* fileUrl = [NSURL fileURLWithPath:
         [[NSBundle mainBundle] pathForResource:@"MySound" ofType:@"wav"] isDirectory:NO];
    

    And then I added MySound.wav to the project (bundle).

提交回复
热议问题