<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    The iPhone SDK’s AVFoundation framework includes AVAudioPlayer, an easy, feature rich, Objective-C based way of playing audio files.

    This tutorial demonstrates how to use AVAudioPlayer. When you’ve finished the tutorial you’ll have created a simple app that plays an MP3 audio file in a loop when the app starts.

    Source/Github

    The code for this tutorial is available on GitHub. You can either clone the repository or download this zip.

    Creating The Project

    Launch Xcode and create a new View-Based iPhone application called AudioPlayer:

    1. Create a new project using File > New Project… from Xcode’s menu
    2. Select View-based Application from the iPhone OS > Application section, click Choose…
    3. Name the project as AudioPlayer and click Save

    Adding The AVFoundation Framework

    In order to use the SDK’s AVAudioPlayer class, we’ll need to add the AVFoundation framework to the project:

    1. Expand the Targets branch in the Groups & Files panel of the project
    2. Control-click or right-click the AudioPlayer item
    3. Choose Add > Existing Frameworks…
    4. Click the + button at the bottom left beneath Linked Libraries
    5. Choose AVFoundation.framework and click Add
    6. AVFoundation.framework will now be listed under Linked Libraries. Close the window

    Next, we’ll import the AVFoundation headers in our view controller’s interface file and set up an AVAudioPlayer instance variable:

    1. Expand the AudioPlayer project branch in the Groups & Files panel of the project
    2. Expand the Classes folder
    3. Edit AudioPlayerViewController.h by selecting it
    4. Update the file. Changes are bold:

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    @interface AudioPlayerViewController : UIViewController {
    AVAudioPlayer *audioPlayer;
    }
    @end
    

    Adding An Audio File

    We’ll need an audio file for playback. We’ll, unimaginatively, call the fille audiofile.mp3. Add it to the project:

    1. Control-click or right click on the Resources folder in the Groups & Files panel of the project
    2. Select Add > Existing Files… from the context menu
    3. Locate and select the file for import and click Add
    4. Check the Copy items into destination group’s folder (if needed) box and click Add

    Starting Audio Playback

    We’ll start the audio playback in ViewDidLoad:

    1. Uncomment the boilerplate ViewDidLoad method
    2. Update it as follows. Changes are bold:

    - (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;
    if (audioPlayer == nil)
    NSLog([error description]);
    else
    [audioPlayer play];
    }
    

    AVAudioPlayer is initialized with a URL, so we create one whose path points to the audio file in our on-the-phone resources directory. Setting the audio player’s numberOfLoops property to a negative number causes it to loop indefinitely. After configuring the player, we start the playback by sending the play message to the object.

    Remember to release the audioPlayer in dealloc. Changes are bold:

    - (void)dealloc {
    [audioPlayer release];
    [super dealloc];
    }
    

    More Capabilities

    You can adjust the volume of the player, check/set the time played so far and pause or stop playback:

    audioPlayer.volume = 0.5; // 0.0 - no volume; 1.0 full volume
    NSLog(@"%f seconds played so far", audioPlayer.currentTime);
    audioPlayer.currentTime = 10; // jump to the 10 second mark
    [audioPlayer pause];
    [audioPlayer stop]; // Does not reset currentTime; sending play resumes
    

    Finally, you can implement the AVAudioPlayerDelegate protocol to, among other things, be notified when audio finishes playing — perhaps to move onto the next song in a playlist.

    posted on 2010-10-01 21:29 seal 閱讀(380) 評論(0)  編輯  收藏 所屬分類: iPhone
    主站蜘蛛池模板: 亚洲人成网站在线播放vr| 亚洲成a人一区二区三区| 亚洲国产精品自在在线观看| 91av免费在线视频| 亚洲一本大道无码av天堂| 深夜免费在线视频| 国外亚洲成AV人片在线观看| www一区二区www免费| 亚洲欭美日韩颜射在线二| 拍拍拍无挡免费视频网站| 亚洲av鲁丝一区二区三区| 免费无码成人AV在线播放不卡| 亚洲精品mv在线观看| 免费看黄视频网站| 亚洲精品国产国语| 国产成人精品免费直播| eeuss免费天堂影院| 亚洲AV无码一区二区乱孑伦AS| 57pao一国产成视频永久免费| 精品亚洲AV无码一区二区| 免费观看一级毛片| 久久国产精品免费一区| 老司机亚洲精品影院| 歪歪漫画在线观看官网免费阅读| 亚洲AV综合色区无码一二三区| 亚洲国产精品无码久久九九| 男人都懂www深夜免费网站| 亚洲乱码中文论理电影| 国产精品视_精品国产免费 | 色吊丝永久在线观看最新免费| 色噜噜狠狠色综合免费视频| 亚洲男同帅GAY片在线观看| 国产大片免费网站不卡美女| 男男黄GAY片免费网站WWW| 亚洲色大成网站www永久一区| 波多野结衣在线免费观看| 免费精品国产自产拍在线观看 | 亚洲精品V欧洲精品V日韩精品| 七色永久性tv网站免费看| 亚洲人成免费电影| 亚洲成AV人网址|