MpMoviePlayer 在停止事件后加载音频
Posted
技术标签:
【中文标题】MpMoviePlayer 在停止事件后加载音频【英文标题】:MpMoviePlayer load audio after stop event 【发布时间】:2011-01-21 09:05:39 【问题描述】:我的应用程序已完成 95%,我有一个非常严重的错误,我真的需要一些帮助。 我有带有流 url 列表的表格视图 > 我选择电影并加载 > 我有停止、暂停、音量和滚动视图的叠加,以在上一个流播放时切换流。 当我启动应用程序时,一切正常,所有流都正常。 但是当我点击 STOP 按钮时,当前流停止并且 mpmovieplayer 关闭,我再次从头开始处于表格视图中。停止按钮代码是这样的(我是 mpmovieplayer 的子类):
- (void) StopAndDismiss: (NSNotification *) notification
[mp stop]; // is MPMoviePlayerController object
[self dismissModalViewControllerAnimated:YES];
然后我从表中选择一些流,过程是相同的show mpmovieplayercontroller,加载电影的代码是:
-(void) ChannelFromMainChannelPicker: (NSNotification *)note
NSString *stringFromNote = (NSString *)[note object];
NSString *title = [[NSString alloc] initWithFormat:@"%@", stringFromNote];
cPlayer = [[CustomMoviePlayerController alloc] initWithUrlPath:title];
[self presentModalViewController:cPlayer animated:NO];
[cPlayer readyPlayer:stringFromNote];
[title release];
- (void)readyPlayer:(NSString *)p_url
if(mp != nil)
NSString *title = [[NSString alloc] initWithFormat:@"%@", p_url];
NSURL *n = [[NSURL alloc]initWithString:title];
[mp setContentURL:n];
mp.initialPlaybackTime = -1.0;
[title release];
[n release];
[mp play];
else
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieUrlPath];
//[mp setAllowsWirelessPlayback:YES];
if ([mp respondsToSelector:@selector(loadState)])
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleNone];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(SwitchToAnotherStreamUrl:)
name:@"notiSwitchToAnotherStreamUrl"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerPlaybackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(PauseStream:)
name:@"notiPauseStream"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(StopAndDismiss:)
name:@"notiStopAndDismiss"
object:nil];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
但是当我现在从覆盖流列表更改为其他流时,只有音频有效,但没有视频(只是黑屏)。但是我再次加载相同的流,它正常加载。但是,当我第一次按下 STOP 按钮后开始流式传输时,只会加载音频,我需要第二次调用它才能工作。 在我的自定义电影播放器中,我重载了这个方法,也许这里有一些问题:
- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification
if(mp.playbackState == MPMoviePlaybackStateStopped)
NSLog(@"Loading so display activity indicator");
[[NSNotificationCenter defaultCenter] postNotificationName:@"notiShowLoader" object:nil];
else if (mp.playbackState == MPMoviePlaybackStatePlaying)
NSLog(@"Done with loading, hide activity indicator");
[[NSNotificationCenter defaultCenter] postNotificationName:@"notiHideLoader" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notiBringCPToFront" object:nil];
else if (mp.playbackState == MPMoviePlaybackStatePaused)
NSLog(@"Movie is paused");
PS 有什么方法可以检查是否加载了音频而没有加载视频并以某种方式强制它重新加载播放器?
我添加了 NSlog 来检查播放状态。还有一些连线的东西,当我调用 STOP next 流时,我尝试多次播放它:
0 MPMoviePlaybackStateStopped,
1 MPMoviePlaybackStatePlaying,
2 MPMoviePlaybackStatePaused,
3 MPMoviePlaybackStateInterrupted,
4 MPMoviePlaybackStateSeekingForward,
5 MPMoviePlaybackStateSeekingBackward
first time from table view
2011-01-21 13:46:04.865[3561:207] CURRENT PLAYBACK STATE ID = 1
second time from overlay
2011-01-21 13:46:30.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:46:30.461[3561:207] CURRENT PLAYBACK STATE ID = 1
STOP button press
2011-01-21 13:47:14.797[3561:207] CURRENT PLAYBACK STATE ID = 0
from table view
2011-01-21 13:47:31.123[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:31.123[3561:207] CURRENT PLAYBACK STATE ID = 1
from overlay again (ONLY AUDIO)
2011-01-21 13:47:50.001[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.001[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.119[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:47:50.148[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.148[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.149[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.149[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.150[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:47:50.150[3561:207] CURRENT PLAYBACK STATE ID = 0
third time (now works and audio and video)
2011-01-21 13:48:21.752[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.752[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.852[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:48:21.852[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.855[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.855[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.856[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.856[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.858[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.858[3561:207] CURRENT PLAYBACK STATE ID = 1
STOP second time
2011-01-21 13:49:39.046[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:49:39.046[3561:207] CURRENT PLAYBACK STATE ID = 0
again load from table
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 1
from overlay
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.325[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:50:27.326[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.326[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.334[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.336[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.336[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:50:27.337[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.342[3561:207] CURRENT PLAYBACK STATE ID = 1
again from overlay (only audio)
2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 0
same icon again (works)
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.120[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.120[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:57:31.121[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.133[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.134[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.134[3561:207] CURRENT PLAYBACK STATE ID = 1
STOP again
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0
play from table
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 1
STOP
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.600[3561:207] CURRENT PLAYBACK STATE ID = 0
PLAY from table again
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 1
【问题讨论】:
【参考方案1】:好的,初学者错误,当我关闭我的自定义电影播放器时,我不知道我需要删除所有观察者。这解决了问题:
[mp stop];
[mp.view removeFromSuperview];
[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self];
【讨论】:
以上是关于MpMoviePlayer 在停止事件后加载音频的主要内容,如果未能解决你的问题,请参考以下文章
搜索文件时 MPMoviePlayer 播放音频但不播放视频
帮我选择合适的 iPhone 音频类 - MPMoviePlayer vs AVAudioPlayer vs MPMusicPlayer