IOS_36 Voice Word Synchronization

Source: Internet
Author: User

Eventually:




Model

  sentence.h//  36_ Voice word synchronization////  Created by Beyond on 14-9-12.//  Copyright (c) 2014 Com.beyond. All rights reserved.//  model, sentence #import <Foundation/Foundation.h> @interface sentence:nsobject//text @property ( nonatomic, copy) NSString *text;//in the audio file, the time of the recitation begins @property (nonatomic, assign) double startTime; @end

Controller

beyondcontroller.m//36_ Voice Word synchronization////Created by Beyond on 14-9-12.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondController.h" #import "Sentence.h" #import "SongTool.h" @interface Beyondcontroller ()//Sentence object array @property (Nonatomic, strong) Nsarray *sentencearr;//start playing on the clock, listen to the Music playback Progress @property ( Nonatomic, Strong) Cadisplaylink *link;//music player can get the current playback time "CurrentTime" @property (nonatomic, strong) Avaudioplayer * Sentenceplayer; @end @implementation beyondcontroller//lazy load, create clock when needed-(Cadisplaylink *) link{if (!_link) {//Bind timing    Clock method self.link = [Cadisplaylink displaylinkwithtarget:self selector: @selector (update)]; } return _link;} Lazy loading, when needed to create an array of sentence objects, from a dictionary array in plist, to an array of objects-(Nsarray *) sentencearr{if (!_sentencearr) {Self.sentencearr = [Sent    ence objectarraywithfilename:@ "Redstory.plist"]; } return _sentencearr;}    -(void) viewdidload{[Super Viewdidload]; The tool class plays music and remembers the created player object "playable CurrentTime" with member variables Self.sentenceplayer = [soNgtool playmusic:@ "one East MP3"];    Play background music [Songtool playmusic:@ "BACKGROUND.CAF"];    At the same time, turn on the clock [self.link addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode]; Self.tableView.contentInset = Uiedgeinsetsmake (20, 0, 0, 0);} #pragma mark-clock method//Key ~ ~ ~ ~ ~ ~ Clock bound Method-(void) update{//Get the current playing position (first number of seconds, for example, 10th second) Double currenttime = Self.sentenceplaye    R.currenttime;    Traverse to find the corresponding sentence int count = Self.sentenceArr.count;                for (int i = 0; i<count; i++) {//1. Current words sentence *s = self.sentencearr[i];        2. Get the next sentence object int next = i + 1;        Sentence *nexts = nil;        Need to prevent cross-border if (Next < count) {nexts = Self.sentencearr[next]; }//3. Key judgment, if the current playback time is greater than the time corresponding to I, and less than i+1 corresponding time if (currenttime >= s.starttime && currenttime < NE            Xts.starttime) {//select and highlight the text of the corresponding line nsindexpath *path = [Nsindexpath indexpathforrow:i insection:0]; [Self.tableview SelectrowatindexPath:path Animated:yes Scrollposition:uitableviewscrollpositiontop];        Break }}} #pragma mark-tableview data source method//Total number of lines, that is, how many sentences-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (NS Integer) section{return self.sentenceArr.count;} Unique text for each line-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath    {//1. Create cell static NSString *cellid = @ "sentence";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:celli    D];    }//2. Set the cell's unique data sentence *s = Self.sentencearr[indexpath.row];        Cell.textLabel.text = S.text; return cell;} @end


Music Playback Tool Class












IOS_36 Voice Word Synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.