How to play MP3 url in AVaudioPlayer without loading buffer - swift

I'm trying to play in my app mp3 file from url ... But the problem is the file size is big 120 MB ... So i said why not making it load same as youtube since i heard we can control the buffer and not loading the full audio file to the player ... instead we can play small sizes same as youtube for example ... i search and tried to get a way or a code but i couldn't find anything.
So is it possible to play mp3 file using AVaudioPlayer immediately without loading the full audio file?

Not with AVAudioPlayer. Use AVPlayer instead.

Related

UIVideoEditorController canEditVideoAtPath returns false on assets from library

on assets from library i.e: assets-library://asset/asset.mov?id=0399CB6D-D3D9-4F4C-82B9-AC93CCE2FB16&ext=mov
[UIVideoEditorController canEditVideoAtPath:videoPath] returns always NO
i see this error in the console:
<Warning>: Video assets-library://asset/asset.mov?id=0399CB6D-D3D9-4F4C-82B9-AC93CCE2FB16&ext=mov cannot be saved to the saved photos album: Error Domain=NSOSStatusErrorDomain Code=2 "This movie could not be played." UserInfo=0x6f7b90 {NSLocalizedDescription=This movie could not be played.}
help?
apparently there is no way to edit library assets with UIVideoEditController.
i ended up copying the file using AVAssetWriter to the app sandbox and then i was able to use UIVideoEditController.
I am able to write video tracks successfully.I am able to save that file to gallery as well.Video gets played with all the quality of original file .But not able to copy audio bufers and hence video gets played without sound.
But if I try to write audio track first and then video track then the writing fails for video buffers .That is BOOL appended = [assetWriterInput appendSampleBuffer:buffer]; fails by returning NO .
Buffers get appended while writing audio track.Then I cancel reader for audio track and start wrting reading video track.Appending the video buffers fails.For both writtes I am setting startSessionAtSourceTime:KCMTimeZero .
If I write only video buffers then video gets played without audio.
I want to make it possible to copy that .MOV file present inside gallery with all audio+video tracks inside it.
***Purpoose : Finally I want to edit the file copied using UIVideoEditorController.We can not directly edit file inside gallery.Hence I am first copying it inside sandbox and then editing it.

Can I preload mp4 before play with MPMoviePlayerController?

Can I preload mp4 before play with MPMoviePlayerController ?
I have a local mp4 video. I fount it stop at the first frame little time. I'm sure it don't matter to network. Can it preload to memry before play with MPMoviePlayerController?
I'm assuming by "local" you mean "on a machine in the same room as you". And if that is true, then the short answer is: "Yes you can."
The way I would do this is to save (or cache) a copy of the movie file to your Documents or Cache directory on your iPhone and then when you create your MPMoviePlayerController object, you can refer to the file saved on the device (via a NSURL pointing to the file saved).
Take a look at Apple's documentation for MPMoviePlayerController's initWithContentURL: method (which I have linked for you).
If by "local" you mean the file is already on the device and it is inexplicably freezing, then there is likely another problem with the mp4 you are trying to play.

record and play the same file

I am trying to record the audio using AVAudioRecorder. My problem is i want to play the file while it is being recorded. Is there any way i can do this? I have set the category to PlayAndRecord. I have created the file for recording.
I have tried to initialize both the recorder and the player for the URL and tried to play the file after the record function. But it seems that only one chunk of the file gets played.
I seriously doubt you can do this with AVAudioRecorder and AVAudioPlayer convenience classes.
The AVAudioPlayer assumes that the file it plays is static and complete. For example, it has no method to dynamically update the track length. It reads the track length once at startup and that is why only the initial chunk of the file gets played. Any modifications to the file made after the AVAudioPlayer instances opens the file are ignored.
You'll need to set up a custom audio queue if you want to get fancy.
As an aside, how do you plan on getting around feedback problem of caused by recording what you are playing? You have nothing but a shrieking squeal in very short order.

Play audio and video at a same time in iPhone application

Is it possible to play audio and video file at a same time? I want to play different audio file and video file at a same time and also want control for both, so is it possible?
Sorry, not that I know of. The movie view automatically stops all audio files and takes up the whole screen, so you are forced to listen to the audio for the video.
If the audio is part of the video file, yes.
If it's an MP3 file or some other external type how are you planning on playing it? The phone might allow this to happen depending on what you're up too.
You might be in luck soon though...
Another helpful link is here.

how can we play audio and video together in ipod/iphone?

HI all
i am playing a video(.mp4 format) without sound (i mean it doesn't have sound it is a mute video ) and in the background i am playing an audio file (.mp3 format) when i play my code through simulator it works fine as i want like when i tap on the video it is just mute but behind i am playing the audio so for user it seems that video has this sound but when i installed my code in device and play video than it doesn't work like so it play video but without sound than how can i play an audio and a video together in the above format ?
actually we are not just playing a single video or audio file it just comes from an array by choosing randomly and same for the audio file so we cann't do this i think so any other idea for it ??
Should we use another format for audio aur video for doing this thing ??
thanks for the help
Balraj verma
The problem is that the default Audio Session does not allow audio mixing.
In the Reference Library (Working with Movie Players) they say you should use a mixable category configuration for your audio session, for example the Ambient category. In the Application Delegate:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryAmbient error: &setCategoryError];
Apple's documentation states that some audio formats are not suited to be played back simultaneously because of hardware restrictions. See Playing Multiple Sounds Simultaneously for more details.
A solution for you would be to use PCM encoded audio which should allow simultaneous playback.
I would like to add that I managed to play two mp3 audio files at the same time on 3G and 3GS iPhones. Which shouldn't be possible according to documentation but worked for me.
You can rather use the an instance of AvAudioPlayer in a new thread. Use the following link to see how does this work
http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/
create an instance of MPMoviePlayer to start playback of videos.
Hope this will work.
You should combine the audio and video using some video editing software like iMovie or Windows Movie Maker. Using the software to "composite" audio and video together is not a good idea and adds additional overhead, synchronization issues, etc.
As far as I know, you can't play AVAudioPlayer and MPMoviePlayer at the same time.