• 热门专题

ios学习笔记音频视频

作者:  发布日期:2014-02-19 20:29:21
Tag标签:ios  学习笔记  音频视频  
  • 1、声音播放

    声音播放
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"win" 
                                                         ofType:@"wav"];
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                         , &soundID);
        AudioServicesPlaySystemSound (soundID);
    


    2、音频播放+震动

    1.	#import <UIKit/UIKit.h>
    2.	#include <AudioToolbox/AudioToolbox.h>
    3.	
    4.	@interface PlaySoundDemo1ViewController : UIViewController {
    5.	    IBOutlet UISwitch *swcallback;
    6.	    IBOutlet UIPickerView *soundPicker;
    7.	    NSArray *soundData;
    8.	        SystemSoundID        soundFileObject;   
    9.	}
    10.	
    11.	@property (nonatomic,retain) UISwitch *swcallback;
    12.	@property (nonatomic,retain) UIPickerView *soundPicker;
    13.	@property (nonatomic,retain)  NSArray *soundData;
    14.	@property (readonly)        SystemSoundID        soundFileObject;
    15.	
    16.	static void completionCallback (SystemSoundID  mySSID, void* myself) ;
    17.	- (IBAction) playSystemSound;
    18.	- (IBAction) playAlertSound;
    19.	- (IBAction) vibrate;
    20.	-(IBAction) stopplaysound;
    21.	-(void) GetPlaysound;
    22.	
    23.	@end
    複製代碼
    PlaySoundDemo1ViewController.m
    1.	//----------- 限制 ------------
    2.	//聲音長度要小於 30 秒
    3.	// In linear PCM 或者 IMA4 (IMA/ADPCM) 格式的
    4.	//需為 .caf, .aif, 或者 .wav 的檔案
    5.	//不能控制播放的進度
    6.	//調用方法後立即播放聲音
    7.	//沒有循環播放和立體聲控制
    8.	//參考資料:
    9.	//http://blog.xiang.li/2009/02/iphone-dev-audio-play/
    10.	//https://developer.apple.com/iphone/library/samplecode/SysSound/
    11.	
    12.	#import "PlaySoundDemo1ViewController.h"
    13.	
    14.	@implementation PlaySoundDemo1ViewController
    15.	
    16.	@synthesize swcallback,soundPicker,soundData,soundFileObject;
    17.	
    18.	//停止當前音效的播放
    19.	-(IBAction) stopplaysound{    
    20.	    AudioServicesRemoveSystemSoundCompletion (self.soundFileObject);
    21.	}
    22.	
    23.	//建立聲音物件
    24.	-(void) GetPlaysound{
    25.	    [self stopplaysound];
    26.	    
    27.	   //取出使用者所選擇的音效項目 
    28.	   NSInteger row=[ soundPicker selectedRowInComponent:0];
    29.	   
    30.	    //指定不同的音效檔
    31.	    NSString *soundfilename;
    32.	    switch (row) {
    33.	        case 0:
    34.	             soundfilename=@"sound1.aiff"; 
    35.	            break;
    36.	        case 1:
    37.	             soundfilename=@"sound2.aiff"; 
    38.	            break;
    39.	        case 2:
    40.	             soundfilename=@"sound3.aiff"; 
    41.	            break;            
    42.	        default:
    43.	            break;
    44.	    }
    45.	
    46.	   
    47.	    NSString *Path=[[NSBundle mainBundle] bundlePath];
    48.	    NSURL *soundfileURL=[NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]]; 
    49.	    
    50.	    //建立音效物件
    51.	    AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject);
    52.	    
    53.	    //判斷是否連續播放
    54.	    if ([swcallback isOn]){
    55.	        // Add sound completion callback
    56.	        AudioServicesAddSystemSoundCompletion (soundFileObject, NULL, NULL,completionCallback,(void*) self);    
    57.	    }    
    58.	}
    59.	
    60.	//當音效播放完畢後的處理方式,這裡設定為再一次播放
    61.	static void completionCallback (SystemSoundID  mySSID, void* myself) {
    62.	    AudioServicesPlaySystemSound(mySSID);    
    63.	} 
    64.	
    65.	- (IBAction) playSystemSound{     
    66.	     [self GetPlaysound];
    67.	        AudioServicesPlaySystemSound (self.soundFileObject);    
    68.	  //  AudioServicesDisposeSystemSoundID (self.soundFileObject);
    69.	}
    70.	
    71.	-(IBAction) playAlertSound{   
    72.	     [self GetPlaysound];
    73.	    //播放音效
    74.	    AudioServicesPlayAlertSound (self.soundFileObject);    
    75.	}
    76.	- (IBAction) vibrate{
    77.	    //震動
    78.	        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    79.	}
    80.	
    81.	- (void)viewDidLoad {
    82.	    [super viewDidLoad];
    83.	    
    84.	    //建立音效清單陣列
    85.	    NSArray *array=[[NSArray alloc] initWithObjects:@"音效1",@"音效2",@"音效3",nil];
    86.	    self.soundData=array;
    87.	    [array release];
    88.	}
    89.	- (void)dealloc {
    90.	    [super dealloc];
    91.	    AudioServicesDisposeSystemSoundID (self.soundFileObject);
    92.	}
    93.	#pragma mark -
    94.	#pragma mark soundPicker Data Soure 
    95.	- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    96.	    return 1;
    97.	}
    98.	- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    99.	    return [soundData count];
    100.	  }
    101.	#pragma mark -
    102.	#pragma mark soundPicker Delegate 
    103.	- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    104.	    return [soundData objectAtIndex:row];
    105.	}
    106.	
    107.	AudioToolbox.frameworkAudioToolbox.framework
    


    3、MP3 播放

    1)、背景音乐播放    支持mp3格式 循环播放长音乐
    
    这种播放音乐的方式导入框架#import <AVFoundation/AVFoundation.h>;
    
    NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"];       //创建音乐文件路径
      NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];  
    
       AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
    
    // 创建播放器
      self.myBackMusic = thePlayer;    //赋值给自己定义的类变量
      
      [musicURL release];
      [thePlayer release];
      
      [myBackMusic prepareToPlay];
      [myBackMusic setVolume:1];   //设置音量大小
      myBackMusic.numberOfLoops = -1;//设置音乐播放次数  -1为一直循环
      if (mainMusicStatus) 
      {
       [myBackMusic play];   //播放
      }
    
    
      2)、按钮播放声音
    需要导入框架#import <AudioToolbox/AudioToolbox.h>   
    
    NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"];    //创建音乐文件路径
    CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];
    AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID); 
    
    //变量SoundID与URL对应
    
    AudioServicesPlaySystemSound(sameViewSoundID);  // 播放SoundID声音
    


    4、视频播放

    视频播放
    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    @interface MoveTestViewController : UIViewController {
        NSURL *movieURL;
    	MPMoviePlayerController *moviePlayer;
    }
    @property(nonatomic,retain)NSURL *movieURL;
    @property(nonatomic,retain)MPMoviePlayerController *moviePlayer;
    -(void)initAndPlayMovie:(NSURL *)movieURL;
    -(NSURL *)localMovieURL;
    -(IBAction)ok;
    @end
    
    //
    //  MoveTestViewController.m
    //  MoveTest
    //
    //  Created by zhen hu on 10/20/10.
    //  Copyright ww 2010. All rights reserved.
    //
    
    #import "MoveTestViewController.h"
    
    @implementation MoveTestViewController
    @synthesize movieURL;
    @synthesize moviePlayer;
    -(IBAction)ok{
    	//获得工程中电影文件的路径
    	NSURL *url=[self localMovieURL];
    	//播放
    	[self initAndPlayMovie:url];
    }
    -(void)initAndPlayMovie:(NSURL *)movUrl
    {
    	// 创建播放器
    	MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movUrl];
    	if (mp)//如果不为空
    	{
    		self.moviePlayer = mp;
    		[mp release];
    		[self.moviePlayer play];//播放对象
    		
    		//4.0专用
    		self.moviePlayer.view.frame = CGRectMake(0, 0, 320, 300);
    		[self.view addSubview:moviePlayer.view];
    	}
    }
    
    
    //-(void)initAndPlayNetMovie:(NSURL *)movieURL
    //{	
    //	
    //	MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    //	if (mp)
    //	{
    //		self.musicPlayer=mp;
    //		[mp release];		
    //		
    //		musicPlayer.view.frame = CGRectMake(0, 340, 320, 5);
    //		[self.view addSubview:musicPlayer.view];
    // 
    //	}
    //	
    //}
    
    -(NSURL *)localMovieURL
    {
        if (movieURL == nil)
        {
            NSBundle *bundle = [NSBundle mainBundle];
            if (bundle) 
            {//得到文件绝对路径地址
                NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
                //NSString *netPath=@"http://www.sina.com.cn/xx.m4v";
    			if (moviePath)
                {// 把路径转换为NSURL对象
                    self.movieURL = [NSURL fileURLWithPath:moviePath];
                }
            }
        }
        
        return self.movieURL;
    }
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    


About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规