音视频

来源:互联网 发布:淘宝买家超级会员等级 编辑:程序博客网 时间:2024/04/28 23:42
 1.录音

 NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
   
   
    path = [path
stringByAppendingPathComponent:@"123.MP3"];
   
   
NSURL *url = [NSURLURLWithString:path];

   
   
//1.1创建录音对象
   
// settings  录音文件质量的
   
NSDictionary *setting = [NSDictionarydictionary];
   
AVAudioRecorder *recorder = [[AVAudioRecorderalloc]initWithURL:urlsettings:settingerror:nil];
   
   
//2.开始
    [recorderrecord];

   2.播放音效
  //1.获取文件
   
   
NSString *path = @"/Users/apple/Desktop/123.MP3";
   
   
NSURL *url = [NSURLfileURLWithPath:path];
   
   
SystemSoundID soundID = 0;
   
   
//1.返回值  2.&
   
//桥接
   
AudioServicesCreateSystemSoundID((__bridgeCFURLRef _Nonnull)(url), &soundID);
   
   
   
//2.播放
   
   
//没有震动效果
   
AudioServicesPlaySystemSound(soundID);
   
   
//有震动效果
//    AudioServicesPlayAlertSound(soundID);

3.播放多个文件,工具类
#import<Foundation/Foundation.h>

@interfaceCZPlayMusicTool : NSObject
//播放音乐
+ (
void)playAudioWithName:(NSString*)name;
//暂停音乐
+ (
void)pauseAudioWithName:(NSString*)name;
//停止音乐
+ (
void)stopAudioWithNmae:(NSString*)name;
@end


#import"CZPlayMusicTool.h"
#import
<AVFoundation/AVFoundation.h>

staticNSMutableDictionary *_players;
@implementationCZPlayMusicTool

+ (
void)initialize{
   
   
_players = [NSMutableDictionarydictionary];
}
//播放音乐
+ (
void)playAudioWithName:(NSString*)name{
   
   
AVAudioPlayer *player = _players[name];
   
   
if (player == nil) {
   
   
NSString *path = [[NSBundlemainBundle]pathForResource:nameofType:nil];
   
   
NSURL *url = [NSURLfileURLWithPath:path];
   
    player = [[
AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];
        [
_playerssetObject:playerforKey:name];
    }
   
//开始播放
    [player
play];
   
}
//暂停音乐
+ (
void)pauseAudioWithName:(NSString*)name{
   
AVAudioPlayer *player = _players[name];
   
if (player) {
        [player
pause];
    }
}
//停止音乐
+ (
void)stopAudioWithNmae:(NSString*)name{
   
AVAudioPlayer *player = _players[name];
   
if (player) {
       [player
stop];
    }
 
   
}
- (IBAction)play:(id)sender {
    [
CZPlayMusicTool playAudioWithName:@"咖啡.mp3"];
}
- (
IBAction)stop:(id)sender {
    [
CZPlayMusicToolplayAudioWithName:@"煎饼侠.mp3"];
}
- (
IBAction)anting:(id)sender {
    [
CZPlayMusicToolpauseAudioWithName:@"煎饼侠.mp3"];
}
- (
IBAction)djkfs:(id)sender {
    [
CZPlayMusicToolstopAudioWithNmae:@"咖啡.mp3"];
}
- (
IBAction)stop2:(id)sender {
    [
CZPlayMusicToolstopAudioWithNmae:@"煎饼侠.mp3"];
}
0 0
原创粉丝点击