Play Song at specific index of MPMediaItemCollection in Swift - ios

I try to make my own Musicplayer with Swift. I need to jump to a specific song/index of my MPMediaItemCollection and start to play it but i can only find methods like skipToNextItem() and skipToPreviousItem(). Is there any other way to do that than with a loop?
let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()

According to the documentation, we use the nowPlayingItem property.
To specify that playback should begin at a particular media item in the playback queue, set this property to that item while the music player is stopped or paused.
So, it sounds like you should stop or pause the player, set the nowPlayingItem, and then call play again.
player.nowPlayingItem = mediaCollection.items[selectedIndex]
player.play()

Related

In which cases avplayer should play? Like play() function, set player rate

I set player to pause and i am setting player rate after the pause. After setting player rate avplayer starting the playing without play() method, so i want to know in which other cases that avplayer should play without play() method.
I find that when we set rate or seek to specific time than it is play but i want to know is there other cases when avplayer should play.
Please check https://github.com/teamSolutionAnalysts/sa-plug-avplayer which is very easy plug and play module, lets you play video easily and you tube embedded url supported.

Swift - Playing Audio file and Itunes Music simultaneously

I am currently building a game with swift and spritekit. I want to play a sound in a certain part of the code. Currently I am using AVAudioPlayer. I am currently using this method.
player = try AVAudioPlayer(contentsOfURL: url)
player.prepareToPlay()
player.play()
The sound plays correctly, but if I were to be listening to music through itunes while the sound is played my itunes music is stopped and the sound is played. I have played other people's games in the past where ingame sound effects play and do not effect music that was already playing from another app. Can somone explain how this is achieved.Thanks a bunch.
You'll need to set your audio session to use a category that allows mixing. By default it uses AVAudioSessionCategorySoloAmbient which is non mixable.
I don't know which category will fit your needs but AVAudioSessionCategoryAmbient allows mixing.
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

How to pause/resume avplayer preload

I'm using AVPlayer to play video from Internet, is there a way to pause/resume preloading video when pause/resume playing?
One way to do that would be to save your AVURLAsset to a variable (tempAsset = player.playerItem.asset), and then replace the playerItem in your player with nil. When you are ready to play it again, you can create a new playerItem from the asset that you saved previously.
I have same problem and i used buffer limit to stop preloading in my code.
This works for me.
You can also try setting buffer limit to stop preloading video when you click on Pause or Resume.
I hope this will help you.
The duration the player should buffer media from the network ahead of the playhead to guard against playback disruption.
player.currentItem?.preferredForwardBufferDuration
When you work with AVPlayerItem you can set time interval for the buffer. Example:
AVPlayerItem(). preferredForwardBufferDuration = TimeInterval(exactly: 100)!

How to use AVFoundation to show subtitle while showing a video?

I'm building an app where I show a video, I need to show a subtitle inside the video. I couldn't find a way to do it. Is this doable using AVFoundation ? Or should I hock-up something around it.
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
player.play()
The only way is create a separate subtitles layer (CATextLayer) and add as a sublayer to the player layer. You can set up a periodic time observer to trigger every second to update the subtitles.
You can find a sample project here SubRipForCocoa. Please note that, you have to write the same concept in Swift.

Creating new AVPlayer while in background does not work?

I'm playing music with an AVPlayer. Now at a certain time a NSTimer fires and I'dlike to fade over to another track. So I start fading out my AVPlayer and create a new AVPlayer instance to play the next song.
When on foreground this works as expected. But when my app is on background. The playing track fades out but the new AVPlayer instance does not start playing. Is it just not possible to create a new AVPlayer instance on background? or how can I make it play? Or is there another way to overlap two tracks?
I could do the playback with AVQeueuPalyer, but then I can't let tracks overlap. Any suggestions?
-- EDIT --
If it was not clear, I am able to play background audio as long as I want. Just creating a new AVPlayer instance in background does not work.
The correct way to do what I wanted seems to be AVMutableComposition. With that I don't need multiple AVPlayers and a few other benefits. For more details: I summarized it in a blogpost: http://www.postblog.me/2012/03/playing-multiple-overlapping-audio-tracks-in-background/
Try adding a key named "UIBackgroundModes" (array) to your app's Info.plist and add the value "audio" inside it. Then call
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
And then you should be able to play audio in the background (you should link to the AVFoundation framework).

Resources