提交 43d96a0f 编写于 作者: Z Zhang Rui

ios: use NotificationCenter to follow MPMediaPlayerController behavior

上级 63cb65c6
......@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
E612EAE517F7E0F800BEE660 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E612EAE417F7E0F800BEE660 /* MediaPlayer.framework */; };
E6166C9C17EDA4A20006B956 /* IJKMediaDemo-Prefix.pch in Resources */ = {isa = PBXBuildFile; fileRef = E6166C9B17EDA4A20006B956 /* IJKMediaDemo-Prefix.pch */; };
E63FC28A17F04C48003551EB /* CoreAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E63FC28917F04C48003551EB /* CoreAudio.framework */; };
E63FC29117F04C83003551EB /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E63FC28F17F04C83003551EB /* AudioToolbox.framework */; };
......@@ -39,6 +40,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
E612EAE417F7E0F800BEE660 /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
E6166C7017EDA43C0006B956 /* libIJKMediaPlayer.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libIJKMediaPlayer.a; path = "../../../../../Library/Developer/Xcode/DerivedData/IJKMedia-earrwrzhlmypuheegnwtfewujljd/Build/Products/Debug-iphoneos/libIJKMediaPlayer.a"; sourceTree = "<group>"; };
E6166C9B17EDA4A20006B956 /* IJKMediaDemo-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "IJKMediaDemo-Prefix.pch"; sourceTree = "<group>"; };
E63FC27817F032FD003551EB /* IJKCommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IJKCommon.h; sourceTree = "<group>"; };
......@@ -81,6 +83,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E612EAE517F7E0F800BEE660 /* MediaPlayer.framework in Frameworks */,
E63FC2B817F17362003551EB /* QuartzCore.framework in Frameworks */,
E63FC2B417F172EA003551EB /* OpenGLES.framework in Frameworks */,
E63FC29117F04C83003551EB /* AudioToolbox.framework in Frameworks */,
......@@ -141,6 +144,7 @@
E6903EFE17EAF70200CFD954 /* Frameworks */ = {
isa = PBXGroup;
children = (
E612EAE417F7E0F800BEE660 /* MediaPlayer.framework */,
E63FC2B717F17362003551EB /* QuartzCore.framework */,
E63FC2B317F172E9003551EB /* OpenGLES.framework */,
E63FC28F17F04C83003551EB /* AudioToolbox.framework */,
......
......@@ -10,7 +10,7 @@
@class IJKMediaControl;
@interface IJKVideoViewController : UIViewController <IJKMediaPlaybackDelegate>
@interface IJKVideoViewController : UIViewController
@property(atomic, retain) id<IJKMediaPlayback> player;
......
......@@ -44,7 +44,6 @@
// NSURL *theMovieURL = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"];
self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:theMovieURL];
self.player.playbackDelegate = self;
self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.player.view.frame = self.view.bounds;
......
......@@ -43,6 +43,7 @@
E6F727BD17F40C360043623F /* IJKMPMoviePlayerController.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E66F8DBF17EEC65200354D80 /* IJKMPMoviePlayerController.h */; };
E6F727BE17F40C390043623F /* IJKFFMoviePlayerController.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E66F8DE517EFD9C300354D80 /* IJKFFMoviePlayerController.h */; };
E6F727BF17F40C4A0043623F /* IJKMediaModule.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E6F727B617F2B1B10043623F /* IJKMediaModule.h */; };
E6F727C217F7C9BA0043623F /* IJKMediaPlayback.m in Sources */ = {isa = PBXBuildFile; fileRef = E6F727C117F7C9B90043623F /* IJKMediaPlayback.m */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
......@@ -214,6 +215,7 @@
E6F727B717F2B1B10043623F /* IJKMediaModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJKMediaModule.m; path = IJKMediaPlayer/IJKMediaModule.m; sourceTree = "<group>"; };
E6F727B917F2D9D30043623F /* IJKFFMoviePlayerDef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IJKFFMoviePlayerDef.h; sourceTree = "<group>"; };
E6F727BA17F2D9D30043623F /* IJKFFMoviePlayerDef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IJKFFMoviePlayerDef.m; sourceTree = "<group>"; };
E6F727C117F7C9B90043623F /* IJKMediaPlayback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJKMediaPlayback.m; path = IJKMediaPlayer/IJKMediaPlayback.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -589,6 +591,7 @@
E6F727B617F2B1B10043623F /* IJKMediaModule.h */,
E6F727B717F2B1B10043623F /* IJKMediaModule.m */,
E6903EC117EAF6C500CFD954 /* IJKMediaPlayback.h */,
E6F727C117F7C9B90043623F /* IJKMediaPlayback.m */,
E66F8DC217EECB1E00354D80 /* IJKMediaPlayer.h */,
);
name = IJKMediaPlayer;
......@@ -671,6 +674,7 @@
E63FC2BE17F18469003551EB /* ijksdl_thread_ios.m in Sources */,
E6F727B817F2B1B10043623F /* IJKMediaModule.m in Sources */,
E6F727BB17F2D9D40043623F /* IJKFFMoviePlayerDef.m in Sources */,
E6F727C217F7C9BA0043623F /* IJKMediaPlayback.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
......@@ -24,11 +24,15 @@
#import "IJKFFMoviePlayerDef.h"
#import "IJKMediaPlayback.h"
@implementation IJKFFMoviePlayerController {
NSURL *_url;
IjkMediaPlayer *_mediaPlayer;
IJKFFMoviePlayerMessagePool *_msgPool;
NSInteger _videoWidth;
NSInteger _videoHeight;
NSInteger _sampleAspectRatioNumerator;
NSInteger _sampleAspectRatioDenominator;
}
@synthesize view = _view;
......@@ -36,7 +40,9 @@
@synthesize duration;
@synthesize playableDuration;
@synthesize playbackDelegate;
@synthesize isPreparedToPlay;
@synthesize playbackState = _playbackState;
@synthesize loadState = _loadState;
- (id)initWithContentURL:(NSURL *)aUrl
{
......@@ -131,49 +137,82 @@
if (!msg)
return;
id<IJKMediaPlaybackDelegate> delegate = self.playbackDelegate;
if (!delegate)
return;
AVMessage *avmsg = &msg->_msg;
switch (avmsg->what) {
case FFP_MSG_FLUSH:
break;
case FFP_MSG_ERROR:
case FFP_MSG_ERROR: {
NSLog(@"FFP_MSG_ERROR: %d", avmsg->arg1);
if ([delegate respondsToSelector:@selector(playerDidFail:)])
[delegate playerDidFail:avmsg->arg1];
_playbackState = MPMoviePlaybackStateStopped;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:self];
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification
object:self
userInfo:@{
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey: @(MPMovieFinishReasonPlaybackError),
@"error": @(avmsg->arg1)}];
break;
}
case FFP_MSG_PREPARED:
NSLog(@"FFP_MSG_PREPARED:");
if ([delegate respondsToSelector:@selector(playerDidPrepare:)])
[delegate playerDidPrepare];
isPreparedToPlay = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMediaPlaybackIsPreparedToPlayDidChangeNotification object:self];
break;
case FFP_MSG_COMPLETED:
NSLog(@"FFP_MSG_COMPLETED:");
if ([delegate respondsToSelector:@selector(playerDidComplete:)])
[delegate playerDidComplete];
case FFP_MSG_COMPLETED: {
_playbackState = MPMoviePlaybackStateStopped;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:self];
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification
object:self
userInfo:@{MPMoviePlayerPlaybackDidFinishReasonUserInfoKey: @(MPMovieFinishReasonPlaybackError)}];
break;
}
case FFP_MSG_VIDEO_SIZE_CHANGED:
NSLog(@"FFP_MSG_VIDEO_SIZE_CHANGED: %d, %d", avmsg->arg1, avmsg->arg2);
if ([delegate respondsToSelector:@selector(playerDidChangeVideoSize:)])
[delegate playerDidChangeVideoSize:IJKSizeMake(avmsg->arg1, avmsg->arg2)];
if (avmsg->arg1 > 0)
_videoWidth = avmsg->arg1;
if (avmsg->arg2 > 0)
_videoHeight = avmsg->arg2;
// TODO: notify size changed
break;
case FFP_MSG_SAR_CHANGED:
NSLog(@"FFP_MSG_SAR_CHANGED: %d, %d", avmsg->arg1, avmsg->arg2);
if ([delegate respondsToSelector:@selector(playerDidChangeSampleAspectRatio:)])
[delegate playerDidChangeSampleAspectRatio:IJKSampleAspectRatioMake(avmsg->arg1, avmsg->arg2)];
if (avmsg->arg1 > 0)
_sampleAspectRatioNumerator = avmsg->arg1;
if (avmsg->arg2 > 0)
_sampleAspectRatioDenominator = avmsg->arg2;
break;
case FFP_MSG_BUFFERING_START:
case FFP_MSG_BUFFERING_START: {
NSLog(@"FFP_MSG_BUFFERING_START:");
if ([delegate respondsToSelector:@selector(playerDidStartBuffering:)])
[delegate playerDidStartBuffering];
_loadState = MPMovieLoadStateStalled;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerLoadStateDidChangeNotification
object:self];
break;
case FFP_MSG_BUFFERING_END:
}
case FFP_MSG_BUFFERING_END: {
NSLog(@"FFP_MSG_BUFFERING_END:");
if ([delegate respondsToSelector:@selector(playerDidStopBuffering:)])
[delegate playerDidStopBuffering];
_loadState = MPMovieLoadStatePlaythroughOK;
[[NSNotificationCenter defaultCenter]
postNotificationName:IJKMoviePlayerLoadStateDidChangeNotification
object:self];
break;
}
case FFP_MSG_BUFFERING_UPDATE:
// NSLog(@"FFP_MSG_BUFFERING_UPDATE: %d, %d", avmsg->arg1, avmsg->arg2);
break;
......@@ -184,12 +223,10 @@
// NSLog(@"FFP_MSG_BUFFERING_TIME_UPDATE: %d", avmsg->arg1);
break;
case FFP_MSG_SEEK_COMPLETE:
NSLog(@"FFP_MSG_SEEK_COMPLETE:");
if ([delegate respondsToSelector:@selector(playerDidSeek:)])
[delegate playerDidSeek];
// NSLog(@"FFP_MSG_SEEK_COMPLETE:");
break;
default:
NSLog(@"unknown FFP_MSG_xxx(%d)", avmsg->what);
// NSLog(@"unknown FFP_MSG_xxx(%d)", avmsg->what);
break;
}
......@@ -210,7 +247,7 @@ int media_player_msg_loop(void* arg)
IjkMediaPlayer *mp = (IjkMediaPlayer*)arg;
__weak IJKFFMoviePlayerController *ffpController = ffplayerRetain(ijkmp_set_weak_thiz(mp, NULL));
while (ffpController && true) {
while (ffpController) {
@autoreleasepool {
IJKFFMoviePlayerMessage *msg = [ffpController obtainMessage];
if (!msg)
......
......@@ -29,7 +29,9 @@
@dynamic duration;
@dynamic playableDuration;
@synthesize playbackDelegate;
@dynamic isPreparedToPlay;
@dynamic playbackState;
@dynamic loadState;
- (id)initWithContentURL:(NSURL *)aUrl
{
......@@ -43,6 +45,11 @@
return self;
}
- (void)dealloc
{
[self removeMovieNotificationObservers];
}
- (BOOL)isPlaying
{
switch (self.playbackState) {
......@@ -53,4 +60,59 @@
}
}
#pragma mark Movie Notification Handlers
/* Register observers for the various movie object notifications. */
-(void)installMovieNotificationObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:)
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerLoadStateDidChangeNotification:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerPlaybackDidFinishNotification:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dispatchMPMoviePlayerPlaybackStateDidChangeNotification:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self];
}
- (void)removeMovieNotificationObservers
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:self];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:self];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self];
}
- (void)dispatchMPMediaPlaybackIsPreparedToPlayDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMediaPlaybackIsPreparedToPlayDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerLoadStateDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerLoadStateDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerPlaybackDidFinishNotification object:notification.object userInfo:notification.userInfo];
}
- (void)dispatchMPMoviePlayerPlaybackStateDidChangeNotification:(NSNotification*)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:IJKMoviePlayerPlaybackStateDidChangeNotification object:notification.object userInfo:notification.userInfo];
}
@end
......@@ -61,22 +61,6 @@ IJKSampleAspectRatioMake(NSInteger numerator, NSInteger denominator)
}
#pragma mark IJKMediaPlaybackDelegate
@protocol IJKMediaPlaybackDelegate <NSObject>
@optional
- (void)playerDidFail:(NSInteger)error;
- (void)playerDidPrepare;
- (void)playerDidComplete;
- (void)playerDidChangeVideoSize:(IJKSize)size;
- (void)playerDidChangeSampleAspectRatio:(IJKSampleAspectRatio)sampleAspectRatio;
- (void)playerDidStartBuffering;
- (void)playerDidStopBuffering;
- (void)playerDidSeek;
@end
#pragma mark IJKMediaPlayback
......@@ -93,6 +77,22 @@ IJKSampleAspectRatioMake(NSInteger numerator, NSInteger denominator)
@property(nonatomic, readonly) NSTimeInterval duration;
@property(nonatomic, readonly) NSTimeInterval playableDuration;
@property(nonatomic, weak) id<IJKMediaPlaybackDelegate> playbackDelegate;
@property(nonatomic, readonly) BOOL isPreparedToPlay;
@property(nonatomic, readonly) MPMoviePlaybackState playbackState;
@property(nonatomic, readonly) MPMovieLoadState loadState;
#pragma mark Notifications
#ifdef __cplusplus
#define IJK_EXTERN extern "C" __attribute__((visibility ("default")))
#else
#define IJK_EXTERN extern __attribute__((visibility ("default")))
#endif
IJK_EXTERN NSString *const IJKMediaPlaybackIsPreparedToPlayDidChangeNotification;
IJK_EXTERN NSString *const IJKMoviePlayerLoadStateDidChangeNotification;
IJK_EXTERN NSString *const IJKMoviePlayerPlaybackDidFinishNotification;
IJK_EXTERN NSString *const IJKMoviePlayerPlaybackStateDidChangeNotification;
@end
/*
* IJKMediaPlayback.m
*
* Copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "IJKMediaPlayback.h"
NSString *const IJKMediaPlaybackIsPreparedToPlayDidChangeNotification = @"IJKMediaPlaybackIsPreparedToPlayDidChangeNotification";
NSString *const IJKMoviePlayerLoadStateDidChangeNotification = @"IJKMoviePlayerLoadStateDidChangeNotification";
NSString *const IJKMoviePlayerPlaybackDidFinishNotification = @"IJKMoviePlayerPlaybackDidFinishNotification";
NSString *const IJKMoviePlayerPlaybackStateDidChangeNotification = @"IJKMoviePlayerPlaybackStateDidChangeNotification";
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册