【14 iOS-Cocos2d game development 】 audio/sound/video play (using Cocos2D-iPhone-Extensions embedded cocos2d video play !)

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/iphone-cocos2d/482.html


It has been around a month since the launch of the himi book "programming for Android games from scratch". The sales volume is good. Thank you for your support!

I still don't know how to read this book:

Baidu encyclopedia connection: http://baike.baidu.com/view/6513437.htm

Book address connection: http://blog.csdn.net/xiaominghimi/article/details/6802444


Today, himi will explain how to play videos, music, and sound effects in cocos2d;

First, we will introduce the playing of music and sound effects:

The playing of audio and sound effects is very simple. After all, the cocos2d engine encapsulates all this for us and directly goes to the Code:

// ---------------- Music related // load music + (void) loadbgmusic {// [simpleaudioengine sharedengine] preloadbackgroundmusic: @ "himi. CAF "];} // play background music + (void) playbgmusic :( nsstring *) filename {[simpleaudioengine sharedengine] playbackgroundmusic: Filename];} // pause background music + (void) pausebgmusic {[[simpleaudioengine sharedengine] pausebackgroundmusic];} // continue playing background music + (void) resumebgmusic {[[simpleaudioengine sharedengine] resumebackgroundmusic];} // stop background music + (void) stopbgmusic {[[simpleaudioengine sharedengine] stopbackgroundmusic];} // ---------------- sound effects // load audio + (void) loadpolictmusic in advance {// [[simpleaudioengine sharedengine] preloadeffect: @ "himi. CAF "];} // specifies the playing background sound + (void) playaudio tmusic {// [simpleaudioengine sharedengine] playeffect: @" himi. CAF "];}

Note that:

1. The background music and sound effects must be loaded in advance. After all, in order to prevent the loading time from being out of sync with the game;

2. When playing background music, you don't need to worry about switching the background music. Coco will pause the latest playing of the previous music;

3. Do not forget to import # import "simpleaudioengine. H"

 

Video Playback:

For cocos2d video playback, I think there are a lot of troubles with kids shoes... The reason is that the engine of cocos2d is not encapsulated, so many children's shoes are forced to use mpmovieplayercontroller In the ios sdk. However, after himi research, it is found that although the engine of cocos2d does not encapsulate video playback, but cocos2d has an extended library "Cocos2D-iPhone-Extensions", in addition to support cocos2d video play in the Cocos2D-iPhone-Extensions is also attached with other support, such as menus, scrolling layer, so this chapter will not introduce much, this article mainly introduces the video play extension class that Cocos2D-iPhone-Extensions brings to us: ccvideoplayer[Cocos2D-iPhone-Extensions class library this chapter finally releases download connection !]

After a general introduction, we will introduce how to use the extension package to play videos in the cocos2d project!

Step 1:Decompress the downloaded Cocos2D-iPhone-Extensions package, find the extensions/ccvideoplayer folder, and compare the IOS package under ccvideoplayer with ccvideoplayer. H and ccvideoplayer. M "is imported into our project; for example:

Step 2:Add the mediaplayer framework in IOS SDK(You can skip this step for the real machine debugging program)

Click the project, select-targets-build phases page, expand "link binary with libraries", and click "+" to add "mediaplayer. Framework", as shown in:

Note: Because my xcode version is 4.1, it may be a bit different to add the framework here. (For lower versions, you can directly right-click frameworks in the project );

Step 3:Modify the layer class of the video to be played. Here we use helloworldlayer as an example.;

In the "helloworldlayer. H" class, import # import "ccvideoplayer. H" and then enable helloworldlayer to use the protocol <ccvideoplayerdelegate>;

In the "helloworldlayer. m" class, add the method for initializing vedeoplayer In the init method:

[CCVideoPlayer setDelegate: self];

Then, rewrite the following functions in the "helloworldlayer. m" class:

- (void) moviePlaybackFinished{    CCLOG(@"moviePlaybackFinished");}- (void) movieStartsPlaying{    CCLOG(@"movieStartsPlaying");}#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED// Updates orientation of CCVideoPlayer. Called from SharedSources/RootViewController.m- (void) updateOrientationWithOrientation: (UIDeviceOrientation) newOrientation{    CCLOG(@"updateOrientationWithOrientation");[CCVideoPlayer updateOrientationWithOrientation:newOrientation ];}#endif

The added statement is complete. However, if you compile at this time, you will find an error, that is, the following sentence will be taken into consideration in the ccvideoplayer. M class of the extension Library:

#import "CCVideoPlayerImplMac.h"

Because the extended library also supports Mac video playback, we certainly don't need to do it for iOS, just block and delete it directly;

OK. The following are two simple methods for playing a video:

[Ccvideoplayer playmoviewithfile: @ "himi.mp4"]; // play a video [ccvideoplayer setnoskip: Yes]; // check whether the video can be skipped

Well, there are very few articles that play videos directly in cocos2d. Although most kids Shoes probably know this extended library but do not know how to use it, today, himi releases this blog post, hoping you can see it in time;

OK, continue to be busy. Thank you for your support!

Cocos2d-iphone-extensions-v0.1.3: http://www.himigame.com/iphone-cocos2d/482.html



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.