I am new at Swift Language. I want to develop simple movie player app for learning. I added some movies(".avi, .flv, .f4u.... which format is best) to my project. My view is just simple.There is a button on my view. ITs called "Play". When user tap on that button the movies will start.
Plus I need to play audio files too, like mp3 or .mid files. Can you give me how i can do that in Swift? IF you explain with example, It will be perfect.
Edit
I am not asking all code. I get confused when I try to convert my Objective C code to Swift. I want to know how I can develop below code in Swift?
NSString *path = [[NSBundle mainBundle] pathForResource:#"eminem1" ofType:#"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
Try this:
let path = NSBundle.mainBundle().pathForResource("eminem1", ofType: "mp4")
let pathURL = NSURL.fileURLWithPath(path)
let myPlayer = MPMoviePlayerViewController(contentURL: pathURL)
myPlayer.moviePlayer.repeatMode = .None
presentMoviePlayerViewControllerAnimated(myPlayer)
Don't forget import MediaPlayer.
Selected answer is deprecatedfor iOS 9+ versions.
Use this answer instead: How to play a local video with Swift?
Related
I am using Xcode 7 and with quick look feature i encountered this error. It says could not load quick look data for "fileURL" here is my code
// here because of below line #file gets attached to the end of my fileURL like this --
#"http:/example.com/some_clips/SHAINA%20NC%20NEWS%20X%20180516%201657PM.mp4 -- file:///" see here something file:/// in the end so because of this i am unable to play video.
IF i use initWithString and give URL directly then it gives no error please suggest me something to set URL to my player below i have created please read comment in the code also. Do anyone have idea about this.
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]]; //here i am getting my URL path upto .mp4 but after below line word -- file gets attached to it because of fileURLWithPath method don't know how to convert that
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath]; //suggest me this line of code so that i can pass URL to the player here this method is not working
// create a player
player = [AVPlayer playerWithURL:fileURL]; //here this fileURL which is going to player is going with that file:/// so giving error and no video runs
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
[player pause];
You should use:
NSURL *fileURL = [NSURL URLWithString:localfilepath];
Instead of:
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath];
The fileURLWithPath: method is used for creating local file (Files that are situated in the device itself) urls.
i have made it guys :) i found the answer :) This is complete code for playing videos into AVPlayer which are on server
// here i am fetching all the url downloaded from server using core data managed objects
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]];
// then on below line i am encoding it into NSUTF8StringEncoding format
don't forget to do this otherwise video will not play as it wasn't in my case
NSURL *url = [NSURL URLWithString:[localfilepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// and below i have assigned url to the player and created the player
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
//player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];
*Below two lines are for those who wants to use AVPlayer in their app.
This player works fine if i uncomment middle line in comments i.e NSURL *url and set the first line below comments i.e playerWithUrl:url , and comment the starting two lines.
*Below is My Real Problem, I need Help.
But i am not able to set URL the value to the player method-playerWithURL which is a first line below comments, and i am setting it through fileURL instance which i have created at starting in 2 line. IN the debugger it shows API path in *localfilepath. but when i am setting local file path to player through fileURL it shows nil.
But if i use middle line in comments i.e set *url for player everything works fine.
Help me to convert API into URL for that player property
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]];
NSURL *fileURL=[NSURL fileURLWithPath:localfilepath]; //because of this i am getting video URL but something #"file://" gets attached to it in the end thus not playing video URL because of that fileURLWithPath suggest something another way to fetch URL
// videoAVURLAsset = [AVURLAsset assetWithURL:fileURL];
// NSURL *url = [[NSURL alloc] initWithString:#"http://www.example.com/example_clips/SHAINA%20NC%20NEWS%20X%20180516%201657PM.mp4"];
// created player here //
player = [AVPlayer playerWithURL:fileURL]; //here i want to set the value which is in the *localfileurl
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
[player pause];
[player play];
You should be using [NSURL fileURLWithPath:] to generate the URL object from a local file path string:
NSURL *fileURL = [NSURL fileURLWithPath:localfilepath];
Here is how I play a video in iOS:
First create a web view
Then link it to your view controller code
and then add this in the viewdidload()
//add the video to the player
let myYoutubeVideo = "https://www.youtube.com/embed/YNUEAxUGxyE"
let myHTMLString = "<iframe width=\"\(videoPlayer.frame.width)\" height=\"\(videoPlayer.frame.height)\" src=\"\(myYoutubeVideo)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>"
videoPlayer.allowsInlineMediaPlayback = true
videoPlayer.loadHTMLString(myHTMLString, baseURL: nil)
videoPlayer.frame = UIScreen.mainScreen().bounds
videoPlayer.center = self.view.center
videoPlayer.backgroundColor = UIColor.clearColor()
Try putting the file in this directory
#if TARGET_IPHONE_SIMULATOR
// where are you?
NSLog(#"Documents Directory: %#", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif
This is the Documents directory for your app in the simulator. Then try to run it from there.
That way it should act like its pulling off the phone and not your local pc.
And give this a try too
file:///Users/codercat/Library/Developer/CoreSimulator/Devices/YourProjectDict/data/Containers/Data/Application/(sample digit)7F53CFB3-C534-443F-B595-62619BA808F2/Documents/your file located here
This one is for Xcode 7 and higher
i have made it guys :) i found the answer :) this is a complete solution if you have many video URL's and you want to play videos from server directly in your player so you need to fetch all your clip paths properly first and use the following code
// here i am fetching all the url downloaded from server using core data managed objects
NSString *localfilepath=[NSString stringWithFormat:#"%#",[managedObject valueForKey:#"clip_path"]];
// then on below line i am encoding it into NSUTF8StringEncoding format
don't forget to do this otherwise video will not play as it wasn't in my case
NSURL *url = [NSURL URLWithString:[localfilepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// and below i have assigned url to the player and created the player
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(10,80,300,300);
controller.player = player;
controller.showsPlaybackControls = YES;
//player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];
How would one play a YouTube video on Apple tvOS?
YouTube's embed feature works in iOS 9 as the OS has a UIWebView we can embed it into.
The new tvOS does not include a UIWebView so I cannot see a way to embed the YouTube video.
UIWebView and MPMoviePlayerController are not available for tvOS. Our next option is to use AVPlayer to play YouTube videos.
AVPlayer cannot play a YouTube video from a standard YouTube URL, ie. https://www.youtube.com/watch?v=8To-6VIJZRE. It needs a direct URL to the video file. Using HCYoutubeParser we can accomplish exactly that. Once we have the URL we need, we can play it with our AVPlayer like so:
NSString *youTubeString = #"https://www.youtube.com/watch?v=8To-6VIJZRE";
NSDictionary *videos = [HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:youTubeString]];
NSString *urlString = [NSString stringWithFormat:#"%#", [videos objectForKey:#"medium"]];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:urlString]];
AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
avPlayerLayer.frame = playerView.layer.bounds;
[playerView.layer addSublayer:avPlayerLayer];
[videoPlayer play];
Note that this is NOT allowed under YouTube's TOS. Use it at your own risk. Your app may stop working at any point if YouTube notices you are not following the TOS or if YouTube changes the embed code it generates.
Swift 2.0 version of Daniel Storm's answer:
let youTubeString : String = "https://www.youtube.com/watch?v=8To-6VIJZRE"
let videos : NSDictionary = HCYoutubeParser.h264videosWithYoutubeURL(NSURL(string: youTubeString))
let urlString : String = videos["medium"] as! String
let asset = AVAsset(URL: NSURL(string: urlString)!)
let avPlayerItem = AVPlayerItem(asset:asset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
let avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height);
self.view.layer.addSublayer(avPlayerLayer)
avPlayer.play()
XCDYouTubeKit has been a popular way to directly play a YouTube video on iOS for a while, and the developer recently updated it to support the new AppleTV.
Here's the code from the Objective-C example:
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[self presentViewController:playerViewController animated:YES completion:nil];
__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:playlistItem.snippet.resourceId.videoId completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
if (video)
{
NSDictionary *streamURLs = video.streamURLs;
NSURL *streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[#(XCDYouTubeVideoQualityHD720)] ?: streamURLs[#(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[#(XCDYouTubeVideoQualitySmall240)];
weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
[weakPlayerViewController.player play];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
Swift example here.
Sadly, even an Apple Staff person echoed the “negative” on this. From an Apple Developer Forums thread on the topic:
There is no support for WebViews on tvOS, and so an iframe implementation will not work. TVML does not offer this capability.
After some trying I'm happy to announce that:
youtube://watch/video_id
will open the YouTube app and play the YouTube video.
Enjoy.
As an addition to Daniel Storm's answer, you can also achieve this with the new TVML language using the code:
<lockup videoURL="http://mp4-url.mp4">
<img src="video thumbnail.jpg" />
</lockup>
(for this to work you'll need the direct video file (mp4) which also is not allowed by the Youtube TOS)
EDIT, found also the .JS solution:
How do I play a video on tvOS for Apple TV?
I used this one https://cocoapods.org/pods/youtube-parser
Youtube.h264videosWithYoutubeURL(testURL) { (videoInfo, error) -> Void in
if let videoURLString = videoInfo?["url"] as? String,
videoTitle = videoInfo?["title"] as? String {
print("\(videoTitle)")
print("\(videoURLString)")
self.playVideo(videoURLString)
}
}
I managed to load an audio file onto a player and play it via the following method:
- (void) play:(NSString*)loop{
NSString* resourcePath = [[NSBundle mainBundle] pathForResource:loop
ofType:#"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath]];
player.delegate = self;
[player play];
player.numberOfLoops = 1;
player.currentTime = 0;
player.volume = 1.0;
}
}
(I removed a couple of lines in this post but the method itself works great)
The problem here is, that the sound only gets loaded when the method play() is being called which results in a little delay in between the button click that calls the method and the player actually playing it.
I have about 200 sounds in my app and one way would be to load each sound in its own AVAudioPLayer in the viewDidLoad, but I doubt that this is considered good code.
On Android, I am using the SoundPool for this concern and it works great but I don't think there is an equivalent for this on iOS.
I now managed to do it via AudioServicesPlaySystemSound()
I use a method that loads all the sounds in a giant array
- (void)prepSounds{
NSString *soundPath =
[[NSBundle mainBundle] pathForResource:#"bl3"
ofType:#"mp3"];
NSURL *soundPathURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(
(__bridge CFURLRef)soundPathURL, &soundID[0]);
AudioServicesPlaySystemSound(soundID[0]);
}
where soundID[] is an array of SystemSoundID declared in the header.
Now, I can just easily fire the sound via AudioServicesPlaySystemSound(soundID[0]) where 0 relates to the first sound I loaded. The latency is close to 0 (I would guess 5ms)
However, this only works for sounds no longer than about 30 seconds.
If you need any assistance with that, just comment here, I'm glad to help.
I want to load video in AVPlayer using YouTube URL but it is not showing anything.Whenever i am loading from a local storage using NSBundle it is working fine.Is there is any alternative to load video or we can do something in AVPlayer.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryError];
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:#"http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player"]];
avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset];
self.songPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer: self.songPlayer];
self.avPlayerLayer.frame = self.view.layer.bounds;
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];
[ self.songPlayer play];
}
You should use the iOS Youtube Helper library for playing youtube videos.
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
I don't know if you can use the AVPlayer. I've seen some examples using MPMoviePlayerController on CocoaControls, like this one: https://www.cocoacontrols.com/controls/hcyoutubeparser or this one: https://www.cocoacontrols.com/controls/xcdyoutubevideoplayerviewcontroller
But I don't think using youtube's url directly in your player fits the ToS of the platform. So I will recommend you tu use the Youtube Helper Library if you are planning to publish your app.
Use XCDYouTubeKit pod
Get youtube video id from url like https://www.youtube.com/watch?v=dLEATulyCdw
you can use this code:
extension URL {
func youtubeVideoId() -> String? {
let pattern = #"(?<=(youtu\.be\/)|(v=)).+?(?=\?|\&|$)"#
let testString = absoluteString
if let matchRange = testString.range(of: pattern, options: .regularExpression) {
let subStr = testString[matchRange]
return String(subStr)
} else {
return .none
}
} }
Then call XCDYouTubeClient.default().getVideoWithIdentifier(videoId)
In the completion you can get url. video?.streamURLs contains urls with a different quality, choose desired.
Finally just pass this url to AVPlayer...
Update
Visual explanation
Use first instead of youtubeMaxAvailableQuality
I don't think if you can use this now because i just used this and encountered the same case.
I read the apple document-,it definitely refers to that (You cannot directly create an AVAsset instance to represent the media in an HTTP Live Stream.).
Instead here is apple's example:
NSURL *url = [NSURL URLWithString:#"<#Live stream URL#>];
// You may find a test stream at
http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
see,if not local url,should be playerItemWithURL:^_^