iOS Video Music playback

Source: Internet
Author: User
Tags uikit

iOS Development tips (video and music playback)1. iOS video playback code (add mediaplayer.framework and #import)-(void) Playmovie: (NSString *) filename{//Video File pathNSString *path = [[NSBundle mainbundle] pathforresource:filename ofType:@"mp4"]; //Video URLNsurl *url =[Nsurl Fileurlwithpath:path]; //Video Playback ObjectsMPMoviePlayerController *movie =[[MPMoviePlayerController alloc] initwithcontenturl:url]; Movie.controlstyle=Mpmoviecontrolstylefullscreen;    [Movie.view SetFrame:self.view.bounds]; Movie.initialplaybacktime= -1;    [Self.view AddSubview:movie.view]; //register a notification to end a play[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (mymoviefinishedcallback:) name:mpm OvieplayerplaybackdidfinishnotificationObject: movie];    [Movie play]; }        #pragmaMark-------------------the end of the video playback delegate---------------------(void) Mymoviefinishedcallback: (nsnotification*) Notify {//Video Playback Objectsmpmovieplayercontroller* Themovie = [notifyObject]; //Destroying playback notifications[[Nsnotificationcenter defaultcenter] removeobserver:self name:mpmovieplayerplaybackdidfinishnotification 
    Object: Themovie];    [Themovie.view Removefromsuperview]; //releasing a video object[Themovie release]; }2. Play background music, use the class to play the "background music playback supports the MP3 format to play music in a way that plays long music. Import Frames #import<AVFoundation/AVFoundation.h>; #import<UIKit/UIKit.h>{Avaudioplayer*Mybackmusic;}//the above steps are important and must be instantiated in the H file. Do not know why, directly in the M file instantiation, will not broadcast sound. The bottom is in the M file-(void) viewdidload{NSString*musicfilepath = [[NSBundle mainbundle] Pathforresource:@"Changan"OfType:@"mp3"];//Create a music file pathNsurl*musicurl =[[Nsurl alloc] initfileurlwithpath:musicfilepath]; Avaudioplayer*theplayer =[[Avaudioplayer alloc] Initwithcontentsofurl:musicurl Error:nil]; //Create playerMybackmusic= Theplayer;//assigning to a class variable of its own definition[Musicurl release];  [Theplayer release]; [Mybackmusic Preparetoplay]; [Mybackmusic SetVolume:1];//Set Volume sizeMybackmusic.numberofloops= -1;//set the number of music plays-1 to keep looping, set the Numberofloops property of the audio player to negative so that it plays an infinite loopNSLog (@"%f seconds played so far", audioplayer.currenttime);//View the initial time of playback, that is, how many seconds to start playingAudioplayer.currenttime=Ten;//Jump to the ten second Mark//set the time for playback to start[Mybackmusic play];//Play[Mybackmusic pause];  [Mybackmusic stop];} 3. iOS plays a sound (add audiotoolbox.framework and #import) "" is mainly used to play a sound, such as click on the sound, beat the second, in the sound method of playing. M method Add #import: #import接下来, The code to play the sound is as follows: NSString*path = [[NSBundle mainbundle] Pathforresource:@"Soundfilename"OfType:@"wav"]; Systemsoundid Soundid; Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path],&Soundid); Audioservicesplaysystemsound (Soundid); 1, get the global delegate object so that we can invoke the methods and variables in this object: [(Myappdelegate*) [[UIApplication sharedapplication]Delegate] mymethodormyvariable];2, obtaining the main bundle:nsbundle of the program*bundle =[NSBundle Mainbundle]; Bundles can be understood as a folder in which the content follows a specific framework. One of the main uses of the main bundle is the use of resource files, slices, sounds, plst files, etc. in the program. Nsurl*plisturl = [Bundle Urlforresource:@"Plistfile"Withextension:@"plist"]; The above code gets the path to the Plistfile.plist file. 3, play sound in the program: first add Audiotoolbox to the program: second, add #import: In the. m method that has the play sound method, #import接下来, the code that plays the sound is as follows: NSString*path = [[NSBundle mainbundle] Pathforresource:@"Soundfilename"OfType:@"wav"]; Systemsoundid Soundid; Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path],&Soundid); Audioservicesplaysystemsound (soundid);4, set and get property values in the class: [Self setValue: Variable value forkey: variable name]; [Self valueforkey: variable name];5to allow a method to execute after a certain period of time: [Self Performselector: @selector (method name) Withobject:nil Afterdelay: Delay time (s)];6, obtain the device version number:floatVersion =[[[ Uidevice Currentdevice] systemversion] floatvalue];7, capture program shutdown, or go to background event: UIApplication*app =[UIApplication sharedapplication]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (applicationwillresignactive:) Name: UiapplicationwillresignactivenotificationObject: app];applicationwillresignactive: Add the desired action in this method8, view the fonts supported by the device: for(NSString *familyinch[Uifont Familynames]) {NSLog (@"%@", family); for(NSString *fontinch[Uifont fontnamesforfamilyname:family]) {NSLog (@"\t%@", font);}}9, add a click event for Uiimageview: imageview.userinteractionenabled=YES; UITapGestureRecognizer*singletap =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (yourhandlingcode:)];[ ImageView Addgesturerecognizer:singletap];Ten, add multi-language support: such as image picker components, the text of the button above it changes with the device language environment, but first in the project to add language: One, enabling the program to support devices such as itunes, such as using a PC-side tool to drag and drop files to documents in the program.  A, page transition effect settings: Controller.modaltransitionstyle=uimodaltransitionstylecoververtical; [Self Presentmodalviewcontroller:controller animated:yes]; Available Effects: Uimodaltransitionstylecoververticaluimodaltransitionstylefliphorizontaluimodaltransitionstylecrossdissolveuimodal Transitionstylepartialcurl page before recovery: [Self Dismissmodalviewcontrolleranimated:yes];via devdiv

iOS Video Music playback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.