//
// Secondviewcontroller. m
// Audioplayer
//
// Created by Apple on 14-7-24.
// Copyright (c) 2014 Apple IOS software developer. All rights reserved.
//
# Import "secondviewcontroller. H"
# Import "viewcontroller. H"
@ Interface secondviewcontroller () <uitableviewdatasource, uitableviewdelegate>
@ End
@ Implementation secondviewcontroller
{
Nsarray * playlist;
}
-(Instancetype) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil
{
Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil];
If (Self ){
// Custom Initialization
}
Return self;
}
-(Void) viewdidload
{
[Super viewdidload];
// (0, 0, self. View. Frame. Size. Width, self. View. Frame. Height)
Uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0, 14,320,466) style: uitableviewstyleplain];
// Set the data source
Tableview. datasource = self;
Tableview. Delegate = self;
Tableview. rowheight = 30;
[Self. View addsubview: tableview];
Playlist = @ [@ "save", @ "give me a reason to forget", @ "apple", @ "Jiangnan style"];
}
-(Void) didreceivememorywarning
{
[Super didreceivememorywarning];
// Dispose of any resources that can be recreated.
}
# Pragma mark-uitableviewdatasource
// This is the method in uitableviewdatasource
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section;
{
// Returns the number of rows in the table.
Return playlist. count;
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath;
{
// Use the reuse tag to obtain the existing cell
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: @ "cell"];
If (cell = nil)
{
// Create a cell with an important identifier
Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: @ "cell"];
}
// Display text on Cell
Cell. textlabel. Text = [nsstring stringwithformat: @ "% lD. % @", (long) indexpath. Row, playlist [indexpath. Row];
Return cell;
}
-(Void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath;
{
Nslog (@ "did select row: % d", indexpath. Row );
Viewcontroller * CTRL = [[viewcontroller alloc] initwithnibname: Nil Bundle: Nil];
[CTRL myrow: indexpath. Row];
// Delayed Loading
[Self presentviewcontroller: CTRL animated: Yes completion: ^ {nslog (@ "... completion");}];
}
# Pragma mark-Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
-(Void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender
{
// Get the New View Controller Using [segue destinationviewcontroller].
// Pass the selected object to the new view controller.
}
@ End
//
// Viewcontroller. m
// Audioplayer
//
// Created by Apple on 14-7-18.
// Copyright (c) 2014 Apple IOS software developer. All rights reserved.
//
# Import "viewcontroller. H"
# Import "secondviewcontroller. H"
# Import <avfoundation/avfoundation. h>
@ Interface viewcontroller () <avaudioplayerdelegate>
{
Int _ myrow;
Avaudioplayer * player;
Uiimageview * imageview;
Nstmer * timer;
Nsarray * playlist;
}
@ Property iboutlet uislider * progressslider;
@ Property iboutlet uilabel * musicname;
@ End
@ Implementation viewcontroller
-(Void) myrow :( INT) Row
{
_ Myrow = row;
}
-(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
{
// 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) createslider
{
Uislider * volumeslider = [[uislider alloc] initwithframe: cgrectmake (60, 35,220, 10)];
Volumeslider. value = 0.5;
Volumeslider. maximumvalue = 1;
Volumeslider. minimumvalue = 0;
[Volumeslider addtarget: Self action: @ selector (volume :) forcontrolevents: uicontroleventvaluechanged];
[Self. View addsubview: volumeslider];
_ Progressslider = [[uislider alloc] initwithframe: cgrectmake (30,390,260, 10)];
_ Progressslider. tintcolor = [uicolor bluecolor]; _ progressslider. minimumvalue = 0;
_ Progressslider. maximumvalue = 1;
[_ Progressslider addtarget: Self action: @ selector (Progress :) forcontrolevents: uicontroleventvaluechanged];
[Self. View addsubview: _ progressslider];
}
-(Void) createlabel
{
_ Musicname = [[uilabel alloc] initwithframe: cgrectmake (60,350,200, 20)];
_ Musicname. textcolor = [uicolor whitecolor];
_ Musicname. textalignment = nstextalignmentcenter;
_ Musicname. Text = [nsstring stringwithformat: @ "% @", playlist [_ myrow];
[Self. View addsubview: _ musicname];
}
-(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 (_ myrow = playlist. Count-1)
{
_ Myrow = 0;
}
Else
{
_ Myrow ++;
}
[Self play];
}
Else {
If (_ myrow = playlist. Count-1)
{
_ Myrow = 0;
}
Else
{
_ Myrow ++;
}
[PLAYER pause];
}
Nslog (@ "the next song ");
}
-(Void) didprevclicked :( uibutton *) sender
{
If (player. Playing)
{
If (_ myrow = 0)
{
_ Myrow = playlist. Count-1;
}
Else
{
_ Myrow = _ myRow-1;
}
[Self play];
}
Else
{
If (_ myrow = 0)
{
_ Myrow = playlist. Count-1;
}
Else
{
_ Myrow = _ myRow-1;
}
[PLAYER pause];
}
Nslog (@ "the previous song ");
}
-(Void) Volume :( uislider *) sender
{
Player. volume = sender. value;
Nslog (@ "% F", Sender. value );
}
-(Void) Progress :( uislider *) sender {
Player. currenttime = sender. Value * player. duration;
}
-(Void) refreshprogress
{
Self. progressslider. value = player. currenttime/player. duration;
}
-(Void) viewdidload {
Uibutton * BTN = [uibutton buttonwithtype: uibuttontypesystem];
[BTN settitle: @ "back" forstate: uicontrolstatenormal];
[BTN addtarget: Self action: @ selector (didclicked) forcontrolevents: uicontroleventtouchupinside];
BTN. Frame = cgrectmake (280, 20, 40, 20 );
[Self. View addsubview: BTN];
[Super viewdidload];
[Self createbjview];
[Self createbuttons];
[Self createslider];
[Self createlabel];
Playlist = @ [@ "save", @ "give me a reason to forget", @ "apple", @ "Jiangnan style"];
[Self play];
// [Player stop];
}
-(Void) didclicked
{
[Timer invalidate];
// Disappear the modal View
[Self dismissviewcontrolleranimated: Yes completion: Nil];
}
-(Void) Play
{
// 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];
// Display album Images
Nsstring * imagename = [nsstring stringwithformat: @ "mongod.jpg", _ myrow];
Imageview. Image = [uiimage imagenamed: imagename];
Nsstring * name = playlist [_ myrow];
_ Musicname. Text = [nsstring stringwithformat: @ "% @", playlist [_ myrow]; // obtain the file path of the local music
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
Nserror * err;
Player = [[avaudioplayer alloc] initwithcontentsofurl: URL error: & err];
Nslog (@ "Err: % @", err );
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;
If (player. Playing)
{
Playorstopbtn. Selected = yes;
}
[Timer invalidate];
// Call refreshprogress every 1 second
Timer = [nstimer scheduledtimerwithtimeinterval: 1 target: Self selector: @ selector (refreshprogress) userinfo: Nil repeats: Yes];
}
-(Void) didreceivememorywarning {
[Super didreceivememorywarning];
}
@ End