iOS学习笔记(三十一)————文件显示与音频视频播放

/ 0评 / 0

加载本地的文件,像本地html文件也是这样加载的,使用webView举例~

   UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    webView.scalesPageToFit = YES;
    [self.view addSubview:webView];
    
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"surfing-friends" ofType:@"jpg"];
    
    // 加载本地文件
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    
    // 加载网络文件
    //    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"网络地址"]]];

播放音频视频

- (void)showVideo{
    // 音频播放。支持AAC,AMR,ALAC,iLBC,IMA4,PCM,MP3
    
    // 播放本地音频
    NSString *localAudioPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
    // 创建一个本地的url
    NSURL *localAudioURL = [NSURL fileURLWithPath:localAudioPath];
    _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:localAudioURL error:nil];
    
    
    // 播放网络音频
    //    NSURL *netAudioURL = [NSURL URLWithString:@"http://127.0.0.1:8080/st/publicfile/baitianbudongyedehei.mp3"];
    //    _audioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfURL:netAudioURL] error:nil];
    
    
    _audioPlayer.delegate = self;
    _audioPlayer.volume = 1.0;
    //音频总长度
    NSLog(@"duration....%g",_audioPlayer.duration);
    
    
    /*******************************************************/
    
    // 视频播放 :支持MOV,MP4,MPV,3gp
    
    // 本地视频的URL
    NSString *localMeidaPath = [[NSBundle mainBundle] pathForResource:@"Movie1" ofType:@"m4v"];
    _mediaPlayURL = [NSURL fileURLWithPath:localMeidaPath];
    
    
    // 网络视频的URL
    //    _mediaPlayURL = [[NSURL URLWithString:@"http://127.0.0.1:8080/st/personalfile/crzs3.mp4"] retain];
    
    
    // 监听视频播放完
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}


// 音频播放完毕的协议方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"音乐播放完毕");
}

//  音频 播放
- (IBAction)startPlayer:(id)sender {
     [_audioPlayer play];
}

// 音频 暂停
- (IBAction)pausePlayer:(id)sender {
    [_audioPlayer pause];
}

/*******************************************************/

// 视频 全屏播放
- (IBAction)fullScreen:(id)sender {
    MPMoviePlayerViewController *viewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:_mediaPlayURL];
    
    // 通过子类去播放,子类可以控制界面的方向
    //    DCPlayerViewController *viewPlayer = [[DCPlayerViewController alloc] initWithContentURL:_mediaPlayURL];
    
    [self presentMoviePlayerViewControllerAnimated:viewPlayer];
}

// 视频 小窗口播放
- (IBAction)smallScreen:(id)sender {
    if (!_smallPlayer){
        _smallPlayer = [[MPMoviePlayerController alloc] initWithContentURL:_mediaPlayURL];
    }
    _smallPlayer.view.frame = CGRectMake(0, 50, 300, 200);
    
    [_smallPlayer play];
    
    [self.view addSubview:_smallPlayer.view];
}

// 收到视屏播放完毕通知执行的方法。
-(void)movieFinishedCallback:(NSNotification *)aNotification{
    NSLog(@"视频播放完了");
    
    [_smallPlayer.view removeFromSuperview];
}


// 指定 我们当前界面只支持竖屏,不管其他界面是横屏还是竖屏  只要回到当前这个界面 就必须是竖屏的
- (BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

代码请查看 http://git.oschina.net/zcb1603999/LearningiOS

评论已关闭。