1. Local Audio playback
2. Local video playback
Local audio playback
Import Header File
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
Import "ViewController.h"
@interface Viewcontroller ()
Avaudioplayer to be a global variable to play
@property (strong,nonatomic) Avaudioplayer *audioplayer;
@end
@implementation Viewcontroller
@synthesize Audioplayer=_audioplayer;
-(void) Viewdidload {
[Super Viewdidload];
[Self.view Setbackgroundcolor:[uicolor Whitecolor];
[Self.navigationitem settitle:@ "audio"];
//Play audio; Note: If you click Stop, then make sure the player is newly created, otherwise there will be some inexplicable problems
[Self.getaudioplayer play];
}
/**
* Create an audio player
*return Audio Player
*/
-(Avaudioplayer *) getaudioplayer{
NSString *path=[[nsbundle Mainbundle] pathforresource:@ "Too Late for Love" oftype:@ "MP3"];
Nsurl *url=[nsurl Fileurlwithpath:path];
//Create a player
_audioplayer=[[avaudioplayer alloc] Initwithcontentsofurl:url Error:nil];
//volume 0.0-1.0 between
_audioplayer.volume=0.2;
//Cycle times
_audioplayer.numberofloops=1;
//Playback position
_audioplayer.currenttime=0.0;
//channel number
Nsuinteger channels=_audioplayer.numberofchannels;//Read-only property
//Duration
Nstimeinterval duration=_audioplayer.duration;//Get Duration
//Assign the required resources to play and add them to the internal play queue
[_audioplayer Preparetoplay];
return _audioplayer;
}
@end
Local video playback
Import Header File
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
Import "ViewController.h"
@interface Viewcontroller ()
Video Playback Controller
@property (strong,nonatomic) MPMoviePlayerController *movieplayer;
@end
@implementation Viewcontroller
@synthesize Movieplayer=_movieplayer;
-(void) Viewdidload {
[Super Viewdidload];
[Self.view Setbackgroundcolor:[uicolor Whitecolor];
[Self.navigationitem settitle:@ "video"];
//Play Video
[Self.getmovieplayer play];
}
/**
* Create a video controller
*return Video Controller
*/
-(MPMoviePlayerController *) getmovieplayer{
NSString *path=[[nsbundle Mainbundle] pathforresource:@ "DotA2 official propaganda film" oftype:@ "mp4"];
Nsurl *url=[nsurl Fileurlwithpath:path];
_movieplayer=[[mpmovieplayercontroller alloc] Initwithcontenturl:url];
_movieplayer.view.frame=self.view.frame;
//Automatic adjustment of the length and width
_movieplayer.view.autoresizingmask=uiviewautoresizingflexiblewidth| Uiviewautoresizingflexibleheight;
[Self.view Addsubview:_movieplayer.view];
return _movieplayer;
}
IOS, multimedia related