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

Source: Internet
Author: User
Tags android games


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 to play background music
+ (void) resumeBgMusic {
    [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
}
// Stop background music
+ (void) stopBgMusic {
    [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
}
// ---------------- Sound related
// Load sound effects in advance
+ (void) loadEffectMusic {
    // [[SimpleAudioEngine sharedEngine] preloadEffect: @ "himi.caf"];
}
// Play background sound effect
+ (void) playEffectMusic {
    // [[SimpleAudioEngine sharedEngine] playEffect: @ "himi.caf"];
}
have to be aware of is:

               1. Before playing background music and sound effects, they 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, even if you switch the background music, don't care, coco is d will pause the latest playback;

               3. Don't forget to import #import "SimpleAudioEngine.h"

 

     Video playback:

                About playing video in cocos2d, I think many children's shoes are troubled. . . The reason is that cocos2d's own engine is not packaged, so many children's shoes are eventually forced to choose MPMoviePlayerController in ios sdk; but after the research of Himi, it is found that although cocos2d's own engine does not package video playback, cocos2d has an expansion library "Cocos2D-iPhone -Extensions ", in addition to supporting cocos2d video playback, Cocos2D-iPhone-Extensions also comes with other support, such as menus, scrolling layers, etc., then this chapter will not introduce much, mainly introducing the video brought to us by Cocos2D-iPhone-Extensions Play extension class: CCVideoPlayer [Cocos2D-iPhone-Extensions class library finally releases the download link in this chapter! 】

                 After a general introduction, let's specifically introduce how to use the expansion pack to play video in the cocos2d project!

      Step 1: Unzip the downloaded Cocos2D-iPhone-Extensions package, then find the Extensions / CCVideoPlayer folder, and import the iOS package under CCVideoPlayer and "CCVideoPlayer.h" and "CCVideoPlayer.m" into our project; such as:

              

              

    

    Step 2: Add the MediaPlayer framework in the ios sdk (this step can be skipped for the real machine debugger)

          Click on the project, then select the -targets-Build Phases page, then expand "Link Binary With Libraries", and finally click "+" to add "MediaPlayer.framework"; for example:

               

      Note: Because my Xcode is version 4.1, there may be some differences in adding frames here; (looks like the lower version can be added directly in the Frameworks right under the project);

     

  Step 3: Modify the layer class that needs to play video, here is HelloWorldLayer as an example;

           In the "HelloWorldLayer.h" class, import #import "CCVideoPlayer.h", and then let HelloWorldLayer use the protocol <CCVideoPlayerDelegate>;

           In the "HelloWorldLayer.m" class, add a method to initialize vedeoPlayer in the init method:

[CCVideoPlayer setDelegate: self];
          Then rewrite the following function 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
      So far, the addition is complete, but if you compile at this time, you will find an error, that is, in the "CCVideoPlayer.m" class of the extension library, the following sentence must be noted:

#import "CCVideoPlayerImplMac.h"
    Because the expansion library also supports the video playback of mac, we do not need to do ios, just shield and delete it;

OK, here are two simple methods for playing videos:

    [CCVideoPlayer playMovieWithFile: @ "himi.mp4"]; // Play video
    [CCVideoPlayer setNoSkip: YES]; // Whether the video can be skipped
        Okay, because there are very few articles directly playing videos in cocos2d, although most children's shoes probably know this extended library but don't know how to use it, so today Himi released this blog post, I hope everyone can see it in time;

        OK, keep busy, thank you for your support!

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

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.