Simple music player production and music player

Source: Internet
Author: User

Simple music player production and music player

Make a simple music player:

1. Functions: music list, play, pause, last song, next song, progress bar (display progress time, control progress), random play, single loop, sequential play, and lyrics display

2. Application Framework: AVFoundation

3. Knowledge Point: AVPlayer usage, pch File Usage, encapsulation idea, MVC mode, storyBoard usage, message sending mechanism, observer, block callback, nstimer, nsunloop, space constraints, third-party usage, etc.

 

Detailed process:

I. Layout: Use storyBoard Layout

1. song list interface: Use TableViewController to create the corresponding viewController, which inherits from UITableViewController and is associated with the storyBoard control to implement the corresponding method.

2. Play page: Add corresponding controls, add constraints, create corresponding viewController, inherit from UIViewController, and associate with storyBoard controls to implement corresponding methods

Difficulty: Play pictures and lyrics pages ---->. add a ScrollView and set the appropriate height. The width is equal to the screen width. add a View in ScrollView, which is equal to Scroll. The width is twice that of ScrollView, and the left and right sides of ScrollView are 0, c. add two UIView (playback and lyrics page) subviews to the View. The playback page is 0 from the top left of the view, the width is the screen width, and the lyrics page is 0 from the top left of the view, the screen width. on the playback page, add imageView as the playback image and tableView as the lyrics.

2. Obtain song Information

The server data is not standardized. It is a plist file and can be directly obtained in the array format.

3. parse the obtained information

1. create a Model. The obtained lyrics on the network are of the NSString type and need to be processed and converted into two arrays. One is used to store the time information of the lyrics and the other is used to store the lyrics. use the componentsSeparatedByString method of NSString to cut the string into data based on a specific character

2. Convert the obtained data array to Model data.

Iv. encapsulation of playback control

1. Single-Use Case: Because the player can only have one program, there may be sound disorder if there are too many players.

2. Apply block: Add bloak so that the ViewController can control the view through callback,

3. Add a listener: This listener is used to listen on the completion of playing and loading the music to perform corresponding operations.

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (playToEnd) name: AVPlayerItemDidPlayToEndTimeNotification object: nil]; // listen for completed music playing

[PlayItem addObserver: self forKeyPath: @ "status" options: NSKeyValueObservingOptionNew context: nil]; // listen for music Loading completed

4. Apply NStimer and nsunloop to control the playback duration: Use callback to control image rotation, progress bar, and lyrics scrolling.

Self. timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @ selector (playTimer) userInfo: nil repeats: YES];
// Add the timer to the runloop
[[Nsunloop mainRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];
[Self. timer fire];

5. AVPlayer knowledge point:. AVPlayerItem must be created for AVPlayer. The data loading listener listens to its status attribute. B. calculate the number of seconds of the current playback CGFloat timer = _ avPlayer. currentTime. value/_ avPlayer. currentTime. timescale; c. obtain the total length of a song: _ sumTime = self. avPlayer. currentItem. duration. value/self. avPlayer. currentItem. duration. timescale; d. set the playback Point self at a specific time. avPlayer seekToTime: CMTimeMakeWithSeconds (timer * _ sumTime, self. avPlayer. currentTime. timescale) completionHandler: ^ (BOOL finished) {}];

5. process the interface

Difficulty: handling the lyrics [self. lyricTableView selectRowAtIndexPath: [self lyricTableViewTime: timer] animated: YES scrollPosition: UITableViewScrollPositionMiddle];
IndexPath calculation method:

-(NSIndexPath *) lyricTableViewTime :( CGFloat) time {
For (int I = 0; I <model. timerArray. count; I ++ ){
CGFloat timeArray = [model. timerArray [I] AG_StringToTime];
If (time <timeArray ){
Return [NSIndexPath indexPathForItem :( I-1> 0? I-1: 0) inSection: 0];
}
}
Return [NSIndexPath indexPathForItem: model. timerArray. count-1 inSection: 0];
}

Make cell transparent: cell. backgroundColor = [UIColor clearColor];
Change the background when cell is selected: cell. selectedBackgroundView = view;


Download a self-written demo

 

  

 

Related Article

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.