iOS Question Audio player for local streams

Cadenzo

Active Member
Licensed User
Longtime User
In my app I generate .wav files from MIDI files and then play them in the MediaPlayer. But the conversion takes quite a long time and I would like to play the audio while it is being generated. Subsequent changing of the wave file does not seem to work in the MediaPlayer object and audio streams are not accepted. I found this solution for a similar problem while searching .

Could this be modified so that I can use it with a local audio stream? I need an object with the methods: "Play", "Stop" and Get / Set Position, which can play a file, which can still be appended at runtime.

I would be very grateful for any tips.
 

walterf25

Expert
Licensed User
Longtime User
according to apple documentation you can do all those things
https://developer.apple.com/documentation/avfoundation/avplayer?language=objc
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
Thank you very much!
So far I have never dealt with NativeObjects. Then I will now deal more closely with it and with the AVplayer. It would be ideal if I could pass a file path to "CreatePlayer" and then manipulate this file while playing and also be able to jump to different positions in the area that has already been created.
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
One more question: Where is a good start to learn about NativeObject and "OBJC"? I would like to understand this code and create my own
B4X:
Sub CreatePlayer(url As String) As NativeObject
   Dim u As NativeObject
   u = u.Initialize("NSURL").RunMethod("URLWithString:", Array(url))
   Dim player As NativeObject
   player = player.Initialize("AVPlayer").RunMethod("alloc", Null) _
     .RunMethod("initWithURL:", Array(u))
   Return player
End Sub

#if OBJC
@import CoreMedia;
@import AVFoundation;
- (double)GetTime:(AVPlayer*)player {
   return CMTimeGetSeconds([player.currentItem currentTime]);
}
- (float)GetDuration:(AVPlayer*)player {
    Float64 duration = CMTimeGetSeconds(player.currentItem.duration);
    return duration;
}
- (void)seekto:(AVPlayer*)player :(float)pos{
    Float64 seconds = pos;
    CMTime targetTime = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);
    [player seekToTime:targetTime
 toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
}
#End If
 
Upvote 0

mydigitalself

New Member
@Cadenzo did you ever get this working?

I'm trying to do something similar. What I've found is that if AVPlayer uses a local file, it will only load it at point in time. So if the file is still being written to, it won't "stream" it locally. I assume you ran into the same thing.

I've tried another 3rd party library (https://github.com/dimitris-c/AudioStreaming) and it behaves in exactly the same way.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…