MediaPlayer Framework
To play a movie file:
You can use MPMoviePlayerController to play movie files in the IOS SDK. However, there are strict formatting requirements for playing movie files on iOS devices, and only movie files in the following two formats can be played.
? H. 3.0 (Baseline profile)
? MPEG-4 Part 2 Video
Fortunately, you can use itunes to convert your files to the top two formats first.
MPMoviePlayerController can also play video files on the Internet. However, it is recommended that you download the video file locally and then play it. If you do not do this, iOS may refuse to play a very large video file.
This class is defined in the MediaPlayer framework. In your application, add this reference first, and then modify the Mediaplayerviewcontroller. H file.
#import <</span>MediaPlayer/MediaPlayer.h>
@interface Mediaplayerviewcontroller:uiviewcontroller <</span>avaudioplayerdelegate>
{
MPMoviePlayerController *movieplayer;
Below we use this class to play a video file in. m4v format. Similar to the previous one, a URL path is required.
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
- (ID) init
{
Self = [super Initwithnibname:@ "mediaplayerviewcontroller" Bundle:nil];
if (self) {
NSString *moviepath = [[NSBundle Mainbundle] Pathforresource: @" Layers "
OfType:@ "m4v"];
if (Moviepath) {
Nsurl *movieurl = [Nsurl Fileurlwithpath:moviepath];
MoviePlayer = [[MPMoviePlayerController alloc]
Initwithcontenturl:movieurl];
}
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
MPMoviePlayerController has a view to show the player controls, and we'll show this player in the Viewdidload method.
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
- (void) viewdidload
{
[Self view] addsubview:[movieplayer view];
float Halfheight = [self view] bounds].size.height / 2.0;
float width = [self view] bounds].size.width;
[[MoviePlayer View] Setframe:cgrectmake (0, halfheight, Width, halfheight)];
}
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
There is also a Mpmovieplayerviewcontroller class for playing video files in full-screen, with the same usage as MPMoviePlayerController.
Mpmovieplayerviewcontroller * Playerviewcontroller =
[[Mpmovieplayerviewcontroller alloc] initwithcontenturl:movieurl];
[Viewcontroller Presentmovieplayerviewcontrolleranimated:playerviewcontroller];
When we listen to music, we can do other things with the iphone, this time we need the player to run in the background, we just need to make a simple setup in the application.
1, add a Required background modes node in the Info property list, it is an array, set the first item to set the app plays audio.
2. Add the following code to the code that plays MP3:
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
if (Musicpath) {
Nsurl *musicurl = [Nsurl Fileurlwithpath:musicpath];
[[Avaudiosession Sharedinstance]
Setcategory:avaudiosessioncategoryplayback Error:nil];
Audioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl
Error:nil];
[Audioplayer setdelegate:self];
}
Video Development "alt=" copy Code "src=" Http://common.cnblogs.com/images/copycode.gif ">
The ability to play music in the background is not visible in the simulator, only on the real machine.
From http://blog.sina.com.cn/s/blog_74e9d98d0101ar8e.html thanks to bloggers for sharing
RELATED Links: http://blog.sina.com.cn/s/blog_6f1a34260100tlvx.html