//
// Viewcontroller. m
// Audioplayer
//
// Created by Apple on 14-7-18.
// Copyright (c) 2014 Apple IOS software developer. All rights reserved.
//
# Import "viewcontroller. H"
# Import <avfoundation/avfoundation. h>
@ Interface viewcontroller () <avaudioplayerdelegate>
{
Avaudioplayer * player;
Nsarray * playlist;
Long int index;
Uiimageview * imageview;
Nstmer * timer;
}
@ Property (weak, nonatomic) iboutlet uislider * didclickedporgress;
@ Property iboutlet uilabel * label;
@ End
@ Implementation viewcontroller
-(Void) createlabels
{
_ Label = [[uilabel alloc] initwithframe: cgrectmake (50,360,220, 20)];
_ Label. textcolor = [uicolor whitecolor];
_ Label. textalignment = nstextalignmentcenter;
[Self. View addsubview: _ label];
}
-(Void) createbjview
{
// Set the volume icon
Uiimageview * volimageview = [[uiimageview alloc] initwithframe: cgrectmake (0, 15, 64, 64)];
Volimageview. Image = [uiimage imagenamed: @ "vol"];
[Self. View addsubview: volimageview];
Imageview = [[uiimageview alloc] initwithframe: cgrectmake (0, 0,320,480)];
Imageview. Image = [uiimage imagenamed: @ "0"];
[Self. View addsubview: imageview];
[Self. View sendsubviewtoback: imageview];}
-(Void) createbuttons
{
// 1. Play/pause button
Uibutton * playorstopbtn = [uibutton buttonwithtype: uibuttontypecustom];
// Set the button Image
[Playorstopbtn setimage: [uiimage imagenamed: @ "play"] forstate: uicontrolstatenormal];
[Playorstopbtn setimage: [uiimage imagenamed: @ "pause"] forstate: uicontrolstateselected];
Playorstopbtn. Frame = cgrectmake (128,410, 64, 64 );
[Playorstopbtn addtarget: Self action: @ selector (didclicked :) forcontrolevents: uicontroleventtouchupinside];
Playorstopbtn. adjustsimagewhenhighlighted = false;
[Self. View addsubview: playorstopbtn];
// 2. Previous or next button
Uibutton * nextbtn = [uibutton buttonwithtype: uibuttontypecustom];
[Nextbtn setimage: [uiimage imagenamed: @ "Next"] forstate: uicontrolstatenormal];
Nextbtn. Frame = cgrectmake (224,410, 64, 64 );
[Nextbtn addtarget: Self action: @ selector (didnextclicked :) forcontrolevents: uicontroleventtouchupinside];
[Self. View addsubview: nextbtn];
Uibutton * prevbtn = [uibutton buttonwithtype: uibuttontypecustom];
[Prevbtn setimage: [uiimage imagenamed: @ "Prev"] forstate: uicontrolstatenormal];
Prevbtn. Frame = cgrectmake (32,410, 64, 64 );
[Prevbtn addtarget: Self action: @ selector (didprevclicked :) forcontrolevents: uicontroleventtouchupinside];
[Self. View addsubview: prevbtn];
}
-(Void) didclicked :( ID) sender
{
Uibutton * playorstopbtn = (uibutton *) sender;
// Determine the current playback status
If (player. Playing)
{
Playorstopbtn. Selected = no;
[PLAYER pause];
}
Else
{
Playorstopbtn. Selected = yes;
[Player play];
}
}
-(Void) didnextclicked :( uibutton *) sender
{
If (player. Playing)
{
If (Index = playlist. Count-1)
{
Index = 0;
}
Else
{
Index ++;
}
[Self play];
}
Else
{
If (Index = playlist. Count-1)
{
Index = 0;
}
Else
{
Index ++;
}
[Self play];
[PLAYER stop];
}
Nslog (@ "the next song ");
}
-(Void) didprevclicked :( uibutton *) sender
{
If (player. Playing)
{
If (Index = 0)
{
Index = playlist. Count-1;
}
Else
{
Index = index-1;
}
[Self play];
}
Else
{
If (Index = 0)
{
Index = playlist. Count-1;
}
Else
{
Index = index-1;
}
[Self play];
[PLAYER stop];
}
Nslog (@ "the previous song ");
}
-(Void) refreshprogress
{
Self. didclickedporgress. value = player. currenttime/player. duration;
}
-(Ibaction) didvolumechanged :( uislider *) sender {
Player. volume = sender. value;
}
-(Ibaction) didprogresschanged :( uislider *) sender {
Player. currenttime = sender. Value * player. duration;
}
-(Void) viewdidload {
[Super viewdidload];
[Self createbjview];
[Self createlabels];
[Self createbuttons];
Playlist [email protected] [@ "give me a reason to forget", @ "save", @ "will be unavailable"];
[Self play];
[PLAYER stop];
}
-(Void) Play
{
// Display album Images
Nsstring * imagename = [nsstring stringwithformat: @ "ldld.jpg", Index];
Imageview. Image = [uiimage imagenamed: imagename];
Nsstring * name = playlist [Index];
// Display the song name
_ Label. Text = [nsstring stringwithformat: @ "% @", playlist [Index];
// Obtain the path of the local music file
Nsstring * Path = [[nsbundle mainbundle] pathforresource: Name oftype: @ "MP3"];
// Read the file into the memory
// Nsdata * Data = [nsdata datawithcontentsoffile: path];
// Create a Player Object
// Player = [[avaudioplayer alloc] initwithdata: data error: Nil];
// Use the following method for network address
// [Nsurl urwithstring: @ "http://www.baidu.com"];
//// Generate a URL object from the file path
Nsurl * url = [nsurl fileurlwithpath: path];
// Avaudioplayer can only play local files
Player = [[avaudioplayer alloc] initwithcontentsofurl: URL error: Nil];
Player. Delegate = self;
// Enable, play Rate Control, initial volume value
Player. enablerate = yes;
Player. Rate = 1;
Player. volume = 0.5;
Nslog (@ "% F", player. Volume );
[PLAYER preparetoplay];
[Player play];
// Start playing from currenttime
// Player. currenttime = 100;
// Call refreshprogress every 1 second
Timer = [nstimer scheduledtimerwithtimeinterval: 1 target: Self selector: @ selector (refreshprogress) userinfo: Nil repeats: Yes];
}
-(Void) didreceivememorywarning {
[Super didreceivememorywarning];
}
@ End
File: // users/Apple/desktop/screenshoot: 202014-07-2320.202.169.59.35.png
File: // users/Apple/desktop/screenshoot: 202014-07-2320.2020.10.0020.5.png
File: /// users/Apple/desktop/screen shortcuts 202014-07-2320.2020.10.00.36.png