AVAudioPlayer没有播放任何声音

时间:2011-11-04 23:09:59

标签: objective-c audio xcode4 avfoundation automatic-ref-counting

我正在开发一个需要使用AVFoundation框架播放声音的iOS应用程序。 Xcode 4中的工作区结构包含两个项目:

  • 工作区
    • 应用程序本身(主项目)
    • 实用程序库

在构建实用程序库之后,它会生成一个静态库,该库在主应用程序中用作框架。

因此,当尝试使用下面的代码在主应用程序中播放声音时,它会按预期工作。

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *path = [NSString stringWithFormat:@"%@/sound.mp3", resourcePath];

NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                                    error:&error];
[audioPlayer play];

相反,当尝试使用与上面相同的代码在实用程序库中播放完全相同的声音(或任何其他声音)时,即使错误 nil ,也根本没有播放声音, audioPlayer 属性值是正确的值(通道数,持续时间)。

我确保AVFoundation框架都在两个项目中。

此外,我的类使用AVAudioPlayerDelegate协议并实现这两种方法:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;

尝试播放声音后,没有调用这些方法。

如果我使用AudioToolbox框架,那么它会播放声音。但是我有兴趣使用AVFoundation有几个原因。

知道发生了什么事吗?我错过了AVFoundation的内容吗?它可能与在静态库中使用AVAudioPlayer有关吗?​​

提前致谢。

11 个答案:

答案 0 :(得分:148)

我找到了解决方案,它与我没有提及但没有考虑的事情有关:我正在使用自动引用计数(ARC)进行编译。

ARC向音频播放器插入一个释放调用,因此在离开创建它的方法后立即取消分配。它与AVAudioPlayer无关,但与ARC无关。

为了解决这个问题,我刚刚在类中创建了一个处理声音的AVAudioPlayer属性,以便它不再被ARC释放。好吧,至少,在发布此类的实例之前,它不会被释放。

有关ARC的详细信息,请参阅Automatic Reference Counting: Zeroing Weak References,详细说明我遇到此问题的原因。

答案 1 :(得分:35)

是的,绝对的; ARC也是我的代码的原因。

我介绍了一个属性:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

这使得一切顺利。

答案 2 :(得分:25)

使用它肯定会起作用....

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

NSURL *url = [NSURL fileURLWithPath:@"path of the sound file :)"];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player setDelegate:self];
[_player prepareToPlay];
[_player play];

答案 3 :(得分:17)

您是否配置了AVAudioSession?

我有类似的问题并使用类似的东西修复它:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:NULL];

希望它有所帮助。

答案 4 :(得分:3)

即使在类范围内声明了audioPlayer var,它也不会播放。 至少在iOS 10 iPhone 5c物理设备上。

//Importing dbdetails file require_once 'dbDetails.php'; //connection to database $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...'); //sql query to fetch all images $sql = "SELECT * FROM images"; //getting images $result = mysqli_query($con,$sql); //response array $response = array(); $response['error'] = false; $response['images'] = array(); //traversing through all the rows while($row = mysqli_fetch_array($result)){ $temp = array(); $temp['id']=$row['id']; $temp['name']=$row['name']; $temp['url']=$row['url']; array_push($response['images'],$temp); } //displaying the response echo json_encode($response); 结果为play(),因此不会出现任何错误。

不得不添加这个( Swift 3,4 ),现在它可以正确播放声音。

true

答案 5 :(得分:0)

我为简单的音频播放器创建了开源项目。如果您在iOS中播放音频有任何问题(在后台也是如此),请查看它:https://github.com/wzbozon/DKAudioPlayer

答案 6 :(得分:0)

请留在页面上一段时间,我遇到同样的问题。并使用睡眠来解决它。


NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:@"sounds-1027-are-you-kidding"
                                             ofType:@"mp3"]];
        AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
                                      initWithContentsOfURL:url
                                      error:nil];
        [audioPlayer play];
        sleep(2);

答案 7 :(得分:0)

我有同样的问题。我通过在.h文件中添加以下行来解决它:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

在.m文件中:

NSError *error;
NSString *soundFilePath = [NSString stringWithFormat:@"%@/dancepechance.mp3",
                               [[NSBundle mainBundle] resourcePath]];
    NSURL *url = [NSURL fileURLWithPath:soundFilePath];
    self.audioPlayer = [[AVAudioPlayer alloc]
                                  initWithContentsOfURL:url
                                  error:&error];
    NSLog(@"Error %@",error);
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer play];

答案 8 :(得分:0)

您可以使用 AVKit 中的 AVPlayerViewController 代替音频和视频。快速和客观c here都可以使用代码。

答案 9 :(得分:0)

对我来说,做得很好

do {
     try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
    } catch {
        assertionFailure("Failed to configure `AVAAudioSession`: \(error.localizedDescription)")
    }

答案 10 :(得分:-1)

Swift Solution:

  import AVFoundation

  // define your player in class
  var player = AVAudioPlayer()

  override func viewDidLoad() {
    super.viewDidLoad() 
  }
相关问题