I am new to Xcode so I've been trying to follow along to tutorials but haven't come across any that explain what I'm trying to achieve. I just want to play a video automatically when you open an application. I have it somewhat working in that the audio plays, but I cannot see any video. Am I missing something? I am getting this Warning in my output window:
Warning: Attempt to present MPMoviePlayerViewController: 0x831d7a0 on ViewController: 0x9d10540 whose view is not in the window hierarchy!
In my ViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Placeholder" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Instead of
[playercontroller.moviePlayer play];
do
[playercontroller play];
Side note:
Also check up on your naming convention. In Objective-C variable names "should" have capital letter on each new word in the variable. E.g. instead of playercontroller use playerController.
I know your status.
it's warning from Xcode.
So, you just change the code.
like this,
[playercontroller performSelector:#selector(player)];
Related
I am new from iOS app development ,my requirement is need access the progress slider while playing , and show the proper start and end time , the player is working in url streaming , can you please help me to give some sample code , to fix this issue , Thanks in advance.
Please find the below code. i have tested in Xcode 8 beta 2
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
{
IBOutlet UISlider *sliderView;
IBOutlet UILabel *lblTimeInterval;
AVAudioPlayer *avpPlayer;
}
#end
#implementation ViewController
#synthesize strName;
#synthesize strTitle;
#synthesize trackCount;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//First comment added to Proj.
//New Branch one is created by Team.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)progrssSlider {
avpPlayer.currentTime = sliderView.value;
}
- (void)updateTime:(NSTimer *)timer {
sliderView.value = avpPlayer.currentTime;
lblTimeInterval.text = [NSString stringWithFormat:#"%f",sliderView.value];
}
- (IBAction)pressPlayButton:(id)sender {
//Read sound file from resource folder
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sample.mp3" ofType:nil]];
//Initialize AVAudioPlayer
NSError *error;
avpPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
//Check Player is initialized
if (!avpPlayer){
NSLog(#"Error: %#", error);
}
else
{
[avpPlayer prepareToPlay];
sliderView.maximumValue = [avpPlayer duration];
sliderView.value = 0.0;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime:) userInfo:nil repeats:YES];
[avpPlayer play];
}
}
#end
Please define UILAbel to show Time, UISlider to Update Slider and Play button in Xib.
Hope this will work.
I implemented a MPMoviePlayerViewControllerlike this:
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UITableViewController
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
/* SOME CODE AND A TAPGESTURERECOGNIZER */
/* CALLS [self playVideo] */
-(void) playVideo{
#try {
NSURL *URL = [NSURL URLWithString:#"myGeneratedURL"];
NSLog(#"URL: %#",URL);
MPMoviePlayerViewController *video = [[MPMoviePlayerViewController alloc]
initWithContentURL:URL];
[self.navigationController presentMoviePlayerViewControllerAnimated:video];
}
#catch (NSException *exception) {
}
}
So now my problem is that it works just fine on both simulator and on my iphone (if i debug it), but when i upload a build to itunesconnect and download it via TestFlight the MPMoviePlayerViewController does not show up. The URL in NSLog is definitely the same in all three cases ( sim , iphone debug, testflight ).
I don't know where i should start searching the error...
I would suggest using strong reference rather than an local variable.
You can follow this tutorial
Hi im creating an app for a uni project which requires me to play videos from a server.
i've decided to use Dropbox. However it would not play the file, is there any way for me to do it? or do i need to get the video from somewhere else.
ive tried another link which works perfectly fine. (not dropbox however)
http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v
my Coding Below
AnimeWatchViewController.m
//
// AnimeWatchViewController.m
// VideoPlayer
//
// Created by Alex Lee on 27/04/2014.
// Copyright (c) 2014 Alex Lee. All rights reserved.
//
#import "AnimeWatchViewController.h"
#interface AnimeWatchViewController ()
#end
#implementation AnimeWatchViewController
#synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)playMovie{
NSURL * url =[[NSURL alloc] initWithString:#"https://www.dropbox.com/s/t05zdw2woogo4kh/ACW_3.mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(20, 100, 275, 150)];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = YES;
moviePlayer.controlStyle = MPMovieControlModeDefault;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
AnimeWatchViewController.h
//
// AnimeWatchViewController.h
// VideoPlayer
//
// Created by Alex Lee on 27/04/2014.
// Copyright (c) 2014 Alex Lee. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface AnimeWatchViewController : UIViewController
#property(nonatomic,strong) MPMoviePlayerController * moviePlayer;
-(IBAction)playMovie;
#end
You're using the URL https://www.dropbox.com/s/t05zdw2woogo4kh/ACW_3.mp4, but that's not a link to a video... it's a link to a page with a video on it.
To convert it to a direct link to the video, change www.dropbox.com to dl.dropboxuser.com, like this: https://dl.dropboxusercontent.com/s/t05zdw2woogo4kh/ACW_3.mp4
for that you have to first get direct download link then you can play video directly from dropbox server
Try to understand this link may help you
I’m making a video that shows overlays at certain intervals of a video. I’ve managed to get the video to play. Now my objective is to watch/observe the currentPlaybackTime value and pause the video when it hits 2 seconds.
After some research found that currentPlaybackTime does not support KOV. So I need to implement this solution but I have no idea where to put the code - I’m very new to Objective C. I keep trying to put it in the ViewController (My only view) but the way its written hints to it being placed somewhere else…
Should I create a new controller for it? Or do I override methods from the MediaPlayer framework?
Here's my code:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) MPMoviePlayerController *movieController;
#property(nonatomic) NSTimeInterval currentPlaybackTime;
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated {
self.movieController = [[MPMoviePlayerController alloc] init];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.movieController.view setFrame:CGRectMake (0, 0, 480, 326)];
[self.view addSubview:self.movieController.view];
[self.movieController play];
[self.movieController currentPlaybackTime];
}
#end
You can implement following method in your found solution in this same view controller.
Right after
[self.movieController play];
call following method
[self BeginPlayerPolling];
Register this class as an observer before in your viewDidAppear where you initialised your movieController
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object: self.movieController];
self.movieController = [[MPMoviePlayerController alloc] init];
and implement this notification observer's method
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: self.movieController];
[self EndPlayerPolling];
}
For example, on the iOS SDK download page, there's sample code; I'm using the calculator app (iPhoneUnitTests). I would like to know if it is possible to add sounds to the button presses on the app that's already built, easily.
It is actually very simple to play short sounds, like button sounds. Here is a quick example.
You must link the AudioToolbox.framework binary
SoundExampleViewController.h;
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface SoundExampleViewController : UIViewController{
SystemSoundID mySoundID;
}
#end
SoundExampleViewController.m:
#import "SoundExampleViewController.h"
#implementation SoundExampleViewController
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SoundFileName" ofType:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
[super viewDidUnload];
AudioServicesDisposeSystemSoundID(mySoundID);
}
#end
Then to play you simply call: AudioServicesPlaySystemSound(mySoundID);