How to toggle a sound on/off - ios

i created a sound in DetailViewController,
soundFileURL = [[NSBundle mainBundle] URLForResource:#"click" withExtension:#"wav"];
s1Player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
s1Player.delegate = self;
s1Player.volume = 2;
[s1Player play];
i want to control the above sound in my ViewController. i created a button in ViewController, and toggle it (sound on / off).
i tried,
DetailViewController.m
ViewController *viewController = [[ViewController alloc] init];
if(viewController.stopSound) {
[s1Player stop];
s1Player.volume = 0;
}
else {
[s1Player play];
s1Player.volume = 2;
}
ViewController.h
#property BOOL stopSound;
in ViewController.m
- (void) setSoundAction {
if(_stopSound){
_stopSound = NO;
}
else{
_stopSound = YES;
}
}
If the above code is not understandable or not cleared please suggest me how to toggle a button i.e, sound on and sound off on DetailViewController. Because Sound placed on DetailViewController.

What you want is just toggle button, then how about this?
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
toggleButton.frame = CGRectMake(10.0, 10.0, 40.0, 40.0);
[toggleButton setTitle:#"Toggle" forState:UIControlStateNormal];
[toggleButton addTarget:self action:#selector(toggleSound:) forControlEvents:UIControlEventTouchUpInside]; //execute the method when touched
[self.view addSubview:toggleButton];
}
- (void)toggleSound:(UIButton *)btn
{
static BOOL muted; //remember the current status of AVAudioPlayer object.
muted = !muted; //toggle!!
if(muted) {
s1player.volume = 0.0;
} else {
s2player.volume = 1.0; //max value for volume is 1.0
}
}

Related

Local video track add and remove after 12 times, video track broken in WebRTC iOS

I create local stream with RTCVideoTrack only, and play the video track in RTCEAGLVideoView. After press the btn for 24 times(means 12 times to play),the video track broken and only shows a stuck picture.
The code seems very simple, but I can not figure out what's the problem.
I upload the code to github, anyone could download the project to test the demo with iphone.
Code in github: https://github.com/tom555cat/WebRTCTest.git
The content of ViewController.m:
#interface ViewController ()
#property (nonatomic, strong) RTCPeerConnectionFactory *peerFactory;
#property (nonatomic, strong) RTCMediaStream *mediaStream;
#property (nonatomic, strong) RTCEAGLVideoView *videoView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// create local stream
[self createLocalStream];
// button to control the play and unplay video of local stream
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(300, 300, 100, 100);
[btn addTarget:self action:#selector(playOrUnPlay) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
}
// control the play and unplay video of local stream
- (void)playOrUnPlay {
static int flag = 0;
static int times = 0;
if (flag % 2 == 0) {
[self play];
++times;
NSLog(#"play times: %d", times);
} else {
[self unplay];
}
++flag;
}
- (void)createLocalStream {
self.peerFactory = [[RTCPeerConnectionFactory alloc] init];
self.mediaStream = [self.peerFactory mediaStreamWithStreamId:#"Fuck"];
// create video track
RTCAVFoundationVideoSource *source = [self.peerFactory avFoundationVideoSourceWithConstraints:nil];
RTCVideoTrack *videoTrack = [self.peerFactory videoTrackWithSource:source trackId:#"FuckVideo"];
// add video track to stream
if (videoTrack) {
[self.mediaStream addVideoTrack:videoTrack];
}
}
// play video of local stream
- (void)play {
self.videoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:self.videoView];
if (self.mediaStream.videoTracks.count > 0) {
RTCVideoTrack *videoTrack = self.mediaStream.videoTracks.firstObject;
[videoTrack addRenderer:self.videoView];
}
}
// stop play video of local stream
- (void)unplay {
if (self.mediaStream.videoTracks.count > 0) {
RTCVideoTrack *videoTrack = self.mediaStream.videoTracks.firstObject;
[videoTrack removeRenderer:self.videoView];
}
[self.videoView removeFromSuperview];
}

iCarousel AVPlayerItem Instances not displaying correctly

What I am trying to achieve
I would like to display a carousel of videos that play independently. No more than 10 at a time. as well as having their own play button per carousel object to play independently.
What I am getting
After 3 Items display in the carousel it displays the same three over and over again until it reaches the end of the 10 items.
I am assigning the AVAsset like below:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:index]] options:nil];
_videoURLS is an NSMutableArray of NSStrings it returns a 10 count to populate 10 items in the carousel like below.
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
return [_videoURLS count];
}
Now to populate the Video View I am doing the below:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:index]] options:nil];
//create new view if no view is available for recycling
if (view == nil)
{
//don't do anything specific to the index within
//this `if (view == nil) {...}` statement because the view will be
//recycled and used with other index values later
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 370.0f)];
view = mainView;
CGFloat mainViewWidth = mainView.bounds.size.width;
//Video Player View
_videoView = [[UIView alloc] initWithFrame:CGRectMake(0,0, mainViewWidth, 220)];
_videoView.backgroundColor = [UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1.0];
_videoView.center = CGPointMake(100, 170);//170
_videoView.tag = 20;
[view addSubview:_videoView];
//AV Asset Player
AVPlayerItem * playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
_player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _videoView.bounds;
[_videoView.layer addSublayer:playerLayer];
[_player seekToTime:kCMTimeZero];
//Play Button
_playButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 60.0f)];
[_playButton setBackgroundImage:[UIImage imageNamed:#"play-icon-grey.png"] forState:UIControlStateNormal];
[_playButton addTarget:self action:#selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
_playButton.center = CGPointMake(100, 160);
_playButton.tag = 1;
[view addSubview:_playButton];
}
else
{
//get a reference to the label in the recycled view
_playButton = (UIButton *) [view viewWithTag:1];
_videoView = (UIView *) [view viewWithTag:20];
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:_player];
return view;
}
I handle the play button like below:
-(IBAction)playVideo:(id)sender {
if (_playButton.isHidden) {
[_playButton setHidden:NO];
}else{
[_playButton setHidden:YES];
[_player play];
}
}
Though when I press the play button it plays the second video in the carousel.
What is the proper way to handle this and have all the videos play single handed?

Button click event is not reponding

I'm working with vuforia augmented Reality app. I'm displaying a button in EAGLView. But the button is not responding to click event. I have to get screenshot of what augmenting in top of overlay while ImageTarget is detect. After image recognize the overlay content and button is displaying on top of overlay. But while clicking the button it's not responding to user event
EAGLView.h:
Display 3D object:
#interface Object3D : NSObject {
unsigned int numVertices;
const float *vertices;
const float *normals;
const float *texCoords;
unsigned int numIndices;
const unsigned short *indices;
Texture *texture;
}
#interface ImageTargetsEAGLView : UIView <UIGLViewProtocol> {
#private
UIButton *photo;
}
#property (nonatomic) BOOL takePhotoFlag;
EAGLView.mm :
- (id)initWithFrame:(CGRect)frame appSession:(SampleApplicationSession *) app
{
self = [super initWithFrame:frame];
if (self) {
takePhotoFlag = NO;
[self DisplayPhotoButton];
return self;
}
- (void)takePhoto
{
NSLog(#"button pressed");
takePhotoFlag = YES;
}
- (void)DisplayPhotoButton
{
UIImage *closeButtonImage = [UIImage imageNamed:#"button.png"];
// UIImage *closeButtonTappedImage = [UIImage imageNamed:#"button_close_pressed.png"];
CGRect aRect = CGRectMake(20,20,
closeButtonImage.size.width,
closeButtonImage.size.height);
photo = [UIButton buttonWithType:UIButtonTypeCustom];
photo.frame = aRect;
[photo setImage:closeButtonImage forState:UIControlStateNormal];
[photo addTarget:self action:#selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:photo];
}
- (BOOL)presentFramebuffer
{
if (takePhotoFlag)
{
[self glToUIImage];
takePhotoFlag = NO;
}
}
Code for displaying overlay:
- (void)targetFound:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
self.targetOverlayView = [[[NSBundle mainBundle] loadNibNamed:#"targetOverlayView" owner:nil options:nil] objectAtIndex:0];
Book *aBook = [notification object];
[self.targetOverlayView setBook:aBook];
[booksDelegate setLastScannedBook:aBook];
[booksDelegate setOverlayLayer:self.targetOverlayView.layer];
[self.view addSubview:self.targetOverlayView];
}

Mpmovieplayercontroller video pause while clicking button for another functionality

i am using MPMovieplayerController for playing video, while im clicking some other button i.e i want to pause my video wherever its playing again. i click play means i want to play while its pausing.But for me now when i click button means my video was stopped.But i want to pause instead of stop.
My sample code here
- (IBAction) playvideo
{
NSURL *url = [NSURL URLWithString:#"http://xyz/video1.mp4"];
movieplayer = [[[MPMoviePlayerController alloc]initWithContentURL:url] retain];
movieplayer.view.frame=CGRectMake(25,54,658,460);
[self.view addSubview:movieplayer.view];
[movieplayer play];
}
-(void)buttononclick
{
[movieplayer pause];
[movieplayer.view removeFromSuperview];
for (int i = 0; i < 13; i++)
{
CGRect frame;
frame.origin.x = 150 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(140, self.scrollView.frame.size.height);
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *temp1 = [[UIImageView alloc] initWithFrame:CGRectMake(25, 7, 75, 75)];
[temp1 setImage:[UIImage imageNamed:#"sti15.png"]];
[self.scrollView addSubview:temp1];
UIImageView *temp2 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 7, 75, 75)];
[temp2 setImage:[UIImage imageNamed:#"sti16.png"]];
[self.scrollView addSubview:temp2];
UIImageView *temp3 = [[UIImageView alloc] initWithFrame:CGRectMake(195, 7, 75, 75)];
[temp3 setImage:[UIImage imageNamed:#"sti17.png"]];
[self.scrollView addSubview:temp3];
}
self.scrollView.contentSize = CGSizeMake(165 * 10, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled=0;
}
- (void)viewDidDisappear:(BOOL)animated
{
// [self setDescText:nil];
[super viewDidDisappear:animated];
[movieplayer pause];
[movieplayer.view removeFromSuperview];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ((movie.playbackState==MPMoviePlaybackStateStopped)||(movie.playbackState==MPMoviePlaybackStatePaused))
{
[movie play];
}
else
{
[movie pause];
}
}
Try this Code , it will Work
Check out the notifications for MPMoviePlaybackStatePlaying, MPMoviePlaybackStateStopped, MPMoviePlaybackStatePaused, and MPMoviePlaybackStateInterrupted.
Something like:
MPMoviePlayerController *player = notification.object;
/* Playback is currently stopped. */
if (player.playbackState == MPMoviePlaybackStateStopped)
{
NSLog(#"MPMoviePlaybackStateStopped");
}
/* Playback is currently under way. */
else if (player.playbackState == MPMoviePlaybackStatePlaying)
{
NSLog(#"MPMoviePlaybackStatePlaying");
}
/* Playback is currently paused. */
else if (player.playbackState == MPMoviePlaybackStatePaused)
{
NSLog(#"MPMoviePlaybackStatePaused");
}
You can wire up your target action something like this:
if ((_moviePlayer.playbackState == MPMoviePlaybackStateStopped) || (_moviePlayer.playbackState == MPMoviePlaybackStatePaused)) {
[_moviePlayer play];
} else {
[_moviePlayer pause];
}
you can add target to the play/pause button.
but first you need to catch the button of the mpmovieplayerview.
step 1 list the button. method reference from here
call this method from when the button appear(the video is ready).
But remember, this will also catch the full screen button and airplay button.(if available)
- (void)CatchSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
for (UIView *subview in subviews) {
// Do what you want to do with the subview
NSLog(#"%#", subview);
if(subview isKindOfClass:[UIButton class]]){
// add your target here
[subview addTarget:self action:#selector(extraAction:) forControlEvents:UIControlEventTouchUpInside];
}
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
step 2 implement the action
-(IBAction)extraAction:(id)sender{
NSLog(#"some extraAction");
}
call the catch method sample.
[self CatchSubviewOfView:movieplayer.view];

iPhone development mpmovieplayer crashing

I am working on an app that will let me play different videos on the iPad remotely with an iPhone. I have been following along with apples example for a video player but I've been having some troubles. The videos play just fine and I can get it to play from a variety of videos but switching between them a few times it will crash and i get this in the debugger:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
*** First throw call stack:
(0x380da8bf 0x37c261e5 0x30acbcb5 0x30abc1f7 0x30ac3bf3 0x30c93d55 0x30c95f7b 0x380ad2dd 0x380304dd 0x380303a5 0x37e07fcd 0x31bb0743 0x25e5 0x257c)
This is the code I am using to create the player:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
if (player) {
[self setMoviePlayerController:player];
[self installMovieNotificationObservers];
[player setContentURL:movieURL];
[player setMovieSourceType:sourceType];
[self applyUserSettingsToMoviePlayer];
[self.view addSubview:self.backgroundView];
[player.view setFrame:self.view.bounds];
[player.view setBackgroundColor = [UIColor blackColor];
[self.view addSubview:player.view];
}
And when the current movie is stopped I use:
[[self moviePlayerController] stop];
MPMoviePlayerController *player = [self moviePlayerController];
[player.view removeFromSuperview];
[self removeMovieNotificationHandlers];
[self setMoviePlayerController:nil];
Edit:
So Ive now discovered it happens every time i try and switch a video for the 11th time. weird! I'm practically pulling my hair out.
What fixed this problem for me was stopping the MPMoviePlayerController before doing the setContentURL.
MPMoviePlayerController *streamPlayer;
[streamPlayer stop];
[streamPlayer setContentURL:[NSURL URLWithString:selectedStation]];
In the implementation you have above, ARC doesn't know that the MPMoviePlayerController is finished and needs to be released.
Define MPMoviePlayerController in your .h file and make it accessible via a #property (and #synthesize).
#property (strong, nonatomic) MPMoviePlayerController * moviePlayerController;
Then take the result of your alloc & init and assign it to that. I.E.
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
you should just keep the moviePlayerController and if you want to play another video, just use
[self.moviePlayerController setContentURL:movieURL];
then in your notification callback:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
self.moviePlayer = nil;
[self initanothermovieplayerandplay];
}
and please do not remove the notification handler from notification center, only do this in dealloc method of your VC.
now let's add some fade when the movie play is done:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[UIView animateWithDuration:1
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
// one second to fade out the view
self.moviePlayer.view.alpha = 0.0;
}
completion:^(BOOL finished){
self.moviePlayer = nil;
[self initanothermovieplayerandplay];
}
}
I had exactly the same problem.
Nothing was wrong with my and i guess with your code :)
Just a broken video file was mine problem.
Changing *.mov type to m4a for example fixed it. Maybe one or more of the files you play are corrupted?
Try to find out which files lead to crash and than if u can try to quickly forward backward the play position of one of them while playing - this should lead to crash in few tries.
This is how i found the bad files. By the way all my bad files were movies .mov made with Snapz Pro X :)
Not sure if it is the case here, but we had a lot of problems, because the MPMoviePlayer is a singleton somewhere under the hood.
What we did is, that we implemented our own MoviePlayer wrapper which can be used from UIView (actually we have exactly one subclass of UIView MoviePlayerView to show movies) and assures that only one instance of MPMoviePlayerController exists. The code goes like this (it contains some special stuff, we need to show previews/thumbs the way we want etc. you should clean up as well as some release-statements):
// MoviePlayer.h
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "Logger.h"
#class MoviePlayerView;
#interface MoviePlayer : NSObject
{
#private
MPMoviePlayerController *controller;
MoviePlayerView *currentView;
}
#property (nonatomic, readonly) MPMoviePlayerController *controller;
+(MoviePlayer *) instance;
-(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view;
-(void) stopMovie;
#end
// MoviePlayer.m
#import "MoviePlayer.h"
#import "MoviePlayerView.h"
#implementation MoviePlayer
#synthesize controller;
static MoviePlayer *player = nil;
#pragma mark Singleton management
+(MoviePlayer *) instance
{
#synchronized([MoviePlayer class])
{
if (player == nil)
{
player = [[super allocWithZone:NULL] init];
player->controller = [[MPMoviePlayerController alloc] init];
player->controller.shouldAutoplay = NO;
player->controller.scalingMode = MPMovieScalingModeAspectFit;
player->currentView = nil;
}
return player;
}
}
+(id) allocWithZone:(NSZone *)zone
{
return [[self instance] retain];
}
-(id) copyWithZone:(NSZone *)zone
{
return self;
}
-(id) retain
{
return self;
}
-(NSUInteger) retainCount
{
return NSUIntegerMax;
}
-(oneway void) release
{
// singleton will never be released
}
-(id) autorelease
{
return self;
}
#pragma mark MoviePlayer implementations
-(void) stopMovie
{
#synchronized(self)
{
if (controller.view.superview)
{
[controller.view removeFromSuperview];
}
if (controller.playbackState != MPMoviePlaybackStateStopped)
{
[controller pause];
[controller stop];
}
if (currentView)
{
NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
[ntfc removeObserver:currentView name:MPMoviePlayerLoadStateDidChangeNotification object:controller];
[ntfc removeObserver:currentView name:MPMoviePlayerPlaybackStateDidChangeNotification object:controller];
currentView = nil;
}
}
}
-(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view
{
#synchronized(self)
{
[self stopMovie];
currentView = view;
NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
[ntfc addObserver:currentView
selector:#selector(loadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:controller];
[ntfc addObserver:currentView
selector:#selector(playbackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:controller];
[controller setContentURL:movieURL];
controller.view.frame = view.bounds;
[view addSubview: controller.view];
[controller play];
}
}
#end
// MoviePlayerView.h
#import <UIKit/UIKit.h>
#import "MoviePlayer.h"
#interface MoviePlayerView : MediaView
{
NSURL *movieURL;
NSURL *thumbnailURL;
UIImageView *previewImage;
UIView *iconView;
BOOL hasPreviewImage;
}
-(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie;
-(void) loadStateDidChange:(NSNotification *)ntf;
-(void) playbackStateDidChange:(NSNotification *)ntf;
#end
// MoviePlayerView.m
#import "MoviePlayerView.h"
#interface MoviePlayerView()
-(void) initView;
-(void) initController;
-(void) playMovie;
-(void) setActivityIcon;
-(void) setMovieIcon:(float)alpha;
-(void) clearIcon;
-(CGPoint) centerPoint;
#end
#implementation MoviePlayerView
-(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie
{
self = [super initWithFrame:frame];
if (self)
{
movieURL = [movie retain];
thumbnailURL = [thumbnail retain];
[self initView];
[self initController];
hasPreviewImage = NO;
loadingFinished = YES;
}
return self;
}
-(void) dealloc
{
[iconView release];
[previewImage release];
[movieURL release];
[super dealloc];
}
-(void)initView
{
self.backgroundColor = [UIColor blackColor];
// add preview image view and icon view
previewImage = [[UIImageView alloc] initWithFrame:self.bounds];
[previewImage setContentMode:UIViewContentModeScaleAspectFit];
UIImage *img = nil;
if (thumbnailURL)
{
img = [ImageUtils loadImageFromURL:thumbnailURL];
if (img)
{
previewImage.image = img;
hasPreviewImage = YES;
}
}
[self addSubview:previewImage];
[self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
}
-(void)initController
{
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(playMovie)];
[self addGestureRecognizer:rec];
[rec release];
}
-(void)playMovie
{
[[MoviePlayer instance] playMovie:movieURL onView:self];
[self setActivityIcon];
}
-(void) loadStateDidChange:(NSNotification *)ntf
{
MPMoviePlayerController *controller = [ntf object];
switch (controller.loadState)
{
case MPMovieLoadStatePlayable:
{
[self clearIcon];
[controller setFullscreen:YES animated:YES];
break;
}
case MPMovieLoadStateStalled:
{
[self setActivityIcon];
break;
}
default:
{
break; // nothing to be done
}
}
}
-(void) playbackStateDidChange:(NSNotification *)ntf
{
MPMoviePlayerController *controller = [ntf object];
switch (controller.playbackState)
{
case MPMoviePlaybackStatePlaying:
{
[self clearIcon];
break;
}
case MPMoviePlaybackStateStopped:
{
[self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
break;
}
case MPMoviePlaybackStatePaused:
{
[self setMovieIcon:0.8f];
break;
}
case MPMoviePlaybackStateInterrupted:
{
[self setActivityIcon];
break;
}
default:
{
break; // nothing to be done
}
}
}
-(void) setActivityIcon
{
[self clearIcon];
iconView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
iconView.center = [self centerPoint];
[self addSubview:iconView];
[iconView performSelector:#selector(startAnimating)];
}
-(void) setMovieIcon:(float)alpha
{
[self clearIcon];
iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_movie.png"]];
iconView.center = [self centerPoint];
iconView.alpha = alpha;
[self addSubview:iconView];
}
-(void) clearIcon
{
if (iconView)
{
SEL stop = #selector(stopAnimating);
if ([iconView respondsToSelector:stop])
{
[iconView performSelector:stop];
}
[iconView removeFromSuperview];
[iconView release];
iconView = nil;
}
}
-(CGPoint) centerPoint
{
return CGPointMake(roundf(self.bounds.size.width / 2.0f), roundf(self.bounds.size.height / 2.0f));
}
-(void)resize
{
for (UIView *view in [self subviews])
{
if (view == iconView)
{
iconView.center = [self centerPoint];
continue;
}
view.frame = self.bounds;
}
[self addCaptionLabel];
}
-(void) layoutSubviews
{
[super layoutSubviews];
[self resize];
}
#end
...
player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:...
...
but I didn't gave internet connection to phone (wi-fi) :)
I had the same problem. My solution is using prepareToPlay:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
if (player) {
[player prepareToPlay];
//...
}
This error seems to be thrown for lots of different reasons, but the reason I found was that the MPMoviePlayerController class freaks out if you call methods in a certain order. From an IRC Channel:
"apparently if you call prepareToPlay WHILE setting source type and
NOT setting the view yet causes this crash"
So I fixed this by just making sure that I called prepareToPlay: LAST (or second to last, with the last being play:).
It is also weird because my original code worked in iOS 5.1, but this problem suddenly manifested when I started using the iOS 6.0 sdk. It is possibly a bug in the MPMoviePlayerController code, so I'm going to be filing a radar report on it, as calling prepareToPlay: before setting the view / setting the sourceFileType should not throw an exception (or at least an exception that seemingly has nothing to do with the actual error)

Resources