[trans]iOS开发之多媒体播放

2023-11-23 03:38

本文主要是介绍[trans]iOS开发之多媒体播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

iOS开发之多媒体播放

  iOS sdk中提供了很多方便的方法来播放多媒体。本文将利用这些SDK做一个demo,来讲述一下如何使用它们来播放音频文件。

AudioToolbox framework

    使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。

1、 播放的时间不能超过30秒

2、数据必须是 PCM或者IMA4流格式

3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)

    声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。看下面注册代码:

#import <AudioToolbox/AudioToolbox.h>
@interface MediaPlayerViewController : UIViewController
{
IBOutlet UIButton
*audioButton;
SystemSoundID shortSound;
}
复制代码
- (id)init
{
self
= [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
if (self) {
// Get the full path of Sound12.aif
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"
ofType:
@"aif"];
// If this file is actually in the bundle...
if (soundPath) {
// Create a file URL with this path
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

// Register sound file located at that URL as a system sound
OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)soundURL,
&shortSound);
if (err != kAudioServicesNoError)
NSLog(
@"Could not load %@, error code: %d", soundURL, err);
}
}
return self;
}
复制代码

这样就可以使用下面代码播放声音了:

- (IBAction)playShortSound:(id)sender
{
AudioServicesPlaySystemSound(shortSound);
}
复制代码

使用下面代码,还加一个震动的效果:

- (IBAction)playShortSound:(id)sender
{
AudioServicesPlaySystemSound(shortSound);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
复制代码

AVFoundation framework

   对于压缩过Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。这个类定义在AVFoundation framework中。

    下面我们使用这个类播放一个mp3的音频文件。首先要引入AVFoundation framework,然后MediaPlayerViewController.h中添加下面代码:

#import <AVFoundation/AVFoundation.h>
@interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>
{
IBOutlet UIButton
*audioButton;
SystemSoundID shortSound;
AVAudioPlayer
*audioPlayer;
复制代码

    AVAudioPlayer类也是需要知道音频文件的路径,使用下面代码创建一个AVAudioPlayer实例:

- (id)init
{
self
= [super initWithNibName:@"MediaPlayerViewController" bundle:nil];

if (self) {

NSString
*musicPath = [[NSBundle mainBundle] pathForResource:@"Music"
ofType:
@"mp3"];
if (musicPath) {
NSURL
*musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer
= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
error:nil];
[audioPlayer setDelegate:self];
}
NSString
*soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"
ofType:
@"aif"];
复制代码

我们可以在一个button的点击事件中开始播放这个mp3文件,如:

- (IBAction)playAudioFile:(id)sender
{
if ([audioPlayer isPlaying]) {
// Stop playing audio and change text of button
[audioPlayer stop];
[sender setTitle:
@"Play Audio File"
forState:UIControlStateNormal];
}
else {
// Start playing audio and change text of button so
// user can tap to stop playback
[audioPlayer play];
[sender setTitle:
@"Stop Audio File"
forState:UIControlStateNormal];
}
}
复制代码

这样运行我们的程序,就可以播放音乐了。

这个类对应的AVAudioPlayerDelegate有两个委托方法。一个是 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag
{
[audioButton setTitle:
@"Play Audio File"
forState:UIControlStateNormal];
}
复制代码

另一个是audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
[audioPlayer play];
}
复制代码

MediaPlayer framework

播放电影文件:

    iOS sdk中可以使用MPMoviePlayerController来播放电影文件。但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件。

• H.264 (Baseline Profile Level 3.0)
• MPEG-4 Part 2 video (Simple Profile)

幸运的是你可以先使用iTunes将文件转换成上面两个格式。
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。

这个类定义在MediaPlayer framework中。在你的应用程序中,先添加这个引用,然后修改MediaPlayerViewController.h文件。

#import <MediaPlayer/MediaPlayer.h>
@interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>
{
MPMoviePlayerController
*moviePlayer;
复制代码

下面我们使用这个类来播放一个.m4v 格式的视频文件。与前面的类似,需要一个url路径。

- (id)init
{
self
= [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
if (self) {

NSString
*moviePath = [[NSBundle mainBundle] pathForResource:@"Layers"
ofType:
@"m4v"];
if (moviePath) {
NSURL
*movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer
= [[MPMoviePlayerController alloc]
initWithContentURL:movieURL];
}
复制代码

MPMoviePlayerController有一个视图来展示播放器控件,我们在viewDidLoad方法中,将这个播放器展示出来。

- (void)viewDidLoad
{
[[self view] addSubview:[moviePlayer view]];
float halfHeight = [[self view] bounds].size.height / 2.0;
float width = [[self view] bounds].size.width;
[[moviePlayer view] setFrame:CGRectMake(
0, halfHeight, width, halfHeight)];
}
复制代码

还有一个MPMoviePlayerViewController类,用于全屏播放视频文件,用法和MPMoviePlayerController一样。

MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[viewController presentMoviePlayerViewControllerAnimated:playerViewController];
复制代码

   我们在听音乐的时候,可以用iphone做其他的事情,这个时候需要播放器在后台也能运行,我们只需要在应用程序中做个简单的设置就行了。

1、在Info property list中加一个 Required background modes节点,它是一个数组,将第一项设置成设置App plays audio。

2、在播放mp3的代码中加入下面代码:

    if (musicPath) {
NSURL
*musicURL = [NSURL fileURLWithPath:musicPath];
[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer
= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
error:nil];
[audioPlayer setDelegate:self];
}
复制代码

在后台运行的播放音乐的功能在模拟器中看不出来,只有在真机上看效果。

总结:本文通过例子详细讲解了iOS sdk中用于播放音频文件的类,文章后面有本文的代码提供下载。

代码:http://files.cnblogs.com/zhuqil/MediaPlayer.zip

作者:朱祁林
出处:http://zhuqil.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

这篇关于[trans]iOS开发之多媒体播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/415065

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件