Forum > C++ > cocos2d-x play video question

cocos2d-x play video question

By tonycruiselin Posted 2011-07-13 20:12 Comments 24
tonycruiselin
  • Posts: 2

Can it play video? if it can,please tell me how,thanks.

zhangxm
  • Posts: 2081
  • Location: Xiamen, China

#1 RE: 2011-07-13 20:21

zhangxm
  • Posts: 2081
  • Location: Xiamen, China

#2 RE: 2011-07-13 20:25

proudchild
  • Posts: 19

#3 RE: 2012-07-24 14:02

One year passed and those references return internal error of redmine…

Yet, i really need some good references on playing video from a arbitrary scene. especially for IOS.

GMTDev
  • Posts: 205
  • Location: Vancouver, Canada

#4 RE: 2012-07-24 15:42

Check out http://www.arrowmania.com - Arrow Mania 2! For iOS, Android and Amazon Kindle Fire. Developed using Cocsos2D-X ;)

MildaGenius
  • Posts: 105

#5 RE: 2012-07-25 07:16

Here is example of video player on android.

Its not possible to upload to forum, so I added it to file share server.

[[http://ulozto.cz/xVjbpaA/videotest-zip]]

aikqzung
  • Posts: 16

#6 RE: 2012-07-26 04:56

Hi Milda,

Did u miss the link or something for the example?

MildaGenius
  • Posts: 105

#7 RE: 2012-07-26 07:55

Aikq:

No I didnt.

Promlem was, that I cant upload example zip. Its always end wih “413 Request Entity Too Large”.

I already contacted support, but no answer yet. So be please patient.

MG

aikqzung
  • Posts: 16

#8 RE: 2012-07-26 08:21

Milda Genius wrote:

Aikq:
>
No I didnt.
>
Promlem was, that I cant upload example zip. Its always end wih “413 Request Entity Too Large”.
>
I already contacted support, but no answer yet. So be please patient.
>
MG

Oh, I see,

may be, if possible, you can upload it to some file hosting websites, because I think there are many problems occurred with the forum right now.

looking forward to see the solution, and very appreciate for your help

cheers

MildaGenius
  • Posts: 105

#9 RE: 2012-07-26 13:51

I added link to video example to my original post.

aikqzung
  • Posts: 16

#10 RE: 2012-07-27 11:54

Thank you for your code Milda,

I will study on it

Victor_K
  • Posts: 61
  • Location: Moscow

#11 RE: 2013-01-15 10:13

GavT, thanks for your link! Playing video in AppController is not the most comfortable solution, calling something like

CQVideoPlayer::sharedVideoPlayer()->playVideo(file, extension)

looks much better to me, here’s the code (for iOS only):

CQVideoPlayer.h:

#ifndef __CQVIDEOPLAYER_H__
#define __CQVIDEOPLAYER_H__

#include 
#include 

USING_NS_CC;

class CQVideoPlayer
{
public:

    bool init(); 
    static CQVideoPlayer* sharedVideoPlayer();
    ~CQVideoPlayer();

    void playVideo(const char* strFileName, const char* strExtension);

    static void notificationHandler(CFNotificationCenterRef center, void *observer, 
                                                           CFStringRef name, const void *object, CFDictionaryRef userInfo);
    void videoFinishedCallback();
};

#endif  // __CQVIDEOPLAYER_H__

CQVideoPlayer.mm

#include "CQVideoPlayer.h"
#import 

static CQVideoPlayer *pSharedVideoPlayer = NULL;

// Keep these pointers to shut everything down in the end
MPMoviePlayerViewController *playerViewController;
MPMoviePlayerController *player;
UIWindow *window;

CQVideoPlayer* CQVideoPlayer::sharedVideoPlayer()
{
    if (! pSharedVideoPlayer)
    {
        pSharedVideoPlayer = new CQVideoPlayer();
        pSharedVideoPlayer->init();
    }

    return pSharedVideoPlayer;
}

bool CQVideoPlayer::init()
{
    // Some black magik to hear objC notifications from C++ class
    CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), this, ¬ificationHandler, 
                        (CFStringRef)MPMoviePlayerPlaybackDidFinishNotification,  NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
    return true;
}

void CQVideoPlayer::playVideo(const char* strFileName, const char* strExtension)
{
    // Init MPMoviePlayerViewController
    NSString *url = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:strFileName] ofType:[NSString stringWithUTF8String:strExtension]];
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];

    // Same thing we're doing in AppController
    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    [window addSubview: playerViewController.view];
    [window makeKeyAndVisible];

    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        [window addSubview: playerViewController.view];
    }
    else
    {
        [window setRootViewController:playerViewController];
    }

    // Actually start a video
    player = [playerViewController moviePlayer];
    [player prepareToPlay];
    player.scalingMode = MPMovieScalingModeAspectFit;
    player.shouldAutoplay = TRUE;
    player.controlStyle = MPMovieControlStyleNone;
    [player play];
}

// React to notification
void CQVideoPlayer::notificationHandler(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    (static_cast(observer))->videoFinishedCallback();
}

void CQVideoPlayer::videoFinishedCallback()
{
    // Hide video
    [player.view removeFromSuperview];
    [window setHidden:YES];
    [window release];
    [playerViewController release];

    // Post a notification to be able to properly react
    CCNotificationCenter::sharedNotificationCenter()->postNotification("VideoPlaybackFinished");
}

CQVideoPlayer::~CQVideoPlayer()
{
    // Remove observer in the end
    CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetLocalCenter(), this);
}

Dividing filename and extension may look a bit strange at the first glance, but video playback implementations for android, win32 etc will surely differ and supported file extensions will differ as well. I’ve tested for leaks and rotations, seems to work fine. Feel free to enhance and comment :)

jqyp324
  • Posts: 33

#12 RE: 2013-01-16 01:29

how to play video on win32?

javawebline
  • Posts: 13

#13 RE: 2013-03-14 09:41

Hi I am looking for video playing in Android using Cocos2dx.

Thanks,

V. D.

bialix
  • Posts: 35

#14 RE: 2013-04-04 08:41

Victor Komarov wrote:

GavT, thanks for your link! Playing video in AppController is not the most comfortable solution, calling something like
>
[…]
>
looks much better to me, here’s the code (for iOS only):

Victor, can you give some hints how to show subtitles over the video, using your code?

Victor_K
  • Posts: 61
  • Location: Moscow

#15 RE: 2013-04-04 08:59

You may use closed captions, but seems like user needs to turn them on at first, another option is to show UILabel above, but thats kinda ugly. Finally you can simply make hardsubs :)

bialix
  • Posts: 35

#16 RE: 2013-04-04 12:52

Victor Komarov wrote:

You may use closed captions, but seems like user needs to turn them on at first, another option is to show UILabel above, but thats kinda ugly. Finally you can simply make hardsubs :)

I’ve made subtitles with help of UILabel. Now I only need to find out how to stop video if player taps the screen (if player does not like my video :-/)

Victor_K
  • Posts: 61
  • Location: Moscow

#17 RE: 2013-04-04 14:03

Latest source codes are in the attachement, this version stops playback after screen is touched and sends corresponding notification

To use player include header

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "CQVideoPlayer.h"
#endif

Set listener method for notification somewhere in init method

CCNotificationCenter *nc = CCNotificationCenter::sharedNotificationCenter();
nc->addObserver(this, callfuncO_selector(CQLayer::onVideoPlaybackFinished), "VideoPlaybackFinished", NULL);

To play video just write

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
CQVideoPlayer::sharedVideoPlayer()->playVideo(videoName, videoExtension);
#endif
bialix
  • Posts: 35

#18 RE: 2013-04-04 15:54

Victor Komarov wrote:

Latest source codes are in the attachement, this version stops playback after screen is touched and sends corresponding notification

Many thanks for this!

Victor_K
  • Posts: 61
  • Location: Moscow

#19 RE: 2013-04-04 15:58

Please let me know if everything works as it should or if there are any problems

bialix
  • Posts: 35

#20 RE: 2013-04-05 02:48

Victor Komarov wrote:

Please let me know if everything works as it should or if there are any problems

It looks like it works OK. Thank you again.

yujunraining
  • Posts: 2

#21 RE: 2013-05-21 09:21

CQVideoPlayer::sharedVideoPlayer()->playVideo(videoName, videoExtension)
what “videoExtension” mean?
“videoName” is resource’s full path?

Victor Komarov wrote:

Please let me know if everything works as it should or if there are any problems

Victor_K
  • Posts: 61
  • Location: Moscow

#22 RE: 2013-05-22 06:29

videoName is just the name of the file, full path will be determined inside the method. Let’s assume you want to play a file called video.mpg, corresponding call will look like

CQVideoPlayer::sharedVideoPlayer()->playVideo("video", "mpg");
yujunraining
  • Posts: 2

#23 RE: 2013-05-22 07:18

oh thank you i have get it.
but now, i have a new problem: when i touch video screen, and the video will stop, but i want control by ios’ button, i have set button show, but i can’t control by it,
what can i do??

Victor Komarov wrote:

videoName is just the name of the file, full path will be determined inside the method. Let’s assume you want to play a file called video.mpg, corresponding call will look like
[…]

Victor_K
  • Posts: 61
  • Location: Moscow

#24 RE: 2013-05-22 07:25

Take a look at CQVideoPlayerHelper. What you want is to call m_VideoPlayer->stopVideo() when your button is touched. here m_VideoPlater is an instance of CQVideoPlayer.

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps