Hi this is loo from glowing pigs international and this is the first in a series of tutorials where we're going to talk about how to code in Android an app that has music playing in the background as a service and the reason why you'd want to do this is so that in your app if a person wants to play a song it'll play and they can go and surf the web or get their email or go to another app and the music will continue to play then they can come back into your app and stop the music when they're ready I haven't seen that many demonstrations of how to actually code this on the internet so that's why I put together these tutorials so let's look at what we're going to do in this first tutorial it's very simple we're going to it's going to take two tutorials to do the whole app in its basic form the first tutorial we're going to deal with the activity which means dealing with the screen and then the second tutorial will deal with the service and at the end of the second tutorial you'll know how to do the basics and then we'll move on to add things down the line to make the app even better so basically right now it's very simple this is how it looks the screen is going to be one song called I believe in love by Danny Brooks and you have a button to play the song and when you press this button the song plays and the button changes to a stop button and then when you want to stop the song you just hit again and a button turns into a play button and when you press that button the surface gets fired up and the music plays so that's very simple to start so let's look at how we do this first of all I want to show you how this button is put together what we have are two ping files graphics files that are in the drawable section of your resource in your project and just for those of you who are new to android development we're using eclipse here and if you want the basics of Android development there's a good website cool the new boston.com is a guy named.
Travis who does a lot of tutorials on basic Android stuff and I would highly recommend you go to him because we're not going to go into the basics of Android we're just going to move along as if you know what you're doing but we will show you some new concepts so anyway in this drawable section we have this button let's get that up here which is a pause or stop button and then we have this button as a graphic and that's the play button and what we're going to do and I'll show you how to do this is to interchange those two graphics on the button that you create so that depending upon whether you want to play music or stop music the right graphic is displayed now let's look at now our main dot XML which is the layout for our app what it looks like here is just what you saw in the graphical layout it's just what you saw without though the graphic and the code for this is really what we have is we have a linear layout and we have a textview the textview contains a string which contains that song title I believe in love what this is doing is it's referencing the string dot XML file and you see there's song underscore title and the title of the song and that's how you should do all of your string referencing put your strings in the strings that XML file and then reference them from wherever any of your other files the other thing we have here is a lineal layout with a button the button is called button play stop that's the button ID and that's what we'll be working with in the application okay I also want to let you know that I have created a shell service doing file new and then class I created a service called my place service dot Java and that's just there as a shell because we're going to reference that in our activity so let's take a look at our activity you've got I've created just a really shell of an activity that we're going to add to through this tutorial and here I have some comments showing what we react we actually have to do in this application number one we need to obviously set up the things that are required to create an activity which I've set up the basics right here and then we have to have an on create section which says what happens when the activity is created then we have to set up our views meaning set up our screen then we have to set up listeners listeners have to listen to what is happening to that button we have and if somebody's pressing play they have to obviously trigger the playing of music if someone's pressing stop they have to end the service and in the music so that's what we have going on here that's what we have to code okay so let's get started here first thing what we want to deal with is our on create so let me plug that in and I'm cutting and pasting just to save us some time so we have on create bundle saved instance state you've probably seen this a lot and it references our main dot XML layout file now because.
I'm starting from scratch some of these things have red underlines and why don't I just increase the size of this a bit so you can see better so let's import the bundle library and we're going to also import the intent library by how I hover over these red lines now service intent let me let me explain what we're doing here first of all we're going to in a try-catch loop we're going to set up the service meaning reference the service we're going to trigger a method called the knit views and then another method method called set listeners which are going to actually set up the view and set up the listeners and then we have a catch here that does some message handling in case something goes wrong and it will put a message up in a toast so let's import that toast library okay so what if we hover over here it's going to tell us that we need to create a local variable called service intent so why don't we do that right now and let's plug that in here okay so I think it's pretty clear what we're doing here I'm going to take this down for a second and we now need to create our two methods and knit views and set listeners okay so let's get those so here's a knit views and he is set listeners I'm going to put paste them in here together and let's see what they do - we're going to set up the view we have to create a local variable called button play stop to reference that button play stop that we have in our main that XML file okay that's the first thing we're going to do so let's set up that variable and we'll just stick it up here with the intent variable now you have a red line going under here we have to import the button library okay now what we have to do with this button play stop is we have to set up some listeners also so we'll be doing that shortly but I wanted to show you this next line here is the key to making that button become a pause or play button what you use is something called set background resource set background resource pulls from the resource file the particularly specify and it will assign that to button play stop.
Over here so right here we're choosing the play button so the play button when the initial view comes up the play button is what will be shown and as you can see that's what we're doing here the next thing we need to do then is set up the listeners for the button so we know what to do when the button is clicked on when it's play it'll execute the surface and play the music and then when the button is stopped and we click on it it will end the service and the music so we have to pull in another couple of libraries here one of them is the view library and one is the onclicklistener library so let me just pull that one in too and what we're saying here then with the listeners is when you click on this button the method button place stop click will be fired up so let's get that button place that click method in there and what that we have a boolean violet variable which I'm going to place up top which says is the music playing or is the music not playing ok and that's going to determine for us which of those 2 graphics we display on the button because when the music's playing we want to be displaying the pause button so you can stop it and when the music's not playing we want to be play displaying the play button so we're saying here if the music is not playing we're going to play it play audio which is another method and when we're going to display the pause button and we're going to set music playing to true but if the music is playing then we want to stop the music and display the play button and make the boolean false ok so that's what's going on in here very simple and then the next thing we want to do then is just the last things which are setting up the method to play the audio and setting up the method to stop and actually I'm going to show you how you can set up the shell just by doing this and it's going to plug it in and then by hovering over here I can do that and it'll plug that one in also ok so that's that's basically what we're going to do and I'll just show you what goes into those methods it's really pretty pretty simple stuff play audio.
Okay I'm going to plug in I'm going to skip this for a second here and we'll just say we're going to show you that we start the service which is the intent to service intent and then we put in this catch in here just to catch any errors what we have to do though in order to play the audio is tell that service what audio clip to play so we're going to pass via the service and ten using something called put extra you can pass information to the service and the information is passed as what in what's called a key value pair so this is the key which is the name of the value sent audio link and this is our variable string audio link contains the name of the audio link and the name of that audio link is going to be 10 dot mp3 so we have to create that variable and initialize it with ten dot mp3 let's do that right up here okay so it's ten mp3 and what we're doing down here then is passing that over to the service to tell it what to play then we have to also have something to stop the music from playing so we have something called stop my place service and whoops it's very similar to what we had in play you're saying stop service intent which is our service intent and then we have a catch to catch an error and we also set that boom music playing to false because the music is no longer playing and this is an extra we don't need that what a mistake there sorry okay so that's pretty much everything we have to do to get this activity set up ok so you've just to review you've set up your oncreate you've set up your service you set up your views you make sure that the play button is the one that's being shown initially you set up your listeners to listen to the button and if the button here is in play mode you want isn't a stop my mode would you want to play the audio if the button is in play mode you want to stop the audio and you stop it by stopping the service ok and here we're passing the actual audio link to the service so if we run this what we're going to get is exactly what I showed you at the beginning except it's not going to do anything right we're going to have the activity and we're going to be able to press it and it changes and then we press it again changes back but there's no music playing yet because we haven't done the service so stick around for tutorial 2 and we'll take you through how to create the service and at the end of that tutorial you'll have a complete simple application to play music in the background.
cool shoes !
ReplyDeleteCheck out my blog:)
BE A FOLLOWER !
http://ashpowwow.blogspot.com/
http://ashpowwow.blogspot.com/
im following yours !
wow, my friends is not too happy bout this lol, but ima follow this page if yall follow mine, sid, you already know me.
ReplyDeletethanks for commenting, we really thank you for it ,come back to see the new fashion for the month
ReplyDeleteThankyou,team reallife
copyrights@
Hi this is loo from glowing pigs international and this is the first in a series of tutorials where we're going to talk about how to code in Android an app that has music playing in the background as a service and the reason why you'd want to do this is so that in your app if a person wants to play a song it'll play and they can go and surf the web or get their email or go to another app and the music will continue to play then they can come back into your app and stop the music when they're ready I haven't seen that many demonstrations of how to actually code this on the internet so that's why I put together these tutorials so let's look at what we're going to do in this first tutorial it's very simple we're going to it's going to take two tutorials to do the whole app in its basic form the first tutorial we're going to deal with the activity which means dealing with the screen and then the second tutorial will deal with the service and at the end of the second tutorial you'll know how to do the basics and then we'll move on to add things down the line to make the app even better so basically right now it's very simple this is how it looks the screen is going to be one song called I believe in love by Danny Brooks and you have a button to play the song and when you press this button the song plays and the button changes to a stop button and then when you want to stop the song you just hit again and a button turns into a play button and when you press that button the surface gets fired up and the music plays so that's very simple to start so let's look at how we do this first of all I want to show you how this button is put together what we have are two ping files graphics files that are in the drawable section of your resource in your project and just for those of you who are new to android development we're using eclipse here and if you want the basics of Android development there's a good website cool the new boston.com is a guy named.
ReplyDeleteTravis who does a lot of tutorials on basic Android stuff and I would highly recommend you go to him because we're not going to go into the basics of Android we're just going to move along as if you know what you're doing but we will show you some new concepts so anyway in this drawable section we have this button let's get that up here which is a pause or stop button and then we have this button as a graphic and that's the play button and what we're going to do and I'll show you how to do this is to interchange those two graphics on the button that you create so that depending upon whether you want to play music or stop music the right graphic is displayed now let's look at now our main dot XML which is the layout for our app what it looks like here is just what you saw in the graphical layout it's just what you saw without though the graphic and the code for this is really what we have is we have a linear layout and we have a textview the textview contains a string which contains that song title I believe in love what this is doing is it's referencing the string dot XML file and you see there's song underscore title and the title of the song and that's how you should do all of your string referencing put your strings in the strings that XML file and then reference them from wherever any of your other files the other thing we have here is a lineal layout with a button the button is called button play stop that's the button ID and that's what we'll be working with in the application okay I also want to let you know that I have created a shell service doing file new and then class I created a service called my place service dot Java and that's just there as a shell because we're going to reference that in our activity so let's take a look at our activity you've got I've created just a really shell of an activity that we're going to add to through this tutorial and here I have some comments showing what we react we actually have to do in this application number one we need to obviously set up the things that are required to create an activity which I've set up the basics right here and then we have to have an on create section which says what happens when the activity is created then we have to set up our views meaning set up our screen then we have to set up listeners listeners have to listen to what is happening to that button we have and if somebody's pressing play they have to obviously trigger the playing of music if someone's pressing stop they have to end the service and in the music so that's what we have going on here that's what we have to code okay so let's get started here first thing what we want to deal with is our on create so let me plug that in and I'm cutting and pasting just to save us some time so we have on create bundle saved instance state you've probably seen this a lot and it references our main dot XML layout file now because.
ReplyDeleteI'm starting from scratch some of these things have red underlines and why don't I just increase the size of this a bit so you can see better so let's import the bundle library and we're going to also import the intent library by how I hover over these red lines now service intent let me let me explain what we're doing here first of all we're going to in a try-catch loop we're going to set up the service meaning reference the service we're going to trigger a method called the knit views and then another method method called set listeners which are going to actually set up the view and set up the listeners and then we have a catch here that does some message handling in case something goes wrong and it will put a message up in a toast so let's import that toast library okay so what if we hover over here it's going to tell us that we need to create a local variable called service intent so why don't we do that right now and let's plug that in here okay so I think it's pretty clear what we're doing here I'm going to take this down for a second and we now need to create our two methods and knit views and set listeners okay so let's get those so here's a knit views and he is set listeners I'm going to put paste them in here together and let's see what they do - we're going to set up the view we have to create a local variable called button play stop to reference that button play stop that we have in our main that XML file okay that's the first thing we're going to do so let's set up that variable and we'll just stick it up here with the intent variable now you have a red line going under here we have to import the button library okay now what we have to do with this button play stop is we have to set up some listeners also so we'll be doing that shortly but I wanted to show you this next line here is the key to making that button become a pause or play button what you use is something called set background resource set background resource pulls from the resource file the particularly specify and it will assign that to button play stop.
DeleteOver here so right here we're choosing the play button so the play button when the initial view comes up the play button is what will be shown and as you can see that's what we're doing here the next thing we need to do then is set up the listeners for the button so we know what to do when the button is clicked on when it's play it'll execute the surface and play the music and then when the button is stopped and we click on it it will end the service and the music so we have to pull in another couple of libraries here one of them is the view library and one is the onclicklistener library so let me just pull that one in too and what we're saying here then with the listeners is when you click on this button the method button place stop click will be fired up so let's get that button place that click method in there and what that we have a boolean violet variable which I'm going to place up top which says is the music playing or is the music not playing ok and that's going to determine for us which of those 2 graphics we display on the button because when the music's playing we want to be displaying the pause button so you can stop it and when the music's not playing we want to be play displaying the play button so we're saying here if the music is not playing we're going to play it play audio which is another method and when we're going to display the pause button and we're going to set music playing to true but if the music is playing then we want to stop the music and display the play button and make the boolean false ok so that's what's going on in here very simple and then the next thing we want to do then is just the last things which are setting up the method to play the audio and setting up the method to stop and actually I'm going to show you how you can set up the shell just by doing this and it's going to plug it in and then by hovering over here I can do that and it'll plug that one in also ok so that's that's basically what we're going to do and I'll just show you what goes into those methods it's really pretty pretty simple stuff play audio.
DeleteOkay I'm going to plug in I'm going to skip this for a second here and we'll just say we're going to show you that we start the service which is the intent to service intent and then we put in this catch in here just to catch any errors what we have to do though in order to play the audio is tell that service what audio clip to play so we're going to pass via the service and ten using something called put extra you can pass information to the service and the information is passed as what in what's called a key value pair so this is the key which is the name of the value sent audio link and this is our variable string audio link contains the name of the audio link and the name of that audio link is going to be 10 dot mp3 so we have to create that variable and initialize it with ten dot mp3 let's do that right up here okay so it's ten mp3 and what we're doing down here then is passing that over to the service to tell it what to play then we have to also have something to stop the music from playing so we have something called stop my place service and whoops it's very similar to what we had in play you're saying stop service intent which is our service intent and then we have a catch to catch an error and we also set that boom music playing to false because the music is no longer playing and this is an extra we don't need that what a mistake there sorry okay so that's pretty much everything we have to do to get this activity set up ok so you've just to review you've set up your oncreate you've set up your service you set up your views you make sure that the play button is the one that's being shown initially you set up your listeners to listen to the button and if the button here is in play mode you want isn't a stop my mode would you want to play the audio if the button is in play mode you want to stop the audio and you stop it by stopping the service ok and here we're passing the actual audio link to the service so if we run this what we're going to get is exactly what I showed you at the beginning except it's not going to do anything right we're going to have the activity and we're going to be able to press it and it changes and then we press it again changes back but there's no music playing yet because we haven't done the service so stick around for tutorial 2 and we'll take you through how to create the service and at the end of that tutorial you'll have a complete simple application to play music in the background.
Delete