Swift uses the Ijkplayer framework to play live video (Rtmp/http video stream playback)

Source: Internet
Author: User
Tags install homebrew uikit using git git clone


Bilibili (b station), presumably everyone is not unfamiliar. The Ijkplayer framework is the B station provides an open source streaming media solution, integrates ffmpeg, and also supports hard decoding (requires iOS8 above version). Using the Ijkplayer framework, we can easily realize the video live function (which is supported by several HTTP/RTMP/RTSP).



This article mainly describes how to use the Ijkplayer framework to play online live video (of course its local playback ability is also very strong). For several live streams are not very clear, you can refer to the article I wrote earlier: RTMP, RTSP, HTTP Video protocol detailed (with: Live streaming address, playback software)



First, environmental deployment



Before using Ijkplayer, we need to build a running environment first.
1, in the "terminal" run the following command, install homebrew, git, yasm and other environments.




Ruby-e "$ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)"
Brew Install Git
Brew Install Yasm



2, download the Ijkplayer project to the local



GitHub Address: Https://github.com/Bilibili/ijkplayer
or download to local using git command in terminal:



1
git clone https://github.com/Bilibili/ijkplayer.git Ijkplayer-ios



3, integrate FFmpeg into Ijkplayer
Run the following command to enter the project folder and integrate the FFmpeg.



CD Ijkplayer-ios
Git checkout-b latest k0.6.0

./init-ios.sh

CD iOS
./compile-ffmpeg.sh Clean
./compile-ffmpeg.sh All



Second, the operation of the sample
Ijkplayer Engineering Directory with a demo sample, we can first run the next look at the effect.
1, open the Ijkmediademo project under the iOS folder




2, after the operation of the interface as follows
You can choose to play local files or enter a video address to play. At the same time demo also brought some video sources, you can directly click to play.




3, enter the video address in the input URL page to play
However, the program defaults to the limit, only play Http/https video stream.
We can change this here and let go of the restrictions. Allow it to also support other protocol video playback (such as the RTMP protocol).
The relevant judgment in the IJKDEMOINPUTURLVIEWCONTROLLER.M can be commented out.




1



if ([Scheme isequaltostring:@ "http"] | | [Scheme isequaltostring:@ "https"]) {
[Ijkvideoviewcontroller presentfromviewcontroller:self withtitle:[nsstring stringwithformat:@ "URL:%@", url] URL:url completion:^{
[Self.navigationcontroller Popviewcontrolleranimated:no];
}];
//}



Third, integrate Ijkplayer into our project
Here's a sample demo of how to play the video stream through Ijkplayer in our project. The official demo is for OC, and here I use Swift to implement it.
1, make the framework
First we will compile the ijkmediaplayer into the framework, which is not only small in size, but also more convenient to use.
(1) Open ijkmediaplayer.xcodeproj (under Ios/ijkmediaplayer folder)



(2) Click Ijkmediaframework appear selection box, select Edit Scheme



(3) Change build configuration to release



(4) compiled on the simulator and the real machine (Generic IOS Device also can)








(5) We open the directory of the build framework



(6) You can see that there are two frameworks generated here. See the name to know that one is for the simulator to use, one is for the real machine. This selects the corresponding framework according to the individual's operational requirements.
Original: Swift-play live video 1 using the Ijkplayer framework (Rtmp/http video stream playback)



(7) Of course, if in the development phase, the real machine simulator need to debug so cut to cut it too troublesome. We merged two frameworks so that we could debug both the real machine and the emulator.
We enter the products catalog, enter the following Lipo command to merge the two outputs:
1
Lipo-create release-iphoneos/ijkmediaframework.framework/ijkmediaframework release-iphonesimulator/ Ijkmediaframework.framework/ijkmediaframework-output Ijkmediaframework



(8) The merged framework is then copied into the Release-iphoneos/ijkmediaframework.framework
1
CP Ijkmediaframework release-iphoneos/ijkmediaframework.framework/



(9) We can view the framework support through the Lipo-info ijkmediaframework command. You can see now that the real machine, the simulator is supported.
Original: Swift-play live video 1 using the Ijkplayer framework (Rtmp/http video stream playback)



2, project configuration
(1) We added the previously made framework (Release-iphoneos/ijkmediaframework.framework) to our project.



(2) Then add some dependent dynamic libraries.
Audiotoolbox.framework
Avfoundation.framework
Coregraphics.framework
Coremedia.framework
Corevideo.framework
Libbz2.tbd
Libz.tbd
Mediaplayer.framework
Mobilecoreservices.framework
Opengles.framework
Quartzcore.framework
Uikit.framework
Videotoolbox.framework
Original: Swift-play live video 1 using the Ijkplayer framework (Rtmp/http video stream playback)



Fourth. Test Examples
Once the project is configured, write a simple sample showing how to use Ijkplayer.
1, Sample Effect chart
After the program starts, it will automatically play a live broadcast with the RTMP protocol, and display full screen.



The playback view will be adaptive to the mobile phone horizontal, vertical two of states.



2, sample code



Import Uikit
Import Ijkmediaframework

Class Viewcontroller:uiviewcontroller {

var player:ijkffmovieplayercontroller!

Override Func Viewdidload () {
Super.viewdidload ()

Let options = Ijkffoptions.optionsbydefault ()

Video Source Address
Let URL = Nsurl (string: "Rtmp://live.hkstv.hk.lxdns.com/live/hks")
Let URL = Nsurl (string: "http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8")

Initialize player, play online video or Live (RTMP)
Let player = Ijkffmovieplayercontroller (Contenturl:url, withoptions:options)
Play Page view width and height adaptive
Let AutoResize = UIViewAutoresizing.FlexibleWidth.rawValue |
UIViewAutoresizing.FlexibleHeight.rawValue
Player.view.autoresizingMask = uiviewautoresizing (rawvalue:autoresize)

Player.view.frame = Self.view.bounds
Player.scalingmode =. Aspectfit//Zoom mode
Player.shouldautoplay = TRUE//Turn on AutoPlay

Self.view.autoresizesSubviews = True
Self.view.addSubview (Player.view)
Self.player = player
}

Override func Viewwillappear (Animated:bool) {
Start playing
Self.player.prepareToPlay ()
}

Override func Viewwilldisappear (Animated:bool) {
Turn off the viewer
Self.player.shutdown ()
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}




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.