Access data from another class without creating new instance - iphone

I have a problem since two days researching more I could not solve.
I have an app in a class that runs a streaming audio. In another view, I have a table with podcasts that will be opened via url.
In order to take advantage of the code of the first class created a delegate, so when the User goes to play in any audio podcast I occupy the methods of the main class just changing the parameters (in this case the URL).
The delegate works correctly, the passing of parameters too. The only problem is that the delegate have to instantiate the main class.
ClassePrincipal *classePrincipal = [[ClassePrincipal alloc] init];
classePrincipal.delegate = self;
[classePrincipal method];
If the audio is already running in the main class, instantiated as a new object class, it will start playing the audio Podcast on top of what was already running.
and even if I have a major stop before he continues to play the podcast, eg:
- (void) playPodcast {
[classePrincipal destroyStreamer];
[classePrincipal startStream];
}
destroyStreamer the method is called correctly, but as the instance was created from scratch classePrincipal he did not see any audio being played.
kind of rolled the question, but is there any way to call a method of parameter passing ClassePrincipal without instantiating the class? For not allocating a new object in memory, I could see if the audio is playing and for him.
if there is any other way to solve also thank.

From what I can tell you may want to turn this class into a singleton. This way you will be instantiating it if its not already and if it has already been instantiated you can just place some checks in your code to stop the current audio before starting the new. A random tutorial I found is enter link description here

Related

just_audio: Why does my audio player work the first time but not the second time?

Tagging this with the appropriate tag so that the repo author can see it.
When my app uses an audio file saved on the user's phone, it works just fine the first time. It shows the audio source as:
justAudioPlayer.audioSource: Instance of 'ProgressiveAudioSource'
playerState: playing=true,processingState=ProcessingState.ready
When I hit the back button (Navigator.pop()) and then go back into the screen and try to play the audio source again, the audio source is now null:
justAudioPlayer.audioSource: null
playerState: playing=true,processingState=ProcessingState.idle
This is likely a simple programming error with your app's state management which, for example, results in your creating a new player instance instead of reusing the same instance.
(Copied from my comment above since you confirmed it was the correct answer)

Using SwiftUI, how to make multiple instances of AvAudioPlayer and access their properties?

I am trying to make a music app using SwiftUI which a user can set multiple times for different songs to play.
What is the best way to have multiple instances of AVAudioPlayer with each set to play at a specific time and have them accessible?
Please note that I will need to be able to access the isPlaying and the Stop() method for each instance of AVAudioPlayer.
You can implement an instance of the AudioPlayer in a separate class and use it as an ObservableObject.
Inside the class you can have functions like play and pause.
You can pass as an initial argument the file name and create multiple instances of this player class.
A more detailed tutorial is here https://youtu.be/HNAhPXOhZnY. Sorry, it would be too much to write here.

How to use NSOperation and NSOperationQueue

I made an app which plays the song on clicking on the image of artist.(see image attached). Each artist image is implemented on button and on clicking this button, a function is being called which first downloads and then plays the song. I passed this method(function) in a thread but problem is that every time when I click on the image of artist(button) new threads starts running and then multiple songs gets started playing concurrently. How can I use "NSOperation and NSOperationQueue" so that only one song will run at a time . Please help.
Thanks in advance
NSOperation and NSOperationQueue aren't going to directly solve your problem.
If I were pursuing a dead simple approach, I would have a global AudioPlayer object that has a method startPlaying: whose argument is the song to play (represented however needed; URL, NSData, whatever you need).
In that method, I'd stop playing whatever is currently playing and start the new track.
If I remember correctly, I don't think you even need a thread for this; the audio APIs are generally quite adept at taking care of playback in the background.
In any case, if you do need a thread, then I'd hide that thread in my AudioPlayer object and let it take care of telling the music to stop/start playing in said thread. A queue of some kind -- operation or GCD -- could be used for that, yes.

How do you send messages between two instances of a custom UITableViewCell?

I have a UITableView populated with a bunch of music players that I've made using a custom UITableViewCell. (as shown in the screenshot linked here: screenshot).
The problem with the way I have it coded is that since each cell is completely independent, there's no way for one tableviewcell to check if any other the other cells have audio playing. This means that when I run my app, I am able to play more than one audio file at the same time.
Ultimately, how would I go about putting some sort of check in place so that when the user hits the play button, the app first checks to see if any other cell is playing audio, and, if so, stops them before playing its own audio file?
Please let me know if you would like me to post my project.
Thank you!
For starters I can't think of any good reason the cells themselves should be responsible for playing the audio. They should be responsible for telling some other object (like whatever controller is responsible for the UITableView) to play the audio. That controller would obviously then know to stop whatever it was already playing before playing something else.
In general, it's not a great idea to be putting that much logic into a 'view.'
It would probably be easiest to use notifications (NSNotification and NSNotificationCenter).
Use a common notification name for starting and stopping.
MusicPlayerWillPlay
MusicPlayerWillStop
When you create each player, register for both notifications. Then when playerA is going to play, it posts a MusicPlayerWillPlay notification. playerB, playerC and playerD, will get this notification, and if they are playing, will stop.
One caveat is that each player is unaware of the others, so you have to register with a nil object, which means playerA will get it's own notification. So in your notification method, you'll probably want to do something like
{
//...
if (sender == self)
return;
//...
}

Find if an audio is currently playing

I need to find out if my program is currently playing any audio and in case it does, I want to stop the previous audio and start a new playback.
The property kAudioSessionProperty_OtherAudioIsPlaying always returns a 0 (probably only checks whether iPod music is playing)
There's another property kAudioQueueProperty_IsRunning but this always returns a 0 whether the audio is running or not. Can someone please tell me how I can find out if an audio is playing in my app or not.
Thanks.
Note: The class from which I invoke my streamer gets deallocated when I move back in the view hierarchy. So I do not have any way of accessing the AudioFileStreamID to know whether audio is playing. I need to use one of the properties provided by the SDK.
Found a workaround. Instead of creating the streamer object in a viewcontroller, I am using a reference in the appDelegate. This way I'll always have a live reference of the streamer class which I can access in any of the viewcontrollers to know whether audio is currently playing or not.