FirstApp,iphone开发学习总结12,播放音乐、视频

本文介绍如何在iOS应用中集成音乐和视频播放功能。通过引入AVFoundation和MediaPlayer框架,实现了音乐的播放控制及视频的播放,并提供了后台播放的支持。

添加AVFoundation.framework 和MediaPlayer.framework。

在NavView.h文件中,添加:

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface NavView1 : UIViewController<AVAudioPlayerDelegate>{
    AVAudioPlayer *audioPlayer;
    MPMoviePlayerController *moviePlayer;
}
@end

 在init中添加音乐url:

- (id)init {
    self = [super init];
    if (self) {
        //...
        NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"yu" ofType:@"mp3"];
        if (musicPath) {
            NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
            [audioPlayer setDelegate:self];
        }
    }
    return self;
}

 在view中添加2个按钮:

- (void)viewDidLoad
{
    //...
    UIButton *musicPlayBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    musicPlayBtn.frame = CGRectMake(40.070.0240.030.0);
    [musicPlayBtn setTitle:@"Play Music" forState:UIControlStateNormal];
    [musicPlayBtn addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *movePlayBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    movePlayBtn.frame = CGRectMake(40.0110.0240.030.0);
    [movePlayBtn setTitle:@"Play Move" forState:UIControlStateNormal];
    [movePlayBtn addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];

    //...
    [[self view] addSubview:musicPlayBtn];
    [[self view] addSubview:movePlayBtn];
}

 播放音乐按钮实现://退出后,再进入,音乐接着播放,按钮变为play,需要解决方案。

- (void)playMusic:(id)sender
{
    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
        [sender setTitle:@"Play Music" forState:UIControlStateNormal];
    }
    else
    {
        [audioPlayer play];
        [sender setTitle:@"Stop Music" forState:UIControlStateNormal];
    }
}

 视频播放:

- (void)playMovie:(id)sender
{
    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"big" ofType:@"m4v"];
    if (moviePath) {
        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopMoviePlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    moviePlayer.view.frame = CGRectMake(40.0150.0240.0140.0);//播放完成
    [[self view] addSubview:[moviePlayer view]];
}

 //添加通知,当视频播放完成,从view移除

- (void)stopMoviePlay:(id)sender
{
    MPMoviePlayerController *mp = [sender object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
    [[moviePlayer view] removeFromSuperview];
}

打开FirstApp-Info.plist文件,添加Required background modes,设置item0值为App plays audio。支持音乐后台播放。(模拟器不支持)

转载于:https://www.cnblogs.com/maxfong/archive/2012/05/14/2499260.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值