草庐IT

ios - AVAudioPlayer 在 iOS 设备上运行,在模拟器中崩溃。我应该担心吗?

coder 2023-09-22 原文

我不知道问题的原因是什么,当播放器尝试播放测试音频文件时它崩溃了,但只在模拟器中。在真实设备上它运行良好。如果你想看一下,我已经粘贴了下面的代码,包括它给我的一些调试信息......如果我找不到这个崩溃的根源,只要它运行良好,它会通过 Apple 的 QA 程序吗在设备本身上?

这个 isa -> unknown class 可能是问题所在吗?

在此方法中,它在 [p play] 上崩溃:

-(void)startPlaybackForPlayer:(AVAudioPlayer*)p
{

    if ([p play])
    {
        [self updateViewForPlayerState:p];
    }
    else
        NSLog(@"Could not play %@\n", p.url);
}

-

 #import "AudioPlayerViewController.h"

@interface AudioPlayerViewController ()
{
    //AVAudioPlayer *player;
}
@property (nonatomic,strong) AVAudioPlayer *player;

@end

@implementation AudioPlayerViewController



-(void)updateCurrentTimeForPlayer:(AVAudioPlayer *)p
{
    self.currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)p.currentTime / 60, (int)p.currentTime % 60, nil];
    self.progressBar.value = p.currentTime;
}

- (void)updateCurrentTime
{
    [self updateCurrentTimeForPlayer:self.player];
}

- (void)updateViewForPlayerState:(AVAudioPlayer *)p
{
    [self updateCurrentTimeForPlayer:p];

    if (self.updateTimer)
        [self.updateTimer invalidate];

    if (p.playing)
    {
        [self.playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
        //[lvlMeter_in setPlayer:p];
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
    }
    else
    {
        [self.playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
        //[lvlMeter_in setPlayer:nil];
        self.updateTimer = nil;
    }

}

- (void)updateViewForPlayerStateInBackground:(AVAudioPlayer *)p
{
    [self updateCurrentTimeForPlayer:p];

    if (p.playing)
    {
        [self.playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
    }
    else
    {
        [self.playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
    }
}

-(void)updateViewForPlayerInfo:(AVAudioPlayer*)p
{
    self.duration.text = [NSString stringWithFormat:@"%d:%02d", (int)p.duration / 60, (int)p.duration % 60, nil];
    self.progressBar.maximumValue = p.duration;
    self.volumeSlider.value = p.volume;
}

- (void)rewind
{
    //AVAudioPlayer *p = rewTimer.userInfo;
    //p.currentTime-= SKIP_TIME;
    //[self updateCurrentTimeForPlayer:p];
}

- (void)ffwd
{
    //AVAudioPlayer *p = ffwTimer.userInfo;
    //p.currentTime+= SKIP_TIME;
    //[self updateCurrentTimeForPlayer:p];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"### AudioPlayerViewController Initiallized");


    playBtnBG = [UIImage imageNamed:@"play.png"]; // Retain, maybe make strong
    pauseBtnBG = [UIImage imageNamed:@"pause.png"];

    [self.playButton setImage:playBtnBG forState:UIControlStateNormal];

    [self registerForBackgroundNotifications];

    self.updateTimer = nil;
    //rewTimer = nil;
    //ffwTimer = nil;

    self.duration.adjustsFontSizeToFitWidth = YES;
    self.currentTime.adjustsFontSizeToFitWidth = YES;
    self.progressBar.minimumValue = 0.0;

    // Load the the sample file, use mono or stero sample
#warning samplefile does not exist
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"bubbles" ofType:@"m4a"]];
    //NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sample2ch" ofType:@"m4a"]];

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    if (self.player)
    {
        self.fileName.text = [NSString stringWithFormat: @"%@ (%d ch.)", [[self.player.url relativePath] lastPathComponent], self.player.numberOfChannels, nil];
        [self updateViewForPlayerInfo:self.player];
        [self updateViewForPlayerState:self.player];
        self.player.numberOfLoops = 1;
        self.player.delegate = self;
    }

    OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);
    if (result)
        NSLog(@"Error initializing audio session! %ld", result);

    [[AVAudioSession sharedInstance] setDelegate: self];
    NSError *setCategoryError = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
    if (setCategoryError)
        NSLog(@"Error setting category! %@", setCategoryError);

#warning did change here - migth cause bug
    //result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, (__bridge void *)(self));
    if (result)
        NSLog(@"Could not add property listener! %ld", result);





    /*
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    self.player.delegate = self;
    [self.player prepareToPlay];
    [self playAudio];
     */
}

-(void)pausePlaybackForPlayer:(AVAudioPlayer*)p
{
    [p pause];
    [self updateViewForPlayerState:p];
}

-(void)startPlaybackForPlayer:(AVAudioPlayer*)p
{

    if ([p play])
    {
        [self updateViewForPlayerState:p];
    }
    else
        NSLog(@"Could not play %@\n", p.url);
}

- (IBAction)playButtonPressed:(UIButton *)sender
{
    if (self.player.playing == YES)
        [self pausePlaybackForPlayer: self.player];
    else
        [self startPlaybackForPlayer: self.player];
}

- (IBAction)rewButtonPressed:(UIButton *)sender
{
    /*
    if (rewTimer) [rewTimer invalidate];
    rewTimer = [NSTimer scheduledTimerWithTimeInterval:SKIP_INTERVAL target:self selector:@selector(rewind) userInfo:player repeats:YES];
     */
}

- (IBAction)rewButtonReleased:(UIButton *)sender
{
    /*
    if (rewTimer) [rewTimer invalidate];
    rewTimer = nil;
     */
}

- (IBAction)ffwButtonPressed:(UIButton *)sender
{
    /*
    if (ffwTimer) [ffwTimer invalidate];
    ffwTimer = [NSTimer scheduledTimerWithTimeInterval:SKIP_INTERVAL target:self selector:@selector(ffwd) userInfo:player repeats:YES];
     */
}

- (IBAction)ffwButtonReleased:(UIButton *)sender
{
    /*
    if (ffwTimer) [ffwTimer invalidate];
    ffwTimer = nil;
     */
}

- (IBAction)volumeSliderMoved:(UISlider *)sender
{
    self.player.volume = [sender value];
}

- (IBAction)progressSliderMoved:(UISlider *)sender
{
    self.player.currentTime = sender.value;
    [self updateCurrentTimeForPlayer:self.player];
}



#pragma mark AudioSession handlers
/**
void RouteChangeListener(   void *                  inClientData,
                         AudioSessionPropertyID inID,
                         UInt32                  inDataSize,
                         const void *            inData)
{
    avTouchController* This = (avTouchController*)inClientData;

    if (inID == kAudioSessionProperty_AudioRouteChange) {

        CFDictionaryRef routeDict = (CFDictionaryRef)inData;
        NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));

        int reason = [reasonValue intValue];

        if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

            [This pausePlaybackForPlayer:This.player];
        }
    }
}*/

#pragma mark AVAudioPlayer delegate methods

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag
{
    if (flag == NO)
        NSLog(@"Playback finished unsuccessfully");

    [p setCurrentTime:0.];
    if (self.inBackground)
    {
        [self updateViewForPlayerStateInBackground:p];
    }
    else
    {
        [self updateViewForPlayerState:p];
    }
}

- (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error
{
    NSLog(@"ERROR IN DECODE: %@\n", error);
}

// we will only get these notifications if playback was interrupted
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p
{
    NSLog(@"Interruption begin. Updating UI for new state");
    // the object has already been paused,  we just need to update UI
    if (self.inBackground)
    {
        [self updateViewForPlayerStateInBackground:p];
    }
    else
    {
        [self updateViewForPlayerState:p];
    }
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)p
{
    NSLog(@"Interruption ended. Resuming playback");
    [self startPlaybackForPlayer:p];
}

#pragma mark background notifications
- (void)registerForBackgroundNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(setInBackgroundFlag)
                                                 name:UIApplicationWillResignActiveNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(clearInBackgroundFlag)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
}

- (void)setInBackgroundFlag
{
    self.inBackground = true;
}

- (void)clearInBackgroundFlag
{
    self.inBackground = false;
}










- (void)playAudio
{
    // Move operation to new thread
    dispatch_queue_t playAudiofile = dispatch_queue_create("play audiofile", NULL);
    dispatch_async(playAudiofile, ^{
        NSLog(@"Playing audiofile in new thread: %@", self.audiofile);
        NSAssert(![self.audiofile isEqualToString:@""], @"audiofile must be specified.");

        NSError *audioError;
        NSString *soundFilePath = [[NSBundle mainBundle]pathForResource:self.audiofile ofType:@"mp3"];
        NSAssert(soundFilePath != NULL, @"Sound File Not found");
        NSURL *soundFileURL = [NSURL URLWithString:soundFilePath];

        self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&audioError];
        [self.player play];
    });
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

frame #0: 0x020b4b54 libc++abi.dylib`__cxa_throw
frame #1: 0x0e215a41 AudioCodecs`ACBaseCodec::GetProperty(unsigned long, unsigned long&, void*) + 757
frame #2: 0x0e237618 AudioCodecs`ACMP4AACBaseDecoder::GetProperty(unsigned long, unsigned long&, void*) + 2458
frame #3: 0x0e235b83 AudioCodecs`ACMP4AACLowComplexityDecoder::GetProperty(unsigned long, unsigned long&, void*) + 565
frame #4: 0x0e28d4b4 AudioCodecs`GetProperty(void*, unsigned long, unsigned long*, void*) + 47
frame #5: 0x001347a8 AudioToolbox`AudioCodecGetProperty + 88
frame #6: 0x000392db AudioToolbox`CodecConverter::CheckInitialize(void const*, unsigned long) + 523
frame #7: 0x00038f34 AudioToolbox`CodecConverter::CodecConverter(OpaqueAudioComponent*&, OpaqueAudioComponentInstance*, StreamDescPair const&, StreamDescPair const&, bool, AudioConverterPrimeInfo const&) + 676
frame #8: 0x00038c7e AudioToolbox`CodecConverter::CodecConverter(OpaqueAudioComponent*&, OpaqueAudioComponentInstance*, StreamDescPair const&, StreamDescPair const&, bool, AudioConverterPrimeInfo const&) + 62
frame #9: 0x0003ddf7 AudioToolbox`CodecDecoderFactory::BuildCodecConverterChain(StreamDescPair const&, ChainBuildSettings const&, AudioConverterChain*, PCMConverterFactory*) + 631
frame #10: 0x0002647c AudioToolbox`ConverterRegistry::CreateConverter(StreamDescPair const&, AudioConverterChain**, unsigned long, AudioClassDescription const*) + 204
frame #11: 0x00019ba0 AudioToolbox`_AudioConverterNewInternal + 352
frame #12: 0x00060a00 AudioToolbox`AudioQueueObject::ConverterConnection::BuildConverter() + 416
frame #13: 0x000607e2 AudioToolbox`AudioQueueObject::ConverterConnection::ConverterConnection(AudioQueueObject&, long&) + 130
frame #14: 0x000620bc AudioToolbox`AudioQueueObject::IONodeConnection::GetConverterConnection_Init(long&) + 60
frame #15: 0x00065803 AudioToolbox`AudioQueueObject::SetProperty(unsigned long, CADeserializer&) + 1123
frame #16: 0x0008c44a AudioToolbox`AQServer_SetProperty + 106
frame #17: 0x00092276 AudioToolbox`AudioQueueSetProperty + 374
frame #18: 0x004c1d2b AVFoundation`AVAudioPlayerCpp::allocAudioQueue() + 683
frame #19: 0x004c13be AVFoundation`AVAudioPlayerCpp::prepareToPlayQueue() + 26
frame #20: 0x004bfebe AVFoundation`AVAudioPlayerCpp::prepareToPlay() + 84
frame #21: 0x004129f7 AVFoundation`-[AVAudioPlayer prepareToPlay] + 59
frame #22: 0x00006b7e SteinerAudio`-[AudioPlayerViewController viewDidLoad](self=0x07ad3cf0, _cmd=0x00b98a77) + 3694 at AudioPlayerViewController.mm:160
frame #23: 0x006c71c7 UIKit`-[UIViewController loadViewIfRequired] + 536
frame #24: 0x006c7232 UIKit`-[UIViewController view] + 33
frame #25: 0x006c74da UIKit`-[UIViewController contentScrollView] + 36
frame #26: 0x006de8e5 UIKit`-[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 36
frame #27: 0x006de9cb UIKit`-[UINavigationController _layoutViewController:] + 43
frame #28: 0x006dec76 UIKit`-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 254
frame #29: 0x006ded71 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 72
frame #30: 0x006df89b UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 386
frame #31: 0x006dfe93 UIKit`-[UINavigationController pushViewController:transition:forceImmediate:] + 1030
frame #32: 0x006dfa88 UIKit`-[UINavigationController pushViewController:animated:] + 62
frame #33: 0x00a3be63 UIKit`-[UIStoryboardPushSegue perform] + 1111
frame #34: 0x00a2db99 UIKit`-[UIStoryboardSegueTemplate _perform:] + 174
frame #35: 0x00a2dc14 UIKit`-[UIStoryboardSegueTemplate perform:] + 115
frame #36: 0x00695249 UIKit`-[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1134
frame #37: 0x006954ed UIKit`-[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 201
frame #38: 0x0109f5b3 Foundation`__NSFireDelayedPerform + 380
frame #39: 0x0199f376 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #40: 0x0199ee06 CoreFoundation`__CFRunLoopDoTimer + 534
frame #41: 0x01986a82 CoreFoundation`__CFRunLoopRun + 1810
frame #42: 0x01985f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #43: 0x01985e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #44: 0x028557e3 GraphicsServices`GSEventRunModal + 88
frame #45: 0x02855668 GraphicsServices`GSEventRun + 104
frame #46: 0x005e5ffc UIKit`UIApplicationMain + 1211
frame #47: 0x0000237d SteinerAudio`main(argc=1, argv=0xbffff3a0) + 141 at main.m:16

最佳答案

在 XCode 中禁用 C++/所有异常断点。或者编辑断点并将其设置为 objectiveC。

关于ios - AVAudioPlayer 在 iOS 设备上运行,在模拟器中崩溃。我应该担心吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17259902/

有关ios - AVAudioPlayer 在 iOS 设备上运行,在模拟器中崩溃。我应该担心吗?的更多相关文章

  1. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  2. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  3. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  7. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. ruby-on-rails - 在这种情况下我如何模拟一个对象?没有明显的方法可以用模拟替换对象 - 2

    假设我在Store的模型中有这个非常简单的方法:defgeocode_addressloc=Store.geocode(address)self.lat=loc.latself.lng=loc.lngend如果我想编写一些不受地理编码服务影响的测试脚本,这些脚本可能已关闭、有限制或取决于我的互联网连接,我该如何模拟地理编码服务?如果我可以将地理编码对象传递到该方法中,那将很容易,但我不知道在这种情况下该怎么做。谢谢!特里斯坦 最佳答案 使用内置模拟和stub的rspecs,你可以做这样的事情:setupdo@subject=MyCl

  9. ruby-on-rails - 禁用设备的 :confirmable on-the-fly to batch-generate users - 2

    Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation

  10. ruby - "public/protected/private"方法是如何实现的,我该如何模拟它? - 2

    在ruby中,你可以这样做:classThingpublicdeff1puts"f1"endprivatedeff2puts"f2"endpublicdeff3puts"f3"endprivatedeff4puts"f4"endend现在f1和f3是公共(public)的,f2和f4是私有(private)的。内部发生了什么,允许您调用一个类方法,然后更改方法定义?我怎样才能实现相同的功能(表面上是创建我自己的java之类的注释)例如...classThingfundeff1puts"hey"endnotfundeff2puts"hey"endendfun和notfun将更改以下函数定

随机推荐