Is it possible to make the video not close after it has finished playing? So this would allow the user to press "done" when the video is finished and then the video will close.
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface player1: UIViewController {
}
-(IBAction) playMovie;
#end
ViewController.m
#import "ViewController.h"
#implementation player1
-(IBAction) playMovie {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"2.DragtoopenfileinaVM" ofType:#"mp4"]];
MPMoviePlayerViewController *windowsmac = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:windowsmac];
windowsmac.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[windowsmac.moviePlayer play];
windowsmac = nil;
}
This is the coding I'm using, could someone help?
moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter] removeObserver:moviePlayer
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer.moviePlayer];
[self presentViewController:moviePlayer animated:YES completion:nil];
Related
I try to play audio from server url but nothing play, But i try to play audio from document directory path url and it play fine.
NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString:
[NSString stringWithFormat:#"%#",aSongURL]]];
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil];
abc.numberOfLoops=0;
[abc prepareToPlay];
[abc play];
You can try this. Hope it helps.
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
My answer is below
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender {
NSString *strAudioFileURL = #"dev.epixelsoft.co/love_app/audio/img_14957068361.caf";
NSURL *soundFileURL = [NSURL fileURLWithPath:strAudioFileURL];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];
}
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!
So I have an app where I want to include background music. I use the following code for the music:
.h: (I did also add the framework)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
{
AVAudioPlayer *player;
}
.m:
- (void)viewDidAppear:(BOOL)animated {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player play];
NSLog(#"playing music");
}
The path for the music file is definitly correct. The app does not crash but I just do not hear any music.
By the way: the LogMessage ("playing music") appears in the debugger.
Any ideas?
Add prepareToPlay method before [player play];
[player prepareToPlay];
Hope this will work...
im trying to add a video to a button click (native video) i had the code down perfect and it compiles and runs ok but when i click on that button thats meant to natively play the video... it gives me a "Thread 1: Signal SIGBART" ??? how can i fix this please?
i have followed everything as i should, imported the video into supporting files and made sure to give it the same name in .m as its file name.
Tried adding a breakpoint exception but wont work
here is the code in .h
#interface ViewController5 : UIViewController {
}
-(IBAction)play;
#end
here is the code in .m
#implementation ViewController5
-(IBAction)play {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test video" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
}
#end
And here is the "green" highlight signal i get
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
If you're using ARC, you should keep a reference to MPMoviePlayerViewController instance so it's not deallocated when play method returns, and don't set it to nil.
// ViewController5.h
#interface ViewController5 : UIViewController {
MPMoviePlayerViewController *playercontroller;
}
- (IBAction)play;
#end
// ViewController5.m
#implementation ViewController5
- (IBAction)play {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test video" ofType:#"mp4"]];
playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
}
#end
I'm trying to create an iOS/iPhone radio app using Xcode 4.5.2.
I wanted to stream #"http://xx.xxxxxxx.com/8111/radio.m3u" with play, pause, volume control and able to play on background feature/multitasking.
I've added AVFoundation, Mediaplayer and AudioToolBox frameworks thus far. I've added play, pause and slider objects to xib.
ViewController.h
#interface ViewController : UIViewController
#property (strong,nonatomic) MPMoviePlayerController *myPlayer;
#property (weak, nonatomic) IBOutlet UISlider *myslider;
- (IBAction)playButtonPressed;
- (IBAction)myslider:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController ()
{
UISlider *volumeSlider;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
- (IBAction)playButtonPressed;
{
NSString *urlAddress = #"http://xxxxxxx.com/8111/listen.m3u";
NSURL *url = [NSURL URLWithString:urlAddress];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;
[player prepareToPlay];
self.myPlayer = player;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer play];
}
- (IBAction)stopButtonPressed;
{
[self.myPlayer stop];
}
- (IBAction)myslider:(id)sender
{
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(10, 10, 200, 40)];
[volumeSlider addSubview:volumeView];
[volumeView sizeToFit];
}
There are Two way to achieve this.
You can directly load you URL in UIWebView and it will properly.
You can also use MPMoviePlayerController.
Create a "MPMoviePlayerController *player" as a strong object in your ViewController.
So you code would look something like below:
#interface ViewController ()
{
UISlider *volumeSlider;
MPMoviePlayerController *player;
}
#end
- (IBAction)playButtonPressed;
{
NSString *urlAddress = #"http://xxxxxxx.com/8111/listen.m3u";
NSURL *url = [NSURL URLWithString:urlAddress];
if(nil != player)
{
player = nil; // Alternatively you can stop and restart with the different stream.
}
player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;
[player prepareToPlay];
self.myPlayer = player;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer play];
}