Use Vitamio to develop a universal player on iOS platform

Source: Internet
Author: User

Use Vitamio to develop a universal player on iOS platform
Quick Start

What does Vitamio do? See what the official saying goes:

"Vitamio SDK for iOS is a software development kit (SDK) officially launched by Yixia Ltd on the iOS platform. It provides iOS developers with simple and quick interfaces, it helps developers implement media playback applications on iOS platforms."

To put it bluntly, it helps you easily develop your own iOS player.

Objectives

Build the project from 0 and use VitamioSDK to develop a simple player: After opening the app, you can see a play button and click the button to start playing the video files that are pre-stored in the Documents directory, click the button to pause.

That's simple.

Preparations

Download Vitamio SDK:

Https://github.com/yixia/Vitamio-iOS

The current version is 4.2.0. The previous version is 1.1.3. Is this skip too big? It is estimated that it is used to maintain synchronization with Android's Vitamio SDK.

Decompress the package as follows:

Open Doc/html/index.html in the browser to view the official Vitamio documentation (or the style of Apple official documentation ). All kinds of interfaces seem to be complete, but it looks a little dazzling in English.

The Demo has a project that can be directly compiled to run, and multiple online videos can be played. If you are happy, you can follow this demo to transform your desired app.

Vitamio is the SDK we will use.

Procedure

The real machine used for debugging in this article is ipod touch5, and the system is iOS7.0.4.

Create a project

Create a Single View project


The name is myPlayer.

Simple UI

Drag and Drop three controls in storyboard: UILabel, used to display the video name; UIView is used to hold the playing screen, and UIButton is used to play or pause the video.

The default status of the button shows "play", and the selected status shows "pause": to establish a connection between the control and the variable:

UILabel is lblTitle

UIView is playerView

UIButton is btnPlayOrPause, And the click event is btnPlayOrPauseClick

Add an NSString variable to save the video address.

It currently looks like this:

@ Brief: UIViewController {NSString * videoPath;} @ property (weak, nonatomic) IBOutletUILabel * lblTitle; @ property (weak, nonatomic) IBOutletUIView * playerView; @ property (weak, nonatomic) IBOutletUIButton * btnPlayOrPause;-(IBAction) btnPlayOrPauseClick :( id) sender; @ end detection of video files

We need to traverse the files in the Documents folder, remember the address of the first video file found, and display its name on UILabel.

-(NSString *) findVideoInDocuments {NSString * documentsDirectory = [NSStringstringWithFormat: @ "% @/Documents", NSHomeDirectory ()]; NSFileManager * fileMg = [[NSFileManageralloc] init]; // traverse the files under Documents, find the video file, and return its full path NSArray * subPaths = [fileMgcontentsOfDirectoryAtPath: documentsDirectoryerror: nil]; if (subPaths) {for (NSString * subPathinsubPaths) {if ([selfisMediaFile: [subPathpathExtension]) {NSString * path = [documentsDirectorystringByAppendingPathComponent: subPath]; returnpath ;}} returnnil ;}
-(BOOL) isMediaFile :( NSString *) pathExtension {// available format /*". m1V ",". MP2 ",". MPE ",". MPG ",". WPAA ",". MPEG ",". MP4 ",". m4V ",". 3GP ",". 3GPP ",". 3G2 ",". 3GPP2 ",". MKV ",". WEBM ",". MTS ",". TS ",". TP ",". WMV ",". ASF ",". ASX ",". FLV ",". MOV ",". QT ",". RM ",". RMVB ",". VOB ",". DAT ",". AVI ",". OGV ",". OGG ",". VIV ",". VIVO ",". WTV ",". AVS ",". SWF ",". YUV "* // you can easily determine whether the video format is used. Here, we first try 6 NSString * ext = [pathExtensionuppercaseString]; if ([extisEqualToString: @" MP4 "]) {returnYES;} elseif ([extisEqualToString: @ "MOV"]) {returnYES;} elseif ([extisEqualToString: @ "RMVB"]) {returnYES;} elseif ([extisEqualToString: @ "MKV"]) {returnYES;} elseif ([extisEqualToString: @ "FLV"]) {returnYES;} elseif ([extisEqualToString: @ "TS"]) {returnYES ;} returnNO ;}
-(Void) viewDidLoad {[superviewDidLoad]; videoPath = [selffindVideoInDocuments]; if (videoPath) {_ lblTitle. text = [videoPathlastPathComponent];}

 

Compile now to see if there are any problems!

Well, it's okay, but it's just the interface we dragged out.

Now we can use iFunBox to place a video to the Documents folder.

What !? You haven't used iFunBox yet? Download one and try it. It can bring convenience to development.

I put a small file 0. ts.

Try compiling again. This time the question is coming out!

Add Vitamio and configure the project (or not ?)

Now we have a video file and obtained its specific path, and then we want to play it. Start using Vitamio!

Add the extracted Vitamio folder to the project:

After adding it, If you itch the compilation, you will find that the compilation is successful!

Is the VitamioSDK so powerful that it can be used without any configuration? And look down. Write Player Code

Introduce the Vitamio header file

# Import "Vitamio. h"

Declare a player variable

VMediaPlayer * matrix;
Apsaravideo player Protocol

After writing the above three lines of code, you can dispatch all the interfaces of the Vitamio SDK. This is the most streamlined configuration.

What are the advantages of VMediaPlayer and VMediaPlayerDelegate? We can only learn and explore.

Now our header file looks like this: # import "Vitamio. h "@ brief: UIViewController {NSString * videoPath; VMediaPlayer * matrix;} @ property (weak, nonatomic) IBOutletUILabel * lblTitle; @ property (weak, nonatomic) IBOutletUIView * playerView; @ property (weak, nonatomic) IBOutletUIButton * btnPlayOrPause;-(IBAction) btnPlayOrPauseClick :( id) sender; @ end
Initialize the player. The following code is the simplest way to initialize the player. -(Void) initPlayer {if (! |

We can see that we dragged the playerView on the UI to upload it, indicating that it is used to carry the video screen.

Prepare to play a video. Similarly, the following code is the simplest way to prepare to play a video:-(void) prepareVideo {if (videoPath) {// do not lock the screen during playback [UIApplicationsharedApplication]. idleTimerDisabled = YES; NSURL * videoURL = [NSURLfileURLWithPath: videoPath]; [matrix setdatasource: videoURL]; [matrix prepareasync];}

When our code is written to prepareAsync, we will throw the job to Vitamio. It will notify us in the Protocol method after preparation.

If you look at what you mean at this time, please refer to this part of the project:

 

It is warning you that some methods are not implemented, because we have referenced VMediaPlayerDelegate and haven't implemented it yet. Several methods are required:


 

@ Required indicates that the above three methods are required and we implement them one by one.

First, add a Boolean variable BOOLdidPrepared. It is very important that our code is involved in the player logic for the first time.

DidPrepared is called after Vitamio is ready to play the video. At this time, we can record the Boolean value-Prepare for playing and start playing the player:-(void) mediaPlayer :( VMediaPlayer *) playerdidPrepared :( id) arg {// display the word "Suspend" _ btnPlayOrPause. selected = YES; didPrepared = YES; [playerstart];}
PlaybackComplete will be called after the video is played. At this time, you can record the Playing History and exit the player. But here we want the simplest, so we need to reset the player so that we can play it again, you have to call the prepareVideo we wrote, and set didPrepared to NO. -(Void) mediaPlayer :( VMediaPlayer *) playerplaybackComplete :( id) arg {_ btnPlayOrPause. selected = NO; [playerreset]; didPrepared = NO ;}
Error will be called when Vitamio processes video errors, so that you have the opportunity to remind users or do other processing. Here we just have to create a log:-(void) mediaPlayer :( VMediaPlayer *) playererror :( id) arg {NSLog (@ "VMediaPlayerError: % @", arg );}
Remember to call initPlayer in ViewDidLoad for initialization. The change is as follows:
-(Void) viewDidLoad {[superviewDidLoad]; videoPath = [selffindVideoInDocuments]; if (videoPath) {_ lblTitle. text = [videoPathlastPathComponent]; [selfinitPlayer];}

If you want to play a video as soon as you open the app, you can put the first prepareVideo in ViewDidAppear.

Click the button to control the playback or pause:
-(IBAction) btnPlayOrPauseClick :( id) sender {if (videoPath) {BOOLisPlaying = [MMP ayerisplaying]; if (isPlaying) {[MMP ayerpause]; _ btnPlayOrPause. selected = NO;} else {if (didPrepared) [MMP ayerstart]; else [selfprepareVideo]; _ btnPlayOrPause. selected = YES ;}}}

Here, the Vitamio interface isPlaying is used to determine whether the video is being played most accurately, which is better than maintaining a playback status by ourselves.

But whether the video is ready is recorded by US (didPrepared ).

It is more complicated to click to play a video because you need to determine whether the video is ready. If yes, continue playing the video. If not, you have to call the preparation method.

Writing this code seems like it's finished! Compile it! Start the first player you wrote!

Configure Vitamio

You were shocked. The code is well written. Now, after compilation, why is the Code directly failed? 50 errors!

You don't have to worry about this debugging. Do you remember that you haven't configured it after the VitamioSDK is added in?

I still had to configure it. I was so happy just now...

First, you need to add some frameworks and dylib to Build Phases, as shown below:

The text version is as follows:

If you have obsessive-compulsive disorder, add the following database in reverse order to get the ascending order of the first letter.

Libbz2.dylib libiconv. dylib libstdc ++. dylib libz. dylib AVFoundation. framework AudioToolbox. framework CoreMedia. framework CoreVideo. framework MediaPlayer. framework OpenGLES. framework QuartzCore. framework

Add and then compile.

Ouye, compilation passed!

However, clicking play will crash. There is a log:

[UIDeviceplatformString]: unrecognizedselectorsenttoinstance is still not in a hurry for debugging. Go to Build Setting, find Other Linker Flags under Linking, and add "-ObjC ":

Compile and try again. oh my god! You can play it!

If you are still calm at this time, you are still thinking about the question: why is this so easy? In fact, the author of The Vitamio sdk has already expected you to have this question. You can find a FAQ. md file in the Doc folder mentioned above, which provides a dedicated answer to this question.

In addition, I will not tell you that Vitamio_SDK_for_iOS_User_Manual_cn.md under the Doc is a Chinese tutorial that is more concise than this blog post but has sufficient information. You deserve a read. Say a few more

Playing and pausing are a good start, but you certainly don't think it is enough. Stop? What about progress control? What about the duration? What about fast release? What about background playback? Switch to the next one? What about it? What about video thumbnails? How about playing online videos ?......

The functions of various players will be discussed in the blog in the future, so stay tuned.

Finally, remember to set the version of your project to 1.0.

Source code in this https://github.com/itechblue/myPlayer

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.