Play Multiple Videos from one page - ios

Ok, so I am creating a basic iPad app that needs to play 4 videos. Videos need to be accessed from the same page.
I have one page/landing screen with an image that I have placed 4 Round Rec Buttons on. The MediaPlayer/MediaPlayer.h framework works perfectly, exactly what I want. But right now all four buttons play the same video, mainly because there is only one set of code but that is my problem. I assumed that repeating the code would allow me to replace the pathForResource:#"Beginning_Bumper" ofType:#"m4v" section of code but I get re-implimentation errors and the build fails. I've tried looking for other apps or tutorials online with no results. Also thought this question was my answer, but nothing there.
Here is the code I have, without duplication. So this code just plays one of the videos. Hopefully all of that makes sense, but feel free to ask more questions.
#import "Video_Presentation1ViewController.h"
#implementation Video_Presentation1ViewController
-(IBAction)playvideo {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Beginning_Bumper" ofType:#"m4v"]];
MPMoviePlayerViewController * playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
[playerController release];playerController=nil;
}

Related

Playing chunck of bytes into MPMoviePlayerController on iPhone

My application is actually downloading a video I can easily read with MPMoviePlayerController. But now I have my video split and so on a segment form (meaning I receive a segment.init and then some other segments containing the video such as video_mp4_init.segment, video_mp4_0000.segment, video_mp4_0001.segment etc) in my bundle.
What I am looking for is some help about the way I need to read the bytes. So far I'm reading the same way I read a video:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"tos_mp4_media_0000" ofType:#"segment"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
A black screen launches then quit. I know MPMoviePlayer does not read .segment and I do guess this is not the way it should be done but I run out of ideas.
I'd find it a bit odd if MPMoviePlayer does not allow the reading of bytes but it might also be a problem. I can swap to another video reader if needed.
Any idea is welcome.
Edited for futher informations.

Objective-C: Making AVPlayer plays in all the app controllers (or in the background)

I'm making a news iOS app, i want to implement a live audio streaming in all the pages like the picture below:
The orange view is a UIView that says "Live Streaming" (in Arabic), it exists in all the viewControllers, i want to let the AVPlayer plays when the user clicks on it and still plays even if the user navigates to other viewControllers.
I can't seem to find a good solution or tutorial about this, sorry if this is a noob question cause i'm new to iOS.
So how can i do that ? and where(in which viewController) should i put the AVPlayer declaration ?
Thanks in advance.
If you are testing in the simulator, the change of views will not continue the audio in the background. If it is not working your device try this:
NSURL *url = [NSURL URLWithString: yourURLHere];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
//This enables background music playing
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
Place this code in your viewDidLoad method, before [super viewDidLoad];
I found two links that exaplin some of the issues on this:
Play Music in Background
Play Audio from Internet
If this does not work, I would recommend placing a bounty on the question as it seems your issue's are a little more in depth than a simple solution.
I found the solution, i had to make a singleton class.
The singleton class allow only one object to be used in the entire iOS application by all the ViewControllers.
And i did put all the needed methods like play and pause in the singleton class and i accessed them from all the ViewControllers.
If anybody wants an example, please ask in the comments.

Objective-C MPMoviePlayerController iOS7 issue

I've seen this mentioned a few places around the web but have yet to find a concrete answer anywhere.
I'm trying to update an app so that it runs properly on iOS7. Part of that involves running a full-screen .mp4 video file (15.4mb / 40 Seconds long). Here is the code i use to setup the video, which runs fine in iOS6:
videoPlayer= [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource: [NSString stringWithFormat:#"introIpad"] ofType:#"mp4"]]];
videoPlayer.fullscreen = YES;
videoPlayer.movieSourceType = MPMovieSourceTypeFile;
videoPlayer.view.frame = self.view.bounds;
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer];
As mentioned above this code ran perfectly on iOS6, however on iOS7 it now gives me the following error log:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I've seen a few people saying that all they had to do is change the name of the video files so i've tried that with no luck. I also found some people mention the movieSourceType being a problem but i've tried to set it to "MPMovieSourceTypeStreaming" and that didn't work either.
Bit of a frustrating one and any help someone can give me would be greatly appreciated!
Thanks in advance.
So it seems that this issue is a generic response when the device cannot play the video file.
I've tried with different encodings and multiple types of video without much luck.
However I've just tried a video with a different dimension and it plays, my original video file was 1536x2048 (iPad Retina resolution) and it doesn't like it. I've re-rendered the video out at 768x1024 (Half the original size) and it works perfectly.
It's not a very useful error log but i guess if you have the same problem as me then try as many different types of video and find the one that works.

Sounds playing over each other using AVAudioPlayer

Right, sorry if i seem like a noob, but i'm new to xcode. I'm currently making a soundboard, and i've got all the buttons to work and play sounds from them.
However, if one sound is playing, when i press another button, rather than stopping the original sound, it just plays over it, so i was wandering if anyone knew how to fix this?
Here's the code i'm using for the buttons:
- (IBAction)playsound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Gold" ofType:#"mp3"];
AVAudioPlayer* myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
myAudio.delegate = self;
myAudio.volume = 2.0;
myAudio.numberOfLoops = 0;
[myAudio play];
}
I'd be really grateful if someone could help me out.
Thanks.
If the other sound is still playing, you need to tell it to stop. You probably want to look into using the calling the stop method on the other AVAudioPlayer. So, something like where you have:
[myAudio play];
...insert a method call just before that to tell the other sound to stop.
[myOtherAudioThatsPlaying stop];
[myAudio play];
It might make sense to also browse the AVAudioPlayer documentation.
While bobtiki's answer is formally correct, it only makes sense to call the stop method if you want to do something useful with the sound after it has stopped, for example start playing it again. Since you just want to get rid of it, I recommend have the player just get deallocated, which automatically makes the sound stop playing.
So, instead of
AVAudioPlayer* myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
add an instance variable to your class:
AVAudioPlayer* myAudio;
then just use
myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
Everytime a new sound is played, myAudio gets assigned a new pointer and the old AVAudioPlayer just gets deallocated.

Custom UIView to display video

Hey All,
Im working on an app for the iPad and Ive run across an issue that I need some guidance on. I have an app that uses the TabBarController. The TabBarController contains 4 UIViewController...one for each screen in the app. On each of these 4 screens, there are 4 tiles that need to act as buttons and play a video when clicked. I would like to play the video in a view that hovers over the rest of the screen and is dismissed if the user touches anywhere outside the playing video.
My question is: How do I go about implementing the custom view to play the video? It seems to me that it should be just another view with a viewController...but I dont know if that is the correct way to go about this. Also, how do I get it to play the right video depending on what button was clicked?
Any advice is greatly appreciated.
Thanks
Alex
I'm not sure if I entirely get what the question is, if my answer is not enough please be more precise.
first you initialize your video player view controller class MPMoviePlayerController, then you can do whatever you want with it's .view property, or the container view to display your 4 buttons.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"someMovie" ofType:#"m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player.view.frame = CGRectMake(someX, someY, someWidth, someHeight);
[self.view addSubview:player.view];
[player play];

Resources