1.IOS視頻播放代碼(添加MediaPlayer.framework和#import)
? | ? |
? | -( void )playMovie:(NSString *)fileName{ |
? | //視頻文件路徑 |
? | NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@ "mp4" ]; |
? | //視頻URL |
? | NSURL *url = [NSURL fileURLWithPath:path]; |
? | //視頻播放對象 |
? | MPMoviePlayerController?*movie = [[MPMoviePlayerController alloc] initWithContentURL:url]; |
? | movie.controlStyle = MPMovieControlStyleFullscreen; |
? | [movie.view setFrame:self.view.bounds]; |
? | movie.initialPlaybackTime = -1; |
? | [self.view addSubview:movie.view]; |
? | // 注冊一個播放結束的通知 |
? | [[NSNotificationCenter defaultCenter] addObserver:self |
? | selector:@selector(myMovieFinishedCallback:) |
? | name:MPMoviePlayerPlaybackDidFinishNotification |
? | object:movie]; |
? | [movie play]; |
? | } |
? | ? |
? | #pragma mark -------------------視頻播放結束委托-------------------- |
? | ? |
? | ? |
? | -( void )myMovieFinishedCallback:(NSNotification*)notify |
? | { |
? | //視頻播放對象 |
? | MPMoviePlayerController* theMovie = [notify object]; |
? | //銷毀播放通知 |
? | [[NSNotificationCenter defaultCenter] removeObserver:self |
? | name:MPMoviePlayerPlaybackDidFinishNotification |
? | object:theMovie]; |
? | [theMovie.view removeFromSuperview]; |
? | // 釋放視頻對象 |
? | [theMovie release]; |
? | } |
《《《背景音樂播放 支持mp3格式 循環播放長音樂
這種播放音樂的方式導入框架
#import <AVFoundation/AVFoundation.h>;
#import <UIKit/UIKit.h>
{
????AVAudioPlayer *myBackMusic;
}
//上邊的步驟很重要,必須在h文件中實例化。不知道為什么,直接在M文件中實例化,會播不出來聲音。
下邊是M文件中的
-(void)viewDidLoad
{
?NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //創建音樂文件路徑?
?NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
?
?AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
??
??//創建播放器?
?myBackMusic = thePlayer; //賦值給自己定義的類變量
?[musicURL release];
?[thePlayer release];
?
?[myBackMusic prepareToPlay];
?[myBackMusic setVolume:1]; //設置音量大小
?myBackMusic.numberOfLoops = -1;//設置音樂播放次數 -1為一直循環 ,將音頻播放器的numberOfLoops屬性設為負數使得播放無限循環
?NSLog(@"%f seconds played so far", audioPlayer.currentTime); //查看播放的初始時間,也就是從多少秒開始播放
?audioPlayer.currentTime?=?10; // jump to the 10 second mark //設置播放開始的時間
?[myBackMusic play]; //播放
?[myBackMusic pause];
?[myBackMusic stop];
}
?
?
3.IOS播放一段聲音(添加AudioToolbox.framework和#import)
《《《主要用來播放一段聲音,比如點擊的聲音,敲打
其次,在有播放聲音方法的.m方法添加#import:
#import
接下來,播放聲音的代碼如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
?
1、獲取全局的Delegate對象,這樣我們可以調用這個對象里的方法和變量:
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];
2、獲得程序的主Bundle:
NSBundle *bundle = [NSBundle mainBundle];
Bundle可以理解成一種文件夾,其內容遵循特定的框架。
Main Bundle一種主要用途是使用程序中的資源文件,如圖片、聲音、plst文件等。
NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];
上面的代碼獲得plistFile.plist文件的路徑。
3、在程序中播放聲音:
首先在程序添加AudioToolbox:
其次,在有播放聲音方法的.m方法添加#import:
#import
接下來,播放聲音的代碼如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
4、設置和獲取類中屬性值:
[self setValue: 變量值 forKey: 變量名];
[self valueForKey: 變量名];
5、讓某一方法在未來某段時間之后執行:
[self performSelector:@selector(方法名) withObject:nil afterDelay:延遲時間(s)];
6、獲得設備版本號:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
7、捕捉程序關閉或者進入后臺事件:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
applicationWillResignActive:這個方法中添加想要的操作
8、查看設備支持的字體:
for (NSString *family in [UIFont familyNames]) {
NSLog(@"%@", family);
for (NSString *font in [UIFont fontNamesForFamilyName:family]) {
NSLog(@"\t%@", font);
}
}
9、為UIImageView添加單擊事件:
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
[imageView addGestureRecognizer:singleTap];
10、添加多語言支持: 比如Image Picker這樣的組件,它上面的按鈕的文字是隨著設備語言環境的改變而改變的,但是要先在工程添加語言:
11、使程序支持iTunes這樣的設備,比如可以使用PC端的工具往程序的Documents中拖放文件。
12、頁面切換效果設置:
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
可供使用的效果:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
恢復之前的頁面:
[self dismissModalViewControllerAnimated:YES];
via?Devdiv