This article reprinted to http://mobile.51cto.com/iphone-423224.htm
The video format can be divided into two categories, which are suitable for local broadcast video and network streaming image video which is suitable for playing in the network. Although the latter may not be good in the quality of the playback stability and playback screen, but the wide spread of network streaming video image is widely used in video-on-demand, network demonstrations, distance education, network video advertising and other Internet Information services.
AD:2014WOT Global Software Technology Summit Beijing Station course video release
Play Video
Video File Introduction
The video format can be divided into two categories, which are suitable for local broadcast video and network streaming image video which is suitable for playing in the network. Although the latter may not be good in the quality of the playback stability and playback screen, but the wide spread of network streaming video image is widely used in video-on-demand, network demonstrations, distance education, network video advertising and other Internet Information services.
Video files for mobile devices
3GP,3GP is a 3G streaming video encoding format, mainly in order to match the high transmission speed of 3G network developed, but also the most common in the mobile phone video format. Video MP4 format, in addition to support the MP3 has the music playback function, but also has a powerful MPEG-4 video playback ability.
mov format files are also supported in the iphone.
iOS play Video
The IOS SDK provides a very easy way to play video, providing the Mpmovieplayerviewcontroller class as a development use, before the iOS4 version is MPMoviePlayerController.
It is forbidden to use the private API to play video in the iphone development specification, so the control of the playback screen is available on the iphone, and we have no other choice. What we can do:
Load the video in the URL
Play, pause video
User control behavior and zoom mode
Generate notifications
Video playback Cases
Add Mediaplayer.framework
MoviePlayerViewController.h
- #import <MediaPlayer/MediaPlayer.h>
- @interface Movieplayerviewcontroller:uiviewcontroller {
- Mpmovieplayerviewcontroller * Movieplayerview;
- }
- @property (nonatomic, retain) Mpmovieplayerviewcontroller * Movieplayerview;
- -(Ibaction) Playmovie: (ID) sender;
- -(void) Playingdone;
- @end
How to load and unload m files
- -(void) Viewdidload {
- [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (playingdone) Name: Mpmovieplayerplaybackdidfinishnotification Object:nil];
- }
- -(void) Dealloc {
- [[Nsnotificationcenter Defaultcenter] removeobserver:self];
- [Movieplayerview release];
- [Super Dealloc];
- }
Mpmovieplayerviewcontroller provides notification of state changes and other events during playback. In Viewdidload registered a playback complete notification, the commonly used notifications are:
Mpmovieplayerplaybackdidfinishnotification notifies the receiver that the playback is complete.
Mpmovieplayerscalingmodedidchangenotification change the size of the movie.
Mpmovieplayercontentpreloaddidfinishnotification indicates preprocessing and completion, ready to start playing the movie.
[[Nsnotificationcenter Defaultcenter] in the Dealloc method
Removeobserver:self]; movie playback complete to unregister the notification.
Play Events
- -(Ibaction) Playmovie: (ID) Sender {
- Movieplayerview = [[Mpmovieplayerviewcontroller alloc]
- Initwithcontenturl:[nsurl Fileurlwithpath:[[nsbundle Mainbundle]
- pathforresource:@"short" oftype:@"3gp"];
- MoviePlayerView.moviePlayer.controlStyle = Mpmoviecontrolstylefullscreen;
- MoviePlayerView.moviePlayer.scalingMode = Mpmoviescalingmodeaspectfit;
- //Mpmoviecontrolstylenone
- //mpmoviecontrolstyleembedded
- //mpmoviecontrolstyledefault
- //[movieplayer Play];
- //view to add a video on the current view
- [[[UIApplication sharedapplication] Keywindow] addSubview:moviePlayerView.view];
- }
Video files can play resource catalogs, sandbox catalogs, and network playback. In this example we use the Resource directory.
The Movieplayerview.movieplayer property is a MPMoviePlayerController type, and it has controlstyle properties
You can control the playback behavior, which has the following values:
Mpmoviecontrolstylefullscreen
Mpmoviecontrolstylenone No playback controls
mpmoviecontrolstyleembedded
Mpmoviecontrolstyledefault
The MPMoviePlayerController class also has the Scalingmode property to control the size of the movie, which takes the following values:
Mpmoviescalingmodenone Original Size
Mpmoviescalingmodeaspectfit Zoom to a fill direction
Mpmoviescalingmodeaspectfill may be partially removed on both sides of the fill
Mpmoviescalingmodefill fills may change proportions on both sides
Play complete
- -(void) Playingdone {
- NSLog (@"play complete");
- [Movieplayerview.view Removefromsuperview];
- [Movieplayerview release];
- Movieplayerview = nil;
- }
The Playingdone method is called when the movie playback is complete, because we register the method in the notification center.
Playback must be done to remove the playback view so that the previous screen can be obtained.
12.2 Playing Audio
12.2.1 Audio File Introduction
There are two main types of audio file formats:
Lossless formats, such as Wav,pcm,tta,flac,au,ape,tak,wavpack (WV), CAF
Lossy formats, such as Mp3,windows Media Audio (WMA), Ogg Vorbis (OGG), AAC
Moving audio files
As a mobile device audio file should be in principle relatively small, general format:
WAV, because lossless compression effect is best.
MP3, lossy compression, the file is relatively small, because the removal of the human can not sense the sound, the effect is very good. This is the current common format.
AAC, compression ratio is larger, smaller than the MP3 file.
The CAF (Core Audio format) is an apple-only lossless compression format.
12.2.2 Core Audio
Advanced API, easy to use
The System sound API – plays short sounds, warning tones, and so on.
Avfoundation can play long-time sound, easy to use.
Low-level API that gives you more control over your audio
Audio toolbox– is fully controlled for recording, playback and streaming.
openal– plays stereo, often used in games.
12.2.3 System Sound API
The System sound can play "short" sounds, and the so-called short sound is within 5 seconds. No loop, no sound control, instant playback.
Playback format Restrictions:
Linear PCM and IMA4
. CAF. AIF or. wav
Play "Short Sound"
Playing "short sound" is primarily a two-step process:
Register Sound
- Audioservicescreatesystemsoundid ((cfurlref) FileURL, &myid);
Play sound
- Audioservicesplaysystemsound (MyID);
Listener Completion Event Method
- Audioservicesaddsystemsoundcompletion
Clear Play sound ID
- Systemsoundid MyID;
- Audioservicesdisposesystemsoundid (MyID);
Vibration
You can also vibrate your iphone with the system sound API, but ipod touch doesn't vibrate.
Vibrations can be achieved by specifying a special system sound id--ksystemsoundid_vibrate.
Audioservicesplaysystemsound (ksystemsoundid_vibrate);
iOS Development Multimedia API (1)