2009-10-25 40 views
0

在我的應用程序,我用這個簡單的代碼播放視頻:iPhone SDK:如何停止使用代碼進行視頻播放?

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *moviePath = [bundle pathForResource:@"video" ofType:@"mp4"]; 
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
theMovie.movieControlMode = MPMovieControlModeHidden; 
[theMovie play]; 

我想知道如何停止與代碼中的視頻,我已經試過[theMovie stop];但是,這並不工作,一個錯誤「theMovie」未申報(首次在此函數中使用)這是可以理解的,因爲「theMovie」只在播放它的方法中聲明。有沒有人有任何想法如何阻止它不必顯示內置的電影播放器​​控件?任何幫助讚賞。

回答

1

如果您使用該代碼以某種方法創建該視頻並以某種其他方法調用stop,則會出現錯誤,因爲theMovie只存在於前一種方法中。您需要設置一個ivar@property

結賬this question

編輯:

一個示例代碼(未測試):

@interface Foo : UIViewController { 
    MPMoviePlayerController *_theMovie; 
} 

@property (nonatomic, retain) MPMoviePlayerController *theMovie; 
- (void) creationMethod; 
- (void) playMethod; 
- (void) stopMethod; 
@end 



@implementation Foo 

@synthesize theMovie = _theMovie; 

- (void) creationMethod { 
    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]; 
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; // retain not necessary 
    self.theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    self.theMovie.movieControlMode = MPMovieControlModeHidden; 
} 

- (void) playMethod { 
    [self.theMovie play]; 
} 

- (void) stopMethod { 
    [self.theMovie stop]; 
} 

- (void) dealloc { 
    [_theMovie release]; 
} 

@end 

你會調用creationMethod地方創建您的電影播放器​​。這只是一個玩家如何放置在房產中的例子,所以您可以通過多種方法使用它,但不一定是最佳做法。你可以/應該看看iPhone documentation on declared properties

我必須注意到,我沒有使用MPMoviePlayerController類,但是,如此精確的代碼可能會有所不同。

+0

是的,我明白爲什麼在另一種方法中使用stop方法在這種情況下不起作用,但是您能否進一步解釋我需要做什麼?我還沒有真正充分理解你的意思是與伊娃或@property。也許是一個例子? :D哦,我檢查了這個問題,我不能把它和我的情況聯繫起來。不管怎麼說,還是要謝謝你。 – Sam

+0

編輯示例 – mga

+0

謝謝,代碼完美工作,我也瞭解它。 :) – Sam