nstudio / nativescript-audio

:microphone: NativeScript plugin to record and play audio :musical_note:
Other
149 stars 104 forks source link

NS7 issue with ios audio #176

Closed bradrice closed 3 years ago

bradrice commented 3 years ago

I get errors with these two lines of code when building for ios

const audioSession = AVAudioSession.sharedInstance(); audioSession.setCategoryError(AVAudioSessionCategoryPlayback);

Also, I'm having some problems with the player instantiating more than once and playing tracks over top of each other. Is there a way to make sure there is only one instance and only one track can play at once? Perhaps it is related to the issue above?

bradmartin commented 3 years ago

Can you provide the errors you see?

bradrice commented 3 years ago

Here are the errors. However, the app does compile and it runs to the device.

`app/core/services/player.service.ts:496:42 - error TS2552: Cannot find name 'AVAudioSession'. Did you mean 'audioSession'?

496 const audioSession = AVAudioSession.sharedInstance();


  app/core/services/player.service.ts:496:27
    496                     const audioSession = AVAudioSession.sharedInstance();
'audioSession' is declared here.

app/core/services/player.service.ts:497:51 - error TS2304: Cannot find name 'AVAudioSessionCategoryPlayback'.

497 audioSession.setCategoryError(AVAudioSessionCategoryPlayback);`

bradmartin commented 3 years ago

so you're calling native APIs in your application directly, to avoid TS warnings which is what TS2552 is, you need to install the native typings so TS knows what you're doing since it's unaware of android/ios APIs.

That will resolve those warnings and let the compiler know that you know what you're doing 😄

bradrice commented 3 years ago

So per the reference document on migrating to NS7

With the new @nativescript/types you can actually simplify your references.d.ts to this if you want both iOS and Android type declarations: (https://nativescript.org/blog/nativescript-7-announcement/)

I have done that but still getting the errors.

/// <reference path="./node_modules/@nativescript/types/index.d.ts" />
You can also include specific platform types as needed if you desire; for example, say you wanted different Android sdk types alongside the iOS types:

/// <reference path="./node_modules/@nativescript/types-ios/index.d.ts" />
/// <reference path="./node_modules/@nativescript/types-android/lib/android-29.d.ts" 
bradmartin commented 3 years ago

Be sure to restart your IDE if you just added those, especially VSCode as it sometimes does not pick up changes like this.

The types are working in the plugin, so I'm certain it works and might just be your editor or a minor change missing.

bradrice commented 3 years ago

The blog doesn't mention installing anything. Do I have to install a dependency?

bradmartin commented 3 years ago

@nativescript/types should be a devDep, just as the older tns-platform-declarations were used for typings.

Let me know if that helps.

bradrice commented 3 years ago

OK, thanks. I do have it installed, I do have a references.d.ts file with thos lines in it, but for some reason it is not finding the declarations.

bradmartin commented 3 years ago

this might be a tsconfig issue to fix then, can you compare with the demo app on the repo here?

bradrice commented 3 years ago

Thanks for your help Brad. I think I figured it out. I had my references.d.ts file at the root of my project, but my tsconfig wasn't including it at that level. So I added it to my "files" parameter and now it is finding it.