iOS Question App crash while using audio play

IamTrying

Active Member
Licensed User
I have to play a ring.wav file when the app is active or in backend, following works but app crashed. Any idea if the crash can be avoided?
I have applied the try catch now before it was not.

B4X:
Sub playAudio()
    Try
        NativeMe.RunMethod("setAudioSession", Null)
        vv.Initialize("vv")
        vv.LoadVideo(File.DirAssets ,"ring.wav")
        vv.Play
        NativeMe.RunMethod("register", Null)
       
    Catch
        Log(LastException)
    End Try
End Sub

#If OBJC
@import MediaPlayer;
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
- (void)setAudioSession {
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *err = nil;
  BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
  if (success) {
  success = [audioSession setActive:YES error:&err];
  }
  if (!success)
  [NSException raise:@"" format:@"Error setting audio session: %@", err];
}
- (void)register {
   MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
   center.playCommand.enabled = true;
   center.pauseCommand.enabled = true;
   [center.playCommand addTarget:self action:@selector(play)];
   [center.pauseCommand addTarget:self action:@selector(pause)];
}
- (void) play {
   NSLog(@"test");
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
}
- (void) pause {
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
}

#end if

LOG
====

B4X:
Class (b4i_httpjob) instance released.
+[CATransaction synchronize] called within transaction
+[CATransaction synchronize] called within transaction
+[CATransaction synchronize] called within transaction
Application_Foreground
>>> Foreground
Application_Active
>>> Active
>>> beatTimer_tick: 26 sr tat vc
JobName = job3, Success = true
Class (b4i_httpjob) instance released.
>>> beatTimer_tick: 27 sr tat vc
JobName = job3, Success = true
>>> beatTimer_tick: 28 sr tat vc
Class (b4i_httpjob) instance released.
JobName = job3, Success = true
>>> beatTimer_tick: 29 sr tat vc
Class (b4i_httpjob) instance released.
JobName = job3, Success = true
Class (b4i_httpjob) instance released.
>>> beatTimer_tick: 30 sr tat vc
JobName = job3, Success = true
Error occurred on line: 31 (HttpUtils2Service)
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Stack Trace: (
  CoreFoundation       <redacted> + 252
  libobjc.A.dylib      objc_exception_throw + 56
  AVFoundation         <redacted> + 0
  AVFoundation         <redacted> + 344
  AVFoundation         <redacted> + 304
  MediaPlayer          <redacted> + 68
  MediaPlayer          <redacted> + 2836
  MediaPlayer          <redacted> + 1980
  MediaPlayer          <redacted> + 1004
  MediaPlayer          <redacted> + 164
 MediaPlayer          <redacted> + 404
 MediaPlayer          <redacted> + 52
 libdispatch.dylib    <redacted> + 24
 libdispatch.dylib    <redacted> + 16
 libdispatch.dylib    <redacted> + 1012
 CoreFoundation       <redacted> + 12
 CoreFoundation       <redacted> + 2272
 CoreFoundation       CFRunLoopRunSpecific + 552
 GraphicsServices     GSEventRunModal + 100
 UIKit                UIApplicationMain + 236
 Notification Route   main + 124
 libdyld.dylib        <redacted> + 4
)
SignalHandler 6
 

BillMeyer

Well-Known Member
Licensed User
Try this - might help - I'm open to other suggestions:

B4X:
Sub Process_Globals
     Dim ping As MediaPlayer
End Sub

Sub PlaySound
    ping.Initialize(File.DirAssets,"ring.wav","")
    ping.Volume = 1
    ping.Play
End Sub
 
Top