Audio playback does not start

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

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

audioPlayer.delegate = self;
[audioPlayer          


        
相关标签:
2条回答
  • Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题