【问题标题】:Audio played using AVAudioPlayer is working in iPhone Simulator but not in the Device使用 AVAudioPlayer 播放的音频在 iPhone 模拟器中有效,但在设备中无效
【发布时间】:2012-06-28 08:32:05
【问题描述】:

我需要播放一段视频以及一段 2 秒的小音频。在 iPhone 模拟器中一切正常,但在设备中却不行。我将音频文件存储在项目包中并从那里播放音频。

NSString *gotItsoundFilePath = [[NSBundle mainBundle] pathForResource:@"gotit" ofType:@"mp3"];
    NSData *gotItsampleData = [[NSData alloc] initWithContentsOfFile:gotItsoundFilePath];
    NSError *gotItaudioError = nil;
    gotItAudioPlayer = [[AVAudioPlayer alloc] initWithData:gotItsampleData error:&gotItaudioError];
    gotItAudioPlayer.numberOfLoops = 0;

当我使用[gotItAudioPlayer play]; 时,此代码在 iPhone 模拟器中运行良好,但相同的构建在设备中不起作用。我在某处做错了吗?有人可以帮忙吗?

【问题讨论】:

  • 设备和模拟器的ios版本是多少?

标签: iphone ios audio avaudioplayer


【解决方案1】:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

把这个代码。在你制作玩家对象之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多