Ext.: http://www.xiaoyaoli.com/?p=368
Just like many iOS player apps, this article writes a program that allows you to put files through itunes, such as writing a music player program, placing a song file through Itune, and then playing the song through this app. First, I created an ordinary Singleview program called Filesharedemo. Here's how to transfer files from itunes to the app:
Can see like Gplayer,aceplayer player, video resources in this way transmission is very common (many Apple users really do not know this can transfer data), here I created an app called Filesharedemo, can see, can also drag data inside, Drag a song here 2.mp3, but by default the app created is not available in the left column, you need to press the plist inside add a line:
The next task is to determine if there is any import, the following code I wrote in the Viewdidload:
Nsfilemanager *manager=[nsfilemanager Defaultmanager];
NSString *path=[[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " 2.mp3″];
if ([manager Fileexistsatpath:path]) {
[Email protected] "have file";
}else {
[Email protected] "has no file";
}
NSLog (@ "Nshomedirectory%@", Nshomedirectory ());
Here are a few things to explain, first nshomedirectory () print out is
/var/mobile/applications/8f01293c-c02d-4ef8-a9f7-1a7b10944f33
This is the path of the program, 8F01293C-C02D-4EF8-A9F7-1A7B10944F33 is a unique tag for each app, such as Angry Birds will be another string of information, specifically why not clear. Then I added/documents and/2.mp3, and through [manager Fileexistsatpath:path] to determine that the path does not exist, print it with a label. If you drag it in, you can see that the label's contents are "file", and you can see the/VAR/MOBILE/APPLICATIONS/8F01293C-C02D-4EF8-A9F7-1A7B10944F33 by using the label information that the program runs. /documents/2.mp3 is the path to the imported file.
We can then use this code to play the song:
nserror* err;
Player = [[Avaudioplayer alloc]
Initwithcontentsofurl:[nsurl Fileurlwithpath:path]
Error:&err];
player.volume=0.7;
[Player play];
The previous path can be used directly here.
The structure of the file is again added below:
Each program will contain these files, documents is the itunes import of the document storage place, Filesharedemo is the Xcode project package, if the project through Xcode to put material what is here, the latter two do not know what the specific role.