File structure
Application
//
#import "ViewController.h"
#import "VideoViewController.h"
#import "MovieViewController.h"
@interface Viewcontroller ()
{
To set a Class View controller as a global variable
Videoviewcontroller *_VIDEOVC;
Movieviewcontroller *_MOVIEVC;
}
-(Ibaction) Doplayaudiobutton: (ID) sender;
-(Ibaction) Doplaymoviebutton: (ID) sender;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
Click to enter the audio page
-(Ibaction) Doplayaudiobutton: (ID) Sender {
Determines whether the view controller exists and initializes if it does not exist.
if (!_VIDEOVC) {
_videovc=[[videoviewcontroller Alloc]init];
}
Set the _VIDEOVC control view to the page that will be entered
[Self.navigationcontroller PUSHVIEWCONTROLLER:_VIDEOVC animated:yes];//yes with animated effect
}
Click to enter the video page
-(Ibaction) Doplaymoviebutton: (ID) Sender {
if (!_MOVIEVC) {
_movievc=[[movieviewcontroller Alloc]init];
}
[Self.navigationcontroller PUSHVIEWCONTROLLER:_MOVIEVC Animated:yes];
}
@end
//
#import "ViewController.h"
#import "VideoViewController.h"
#import "MovieViewController.h"
@interface Viewcontroller ()
{
To set a Class View controller as a global variable
Videoviewcontroller *_VIDEOVC;
Movieviewcontroller *_MOVIEVC;
}
-(Ibaction) Doplayaudiobutton: (ID) sender;
-(Ibaction) Doplaymoviebutton: (ID) sender;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
Click to enter the audio page
-(Ibaction) Doplayaudiobutton: (ID) Sender {
Determines whether the view controller exists and initializes if it does not exist.
if (!_VIDEOVC) {
_videovc=[[videoviewcontroller Alloc]init];
}
Set the _VIDEOVC control view to the page that will be entered
[Self.navigationcontroller PUSHVIEWCONTROLLER:_VIDEOVC animated:yes];//yes with animated effect
}
Click to enter the video page
-(Ibaction) Doplaymoviebutton: (ID) Sender {
if (!_MOVIEVC) {
_movievc=[[movieviewcontroller Alloc]init];
}
[Self.navigationcontroller PUSHVIEWCONTROLLER:_MOVIEVC Animated:yes];
}
@end//
#import "ViewController.h"
#import "VideoViewController.h"
#import "MovieViewController.h"
@interface Viewcontroller ()
{
To set a Class View controller as a global variable
Videoviewcontroller *_VIDEOVC;
Movieviewcontroller *_MOVIEVC;
}
-(Ibaction) Doplayaudiobutton: (ID) sender;
-(Ibaction) Doplaymoviebutton: (ID) sender;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
Click to enter the audio page
-(Ibaction) Doplayaudiobutton: (ID) Sender {
Determines whether the view controller exists and initializes if it does not exist.
if (!_VIDEOVC) {
_videovc=[[videoviewcontroller Alloc]init];
}
Set the _VIDEOVC control view to the page that will be entered
[Self.navigationcontroller PUSHVIEWCONTROLLER:_VIDEOVC animated:yes];//yes with animated effect
}
Click to enter the video page
-(Ibaction) Doplaymoviebutton: (ID) Sender {
if (!_MOVIEVC) {
_movievc=[[movieviewcontroller Alloc]init];
}
[Self.navigationcontroller PUSHVIEWCONTROLLER:_MOVIEVC Animated:yes];
}
@end
Audio
//
#import "VideoViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface Videoviewcontroller ()
{
Avaudioplayer *_player;//playing the file class
}
/*
iOS supported audio formats: Mp3,aac,alac,ima4,linear (WMA?)
*/
@end
@implementation Videoviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Email protected] "audio";
Get Audio Path
NSString *musicpath=[[nsbundle mainbundle]pathforresource:@ "Simple Love" oftype:@ "MP3"];
Establish a Connection object (Nsurl do the class used for the link)
Nsurl *url=[[nsurl Alloc]initfileurlwithpath:musicpath];
Nserror *error=nil;
_player=[[avaudioplayer Alloc]
Initwithcontentsofurl:url error:&error];//and Initwithdata.
[_player preparetoplay];//ready to play (start loading the audio file to make sure the player starts playing as soon as possible)
if (Error) {
NSLog (@ "error message:%@," [Error localizeddescription]);
}
NSLog (@ "1");
}
-(void) Viewwillappear: (BOOL) animated{
NSLog (@ "2");
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
-(Ibaction) Doplaybutton: (ID) Sender {
[_player play];
}
-(Ibaction) Dopausebutton: (ID) Sender {
[_player pause];//Pause
[_player stop];//stop
/* Stops playback. No longer ready to play. */
_player.currenttime=0;//from the beginning.
/* Returns the current time associated with the output device */
}
-(Ibaction) Dogo5button: (ID) Sender {
if (_player.currenttime<=_player.duration-5) {
_player.currenttime+=5;//current time changed to five seconds
}
}
-(Ibaction) Doback5button: (ID) Sender {
if (_player.currenttime>=5) {
_player.currenttime-=5;
}
}
Volume
-(Ibaction) Dochangevoice: (UISlider *) Sender {
_player.volume=sender.value;
}
@end
Video
#import "MovieViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface Movieviewcontroller ()
{
Mpmovieplayerviewcontroller *_player;
}
@end
@implementation Movieviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Email protected] "video";
self.navigationcontroller.navigationbarhidden=yes;//Hide Navigation Bar
NSString *moviepath=[[nsbundle mainbundle]pathforresource:@ "Hulala" oftype:@ "mp4"];
Establish a Connection object (Nsurl do the class used for the link)
Nsurl *url=[[nsurl Alloc]initfileurlwithpath:moviepath];
Nserror *error=nil;
_player=[[mpmovieplayerviewcontroller Alloc]initwithcontenturl:url];
if (Error) {
NSLog (@ "error message:%@," [Error localizeddescription]);
}
[_player.movieplayer Setscalingmode:mpmoviescalingmodeaspectfit];
/*
Mpmoviescalingmodenone//Do not scale video
Mpmoviescalingmodeaspectfit,//Video Scaling 4:3
Mpmoviescalingmodeaspectfill,//Zoom to outer frame, 4:3 size hides part
Mpmoviescalingmodefill//video stretching loses original proportions
*/
[_player.movieplayer setcontrolstyle:mpmoviecontrolstylefullscreen];//Full Screen
/* Mpmoviecontrolstylenone,//No controls
mpmoviecontrolstyleembedded,//Controls for an embedded view
Mpmoviecontrolstylefullscreen,//Controls for fullscreen playback
Mpmoviecontrolstyledefault = mpmoviecontrolstyleembedded
*/
[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (finshmovieplay) Name: Mpmovieplayerplaybackdidfinishnotification Object:nil];
NSLog (@ "1");
}
-(void) Viewwillappear: (BOOL) animated{
[Self.view Addsubview:_player.view];
}
-(void) Finshmovieplay
{
Self.navigationcontroller.navigationbarhidden=no;
[Self.navigationcontroller Poptorootviewcontrolleranimated:yes];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
Audio and video playback