I'm taking an online course and they're using outdated code using MPMoviePlayerController, and I need specific instructions on what to change it to so that it works. The app I'm making has a table view controller with an inbox with messages in it. Short video files can be sent, and when you tap on a row that has a video message, I need the video file to play. The free trial is expiring soon, so I need help please.
This is what needs to be changed in my header file:
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
This is what needs to be changed in my viewDidLoad method in my implementation file:
self.moviePlayer = [[MPMoviePlayerController alloc] init];
This is also what needs to be changed in the implementation file. I'm getting the videos through a backend via Parse.com, so that's what the PFFile is about:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PFFile *videoFile = [self.selectedMessage objectForKey:#"file"];
NSURL *fileUrl = [NSURL URLWithString:videoFile.url];
self.moviePlayer.contentURL = fileUrl;
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
In header file :
#property(nonatomic,strong)AVPlayer *avPlayer;
In viewDidLoad method in implementation file
self.avPlayer = [[AVPlayer alloc] init];
In implementation UITableView Method :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PFFile *videoFile = [self.selectedMessage objectForKey:#"file"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFile.url];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, width, height);
[self.view.layer addSublayer: layer];
[self.avPlayer play];
}
Hope it will help you :)
Related
I am trying to play a video which is stored on my device trying both AVPlayer and MPMoviePlayerController.
Video URl is: file:///var/mobile/Containers/Data/Application/73695F3E-9351-447B-BB8A-0B4A62CE430F/Documents/08-03-201711:48:50.3gp. Now the problem is I get blank screen.
AVPlayer *player = [AVPlayer playerWithURL:fileURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player pause];
[player play];
playerViewController.player = player;
[self addChildViewController: playerViewController];
[self.view addSubview: playerViewController.view];
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
I think the problem is in link. What I am doing is getting local directory link and append name with that link.
I will you good solution.It works perfectly.
ViewController.h
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController (){
NSURL *vedioURL;
}
#end
#implementation ViewController
#synthesize playerViewController;
- (IBAction)actionPlayVideo:(id)sender{
NSString *fullpath = [[self documentsDirectory] stringByAppendingPathComponent:#"yourdate.3gp"];
vedioURL =[NSURL fileURLWithPath:fullpath];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:vedioURL];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = playVideo;
playerViewController.player.volume = 0;
playerViewController.view.frame = self.view.bounds;
[self.view addSubview:playerViewController.view];
[playVideo play];
}
-(NSString *)documentsDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
EDIT: MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
You can use MPMoviePlayerController to play local file.
Add Mediaplayer framework and do #import in your viewController.
Drag and drop your video file you created on desktop into the xcode.
Get the path of the local video.
NSStringthePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"MOV"];
NSURLtheurl=[NSURL fileURLWithPath:thePath];
Initialize the moviePlayer with your path.
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:self.moviePlayer.view];
To control what is to be done after playback:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//playBackFinished will be your own method.
EDIT 2: To handle completion for AVPlayerViewController rather than MPMoviePlayerController, use the following...
AVPlayerItem *playerItem = player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
In this example, I dismiss the AVPlayerViewController after completion:
-(void)playBackFinished:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[playerViewController dismissViewControllerAnimated:false completion:nil];
}
It may happen because of your file url is incorrect.
Try below codes to get the path of your answer.
If you have the video in your mac, then copy it in to your project firstly, just drug it to Xcode.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"08-03-201711:21:67" ofType:#"3gp"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
And use this path to AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:fileURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player pause];
[player play];
playerViewController.player = player;
[self addChildViewController: playerViewController];
[self.view addSubview: playerViewController.view];
**As I review your code, a blank screen may caused by you adding AVPlayer it into layer but not as subview/childviewcontroller. So try my code!
I want to play music from URL with an AVPlayer on the iOS Simulator, but it doesn't work :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Track *selectedTrack = self.tracksByAlbum[indexPath.row];
NSLog(selectedTrack.preview);
NSURL *url = [NSURL URLWithString:[selectedTrack.preview stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
if (!player) {
NSLog(#"Error");
}
[player play];
}
The url is :
http://cdn-preview-6.deezer.com/stream/6d180c6b61078f437b04ec21bbe0fb76-3.mp3
I read that I have to enable user interface sounds effects on OS X, but it doesn't change anything...
If you're using ARC, you'll need to retain the AVAudioPlayer as it sets to nil automatically, which would be a likely reason for no sound. In your header file if you could make sure your self.player's property is set up like this,
#property (strong, nonatomic) AVPlayer *player;
from within your didSelectRowAtIndexPath change the AVPlayer code to this
self.player = [[AVPlayer alloc] initWithURL:url];
if (!self.player) {
NSLog(#"Error");
}
[self.player play];
I hope this helps
I'm trying to play audio from a URL using AVPlayer when a row is selected in a table view but it doesn't play. What am I doing wrong?
Here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *streamingString = [NSString stringWithFormat:#"%#.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
NSURL *streamingURL = [NSURL URLWithString:streamingString];
NSLog(#"%#", streamingURL);
AVPlayer *player = [AVPlayer playerWithURL:streamingURL];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}
An example of streamingURL when I log it is this.
http://api.soundcloud.com/tracks/146814101/stream.json?client_id=fc886d005e29ba78f046e5474e3fdefb
declare AVAudioPlayer globally in your .h file
like this AVPlayer *player;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *streamingString = [NSString stringWithFormat:#"%#.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
NSURL *streamingURL = [NSURL URLWithString:streamingString];
NSLog(#"%#", streamingURL);
player = [AVPlayer playerWithURL:streamingURL];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}
You'll find numerous audio streaming kit all around GitHub.
https://github.com/tumtumtum/StreamingKit
https://github.com/muhku/FreeStreamer
https://github.com/mattgallagher/AudioStreamer
call this method
[player prepareToPlay];
before calling
[player play];
It may help to u.
Here i want to play videos from server using MediaPlayer framework. 1)I am adding mediaplayer framework. 2)I am importing #import header file 3)I implemented code by using google.
My Code is :
- (void)act_GoToVideoPlayer:(UIButton*)sender
{
NSString* resourcePath = [NSString stringWithFormat:#"http://%#",[postMediaFileArr objectAtIndex:sender.tag]]; //Video file URL is getting from server and stored it in a MutableArray
NSURL *url = [NSURL URLWithString: resourcePath];
mediaplayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]];
[mediaplayer.view setFrame:CGRectMake(20, 100, 250, 150)];
[self.view addSubview:mediaplayer.view];
mediaplayer.fullscreen = YES;
mediaplayer.allowsAirPlay = YES;
mediaplayer.shouldAutoplay = YES;
mediaplayer.controlStyle = MPMovieControlStyleEmbedded;
}
The view goes to blank screen and infinite loading when this function is called. I have tried many other versions of this implementation and the results are all fail. The log in the is particular case is:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
If i passed any other videos from websites. it will be playing audios not videos.
Got ideas on the cause? This is my first venture into playing video and it has turned out to be a nightmare at this point.
-(void)notifyCompletelyVisible
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
this is working just import
import "MediaPlayer/MediaPlayer.h"
you try in .h:
#import <MediaPlayer/MediaPlayer.h>
and you add delegate
interface YourViewcontroler : UIViewController <MPMediaPlayback>
{
MPMoviePlayerController *player;
}
and in your .m file:
-(void) launchVideo{
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString* myURL =#"http://yourUrl/file.mp4";
NSURL *urlString = [NSURL URLWithString:myURL];
player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player view].alpha=1;
[appDelegate.window.rootViewController.view addSubview:[player view]];
[player play];
}
and the delegate methode for finishing:
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[player.view removeFromSuperview];
[player stop];
player=nil;
[player release];
}
i hope this help you
I have a movie that loads fine and plays fine but it plays as soon as the page loads. I would like it to load in a paused state requiring the user to hen press play to watch.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController pause];
}
The pause on the last line does not seem to do anything. Any help? Thanks
Put your code blurb you've written above in viewwillappear.
Then add play at the end like so:
-(void) viewWillAppear: (BOOL) animated {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
}
at the end of it.
In a viewdidappear do a
-(void) viewDidAppear: (BOOL) animated {
[moviePlayerController pause];
}
Remember to remove code from viewDidLoad
You might want to try setting the MPMoviePlayerController property shouldAutoplay to NO.
Add the following line before your prepareToPlay call;
moviewPlayerController.shouldAutoplay = NO;
From the reference:
shouldAutoplay
A Boolean that indicates whether a movie should begin playback automatically.
#property (nonatomic) BOOL shouldAutoplay
Discussion
The default value of this property is YES. This property determines
whether the playback of network-based content begins automatically
when there is enough buffered data to ensure uninterrupted playback.
Availability
Available in iOS 3.2 and later.
Declared In