Audio playback does not start

∥☆過路亽.° 提交于 2019-12-01 09:27:51

问题


NSError *err;

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

audioPlayer.delegate = self;
[audioPlayer play];

With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.

Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer

The operation couldn’t be completed. (OSStatus error -43.)

回答1:


Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.




回答2:


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).



来源:https://stackoverflow.com/questions/3413932/audio-playback-does-not-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!