IOS-Use the music's background playback feature to play a movie using the MediaPlayer Framework (Swift)

Source: Internet
Author: User

1. Use the music background playback function

(1) Import audio playback frame

Import Avfoundation

(2) Creating an audio playback object

Initializes the audio player object and zodiac the audio playback object as the View controller class.

var audioplayer:avaudioplayer = Avaudioplayer ()

Override Func Viewdidload () {

Super.viewdidload ()

Additional setup after loading the view, typically from a nib.

Gets the audio session object, which is in singleton mode, which means that it is instantiated by itself without a developer. This class plays an important role in various audio environments

Let session = Avaudiosession.sharedinstance ()

First create an exception capture statement before the audio is played

do {

Start audio session management, which will block the playback of background music.

Try Session.setactive (True)

Set the audio action category to indicate that the app only supports audio playback.

Try Session.setcategory (Avaudiosessioncategoryplayback)

Set application support to accept remote control events

Uiapplication.sharedapplication (). Beginreceivingremotecontrolevents ()

Defines a string variable that describes the path to a sound file

Let path = Nsbundle.mainbundle (). Pathforresource ("Music", OfType: "MP3")

Converts a string path to a URL path

Let Soundurl = Nsurl (fileurlwithpath:path!)

Initialize the audio playback object, and load the developed audio file

Try Audioplayer = Avaudioplayer (Contentsofurl:soundurl)

Prepare for audio playback

Audioplayer.preparetoplay ()

To set the playback volume size of an audio playback object

Audioplayer.volume = 1.0

Number of plays

Audioplayer.numberofloops =-1//-1: Infinite loop playback

Audioplayer.play ()

}catch {

Print (Error)

}

}

2. Use the MediaPlayer frame to play the movie

(1) Import Media Player Framework, which allows access to audio and video, audio and video files of the player

Import MediaPlayer

(2) Create a video player

Create a new video player property to play the video

var mediaplayer:mpmovieplayercontroller = MPMoviePlayerController ()

Override Func Viewdidload () {

Super.viewdidload ()

Additional setup after loading the view, typically from a nib.

do {

Gets the path where the video file is located in the sandbox directory.

Let path = Nsbundle.mainbundle (). Pathforresource ("movie", OfType: "mp4")

Converts a string path to a nsurl path

Let Movieurl = Nsurl (fileurlwithpath:path!)

Initialization

MediaPlayer = MPMoviePlayerController (Contenturl:movieurl)

Set the video playback mode to play in full screen.

Mediaplayer.controlstyle = Mpmoviecontrolstyle.fullscreen

MediaPlayer.view.frame = Self.view.bounds

Set the start time for video playback

Mediaplayer.initialplaybacktime =-1

Add a video playback controller to the root view of the current View controller

Self.view.addSubview (Mediaplayer.view)

Add a notification to monitor whether the video playback is over.

Nsnotificationcenter.defaultcenter (). Addobserver (Self, selector: #selector (Viewcontroller.moviefinished (_:)), Name:mpmovieplayerplaybackdidfinishnotification, Object:mediaplayer)

Mediaplayer.play ()

} catch {

Print (Error)

}

}

Create a method to react video playback end time

Func moviefinished (notifi:nsnotification) {

Print ("Movie ends")

Get video Playback Controller

Let player = Notifi.object

Unblock notifications, freeing resources

Nsnotificationcenter.defaultcenter (). Removeobserver (Self, Name: NSNotification.Name.MpMoviePlayerPlaybackDidFinish, Object:player)

}

IOS-Use the music's background playback feature to play a movie using the MediaPlayer Framework (Swift)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.