123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- #include "CustomVideoPlayer.h"
- //#include "iPhone_View.h"
- #import <UIKit/UIKit.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- extern "C" __attribute__((visibility ("default"))) NSString *const kUnityViewDidRotate;
- @interface CustomVideoPlayerInterface : NSObject <CustomVideoPlayerDelegate> {
- @public
- CustomVideoPlayer *player;
- CGRect margin;
- bool bLoop;
-
- bool m_bFinish;
- bool m_bUnload;
- bool m_bLoading;
-
- bool m_bLoopPlay;
- NSURL* m_videoURL;
- }
- - (void)onPlayerReady;
- - (void)onPlayerDidFinishPlayingVideo;
- @end
- @implementation CustomVideoPlayerInterface
- - (void)loadVideo:(NSURL *)videoURL {
- m_bFinish = false;
- [player loadVideo:videoURL];
- }
- - (void)playVideo{
-
- if ([player readyToPlay])
- [self play];
- }
- - (void)onPlayerReady {
-
-
- m_bLoading = false;
-
- if(m_bUnload == true)
- {
- m_bUnload = false;
- [self unload];
- }
-
- if( m_bLoopPlay == true)
- {
- [self play];
- m_bLoopPlay =false;
- }
-
-
-
- }
- - (void)resizeView {
- /* //FIXME Orientationが変更された時にうまくリサイズされていない view frame更新
-
- CGFloat scale = UnityGetGLView().contentScaleFactor;
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
- CGRect bounds;
-
- if (orientation) {
- bounds.size.width = UnityGetGLView().bounds.size.width - (margin.origin.x + margin.size.width) / scale;
- bounds.size.height = UnityGetGLView().bounds.size.height - (margin.origin.y + margin.size.height) / scale;
- } else {
- bounds.size.width = UnityGetGLView().bounds.size.height - (margin.origin.x + margin.size.width) / scale;
- bounds.size.height = UnityGetGLView().bounds.size.width - (margin.origin.y + margin.size.height) / scale;
- }
-
- view.bounds = bounds;
- view.center = CGPointMake(view.bounds.size.width / 2 + margin.origin.x / scale, view.bounds.size.height / 2 + margin.origin.y / scale);*/
- }
- - (void)play {
- m_bFinish = false;
-
- [player playToTexture];
-
- }
- - (void)playloop {
- m_bFinish = false;
-
- [player playToTextureloop];
-
- }
- - (void)unload {
-
-
- if( m_bLoading == true)
- {
- m_bUnload = true;
- return;
- }
-
- [player unloadPlayer];
- }
- - (void)onPlayerDidFinishPlayingVideo {
-
-
- if(bLoop)
- {
- if( [m_videoURL isFileURL])
- {
- [player seekTo:0.0f];
- [self playloop];
- }
- else
- {
- [self unload];
- [self loadVideo:m_videoURL];
-
- m_bLoopPlay = true;
- }
-
-
-
- }
- else
- {
- //[self unload];
- m_bFinish = true;
- }
-
- }
- @end
- const int PLAYER_MAX = 8;
- static CustomVideoPlayerInterface * _Player[PLAYER_MAX];
- static bool _PlayerUsed[PLAYER_MAX] = {0,0,0,0,0,0,0,0};
- static CustomVideoPlayerInterface *_GetPlayer(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return nil;
-
- if (!_Player[iID]) {
- _Player[iID] = [[CustomVideoPlayerInterface alloc] init];
- _Player[iID]->player = [[CustomVideoPlayer alloc] init];
- _Player[iID]->player.delegate = _Player[iID].self;
- _PlayerUsed[iID] = true;
- }
-
- if (!_Player[iID]->player) {
- _Player[iID]->player = [[CustomVideoPlayer alloc] init];
- _Player[iID]->player.delegate = _Player[iID]->player.self;
- }
-
- return _Player[iID];
- }
- static NSURL *_GetUrl(const char *videoURL) {
- NSURL *url = nil;
- if (::strstr(videoURL, "://"))
- url = [NSURL URLWithString:[NSString stringWithUTF8String:videoURL]];
- else
- url = [NSURL fileURLWithPath:[[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Data/Raw/"] stringByAppendingPathComponent:[NSString stringWithUTF8String:videoURL]]];
- return url;
- }
- extern "C" int VideoPlayerPluginCreateInstance()
- {
- for(int i = 0; i < PLAYER_MAX; i++)
- {
- if(_PlayerUsed[i] == false)
- {
- CustomVideoPlayerInterface * player = _GetPlayer(i);
- player->bLoop = false;
- return i;
- }
- }
-
- return -1;
- }
- extern "C" void VideoPlayerPluginDestroyInstance(int iID)
- {
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- if(_Player[iID])
- {
- if(_Player[iID]->player)
- {
- [_Player[iID]->player unloadPlayer];
- [_Player[iID]->player cleanCache];
- //[_Player[iID]->player dealloc];
- _Player[iID]->player = NULL;
-
-
- }
-
-
- //[_Player[iID] dealloc];
- _Player[iID] = NULL;
-
- }
-
- _PlayerUsed[iID] = false;
-
-
- }
- extern "C" void VideoPlayerPluginLoadVideo(int iID,const char *videoURL) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
-
-
- if (_GetPlayer(iID)->player.isPlaying) {
- [_GetPlayer(iID)->player unloadPlayer];
- }
-
- _GetPlayer(iID)->m_bFinish = false;
- _GetPlayer(iID)->m_bLoading = true;
-
- _GetPlayer(iID)->m_videoURL = _GetUrl(videoURL);
-
- [_GetPlayer(iID) loadVideo:_GetPlayer(iID)->m_videoURL];
-
- }
- extern "C" void VideoPlayerPluginPlayVideo(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- _GetPlayer(iID)->m_bFinish = false;
-
- [_GetPlayer(iID) playVideo];
- }
- extern "C" void VideoPlayerPluginSetLoop(int iID,bool bLoop) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- _GetPlayer(iID)->bLoop = bLoop;
- }
- extern "C" void VideoPlayerPluginSetVolume(int iID,float fVolume) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- [_GetPlayer(iID)->player setAudioVolume:fVolume];
- }
- extern "C" void VideoPlayerPluginPauseVideo(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- [_GetPlayer(iID)->player pause];
- }
- extern "C" void VideoPlayerPluginResumeVideo(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- [_GetPlayer(iID)->player resume];
- }
- extern "C" void VideoPlayerPluginRewindVideo(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- }
- extern "C" bool VideoPlayerPluginCanOutputToTexture(const char *videoURL) {
-
- return [CustomVideoPlayer CanPlayToTexture:_GetUrl(videoURL)];
- }
- extern "C" bool VideoPlayerPluginPlayerReady(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return false;
-
- return [_GetPlayer(iID)->player readyToPlay];
- }
- extern "C" float VideoPlayerPluginDurationSeconds(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0.0f;
-
- return [_GetPlayer(iID)->player durationSeconds];
- }
- extern "C" int VideoPlayerPluginGetCurrentSeekPercent(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0;
-
- if(_Player[iID])
- {
- if(_Player[iID]->player)
- {
- if (_GetPlayer(iID)->player.isPlaying) {
- return (int)( [_GetPlayer(iID)->player availableDuration] / [_GetPlayer(iID)->player durationSeconds] * 100);
- }
- }
- }
-
- return 0;
- }
- extern "C" int VideoPlayerPluginGetAudioTrack(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0;
-
- return [_GetPlayer(iID)->player getAudioTrack];
- }
- extern "C" void VideoPlayerPluginSetAudioTrack(int iID, int trackID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- return [_GetPlayer(iID)->player setAudioTrack:trackID];
- }
- extern "C" int VideoPlayerPluginGetAudioTrackCount(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0;
-
-
-
- return [_GetPlayer(iID)->player getAudioTrack];
- }
- extern "C" void VideoPlayerPluginExtents(int iID,int *w, int *h) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- CGSize sz = [_GetPlayer(iID)->player videoSize];
- *w = (int) sz.width;
- *h = (int) sz.height;
-
-
- if(sz.width == 0)
- {
- [_GetPlayer(iID)->player curFrameTexture];
- }
-
-
- }
- extern "C" void VideoPlayerPluginSetTexture(int iID,int iTextureID)
- {
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- [_GetPlayer(iID)->player setTextureID:iTextureID];
- }
- extern "C" intptr_t VideoPlayerPluginCurFrameTexture(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0;
-
- return [_GetPlayer(iID)->player curFrameTexture];
- }
- extern "C" void VideoPlayerPluginSeekToVideo(int iID,float time) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- [_GetPlayer(iID)->player seekTo:time];
- }
- extern "C" float VideoPlayerPluginCurTimeSeconds(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return 0.0f;
-
- return [_GetPlayer(iID)->player curTimeSeconds];
- }
- extern "C" bool VideoPlayerPluginIsPlaying(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return false;
-
- if (!_GetPlayer(iID)->player)return false;
- return [_GetPlayer(iID)->player isPlaying];
- }
- extern "C" void VideoPlayerPluginStopVideo(int iID) {
-
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- if (_GetPlayer(iID)->player) {
- [_GetPlayer(iID) unload];
- }
- }
- extern "C" bool VideoPlayerPluginFinish(int iID) {
- if(iID < 0 || iID >= PLAYER_MAX)
- return false;
-
- if (_GetPlayer(iID)->player) {
- return _GetPlayer(iID)->m_bFinish;
- }
-
- return false;
-
- }
- extern "C" bool VideoPlayerPluginError(int iID) {
- if(iID < 0 || iID >= PLAYER_MAX)
- return false;
-
- if (_GetPlayer(iID)->player) {
- return [_GetPlayer(iID)->player getError ];
- //return _GetPlayer(iID)->player get;
-
- }
-
- return false;
- }
- extern "C" void VideoPlayerPluginSetSpeed(int iID,float fSpeed) {
- if(iID < 0 || iID >= PLAYER_MAX)
- return;
-
- if (_GetPlayer(iID)->player) {
- [_GetPlayer(iID)->player setSpeed:fSpeed ];
- //return _GetPlayer(iID)->player get;
-
- }
-
-
-
- }
|