音乐播放-FreeStreamer

1.首先用xcode7.x创建一个iphone工程,选择singleview模板

2.导入第三方库FreeStreamer

3.添加

MediaPlayer.framework

AudioToolbox.framework

AVFoundation.framework

CFNetwork.framework

libxml2.dylib

本地库

4.在Header Search Paths中添加:$(SDKROOT)/usr/include/libxml2

5.在Prefix Header中添加:$(SRCROOT)/GTYPlayMusic/PrefixHeader.pch

#import <Availability.h>

#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif

6.将Music添加到资源文件夹中

7.在ViewController.h编写代码:

//
//  ViewController.h
//  GTYPlayMusic
//
//  Created by Gao tianyu on 15/8/25.
//  Copyright © 2015年 GTY. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *localMusicName;
@property (weak, nonatomic) IBOutlet UITextField *netMusicAddress;
@property (weak, nonatomic) IBOutlet UISwitch *isNetPlaySwitch;

- (IBAction)playOnclick:(id)sender;

@end

8.在ViewController.m编写代码:

//
//  ViewController.m
//  GTYPlayMusic
//
//  Created by Gao tianyu on 15/8/25.
//  Copyright © 2015年 GTY. All rights reserved.
//

#import "ViewController.h"
//导入第三方库FreeStreamer
#import "FSAudioStream.h"

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) FSAudioStream *audioStream;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化信息
    [self initInfo];
    //添加监听
    [self addAllListen];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) initInfo
{
    self.localMusicName.tag = 100;
    self.netMusicAddress.tag = 101;
    [self.localMusicName becomeFirstResponder];
}

- (void)addAllListen
{
    _localMusicName.delegate = self;
    _netMusicAddress.delegate = self;
}

- (IBAction)playOnclick:(id)sender {
    [self.audioStream play];
}

//local music name:monkey
//net music address:http://yinyueshiting.baidu.com/data2/music/2310119/245200144000128.mp3?xcode=270215fe5404df41ec0a60cf41a4a187
/**
 *  取得本地文件路径
 *
 *  @return 文件路径
 */
-(NSURL *)getFileUrl {
    NSString * localMusicName = _localMusicName.text;
    if (![self isEmptyOrNull:localMusicName]) {
        localMusicName = [NSString stringWithFormat:@"%@.mp3",localMusicName];
        NSString *urlStr=[[NSBundle mainBundle]pathForResource:localMusicName ofType:nil];
        NSURL *url=[NSURL fileURLWithPath:urlStr];
        return url;
    }
    return nil;
}
-(NSURL *)getNetworkUrl {
    NSString * netMusicAddress = _netMusicAddress.text;
    if (![self isEmptyOrNull:netMusicAddress]) {
        NSURL *url=[NSURL URLWithString:netMusicAddress];
        return url;
    }
    return nil;
}

#pragma mark -UITextField Delegate Method
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

/**
 *  创建FSAudioStream对象
 *
 *  @return FSAudioStream对象
 */
-(FSAudioStream *) audioStream {
    
    if (!_audioStream) {
        [_audioStream stop];
        _audioStream = nil;
    }
    NSURL * url;
    if (self.isNetPlaySwitch.isOn) {
        url = [self getNetworkUrl];
    } else {
        url = [self getFileUrl];
    }
    if (url == nil) {
        NSLog(@"请输入网络路径或者音乐名称!");
        return nil;
    }
    //创建FSAudioStream对象
    _audioStream=[[FSAudioStream alloc]initWithUrl:url];
    _audioStream.onFailure=^(FSAudioStreamError error,NSString *description){
        NSLog(@"播放过程中发生错误,错误信息:%@",description);
    };
    _audioStream.onCompletion=^(){
        NSLog(@"播放完成!");
    };
    [_audioStream setVolume:0.5];//设置声音
    return _audioStream;
}

- (BOOL) isEmptyOrNull:(NSString *) str
{
    if (str == nil || [str isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

- (void)dealloc
{
    self.localMusicName = nil;
    self.netMusicAddress = nil;
    self.isNetPlaySwitch = nil;
}

@end

9.在Main.storyboard拖拽控件,与代码关联,自己layout适配一下,最后运行结果为下图

10.该软件功能可以播放网络歌曲,或者本地存储歌曲(只需填写歌曲名就行),点击播放就可以听到歌声了

posted @ 2015-08-25 15:00  IT赏金猎人  阅读(145)  评论(0)    收藏  举报