I am very new to programming but through determination have found a way to make an application that allows a media url to be played through my app the issue is im trying to make it so that its open to more that 1 specific url for that specific song Im trying to make it so that a playlist that is already set on a site can be chosen through this player if anyone is up to helping me learn and or taking the time to work with me through email or messaging please let me know the code i have so far for this app is as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String url = "http://beatswith.us/uploads/Mac%20Miller%20-%20Paper%20Route%20feat.%20Kev%20Da%20Hustla.mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
mediaPlayer.start();
}
};
So if I'm reading your question right, it sounds like you currently have just one song your app will play but instead you want to be able to choose from a list of songs to play..
What you probably would want to do as a next step is create an array of songs and then use ListView to allow the user to pick which song they want to play. It looks like this may be your first time programming. I suggest you look up some tuturials showing how a list view works and then make an ArrayAdapter to bind to your ListView.. The ArrayAdapter has an array (think of an array as being a variable that has more than 1 thing in it, in your case Strings which represent URL's to songs..
Here is some code to get you started:
public class SongListActivity extends ListActivity {
static final String[] songList = new String[] {
"http://beatswith.us/uploads/Mac%20Miller%20-%20Paper%20Route%20feat.%20Kev%20Da%20Hustla.mp3",
"http://beatswith.us/another1.mp3",
"http://beatswith.us/another2.mp3"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayAdapter songAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songList);
setListAdapter(songAdapter);
}
#Override
protected void onListItemClick(ListView lv, View v,int pos, long id) {
String url = (String) lv.getAdapter().getItem(pos);
playSong(url);
}
private void playSong(String url) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
here is more documentation that should help:
http://developer.android.com/reference/android/app/ListActivity.html
You will need to update the array with valid URL's to songs.
-- Update --
I'm a bit confused still by the exact question.. I'm not sure what exactly you want to do.. If there is a playlist on a website (perhaps in xml format) you could download that xml file and then make a ListAdapter that you would use instead of an ArrayAdapter.. I mean really this gets much more complicated because in reality you probably want to show the name of the song so that means you need to parse the ID3 information from the mp3 -- which is relatively easy to do but it is definitely much more involved than what your current code does.. My example should give you a bit of an idea on how to play a list of songs and should get you a bit more programming experience before you move on to more complicated things.
Related
I am developing an application in which i want to play a short 5 seconds video at the startup. which is the best format 3gp, mpg or something else? i have generated a title activity. I wanted to play the video before title. Help please!!! Below is the code of my title activity.
public class Title extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
setTitle("M.I.S.T");
this.setTitleColor(Color.BLUE);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.YELLOW);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent open= new Intent("com.congestion6.asad.MENU");
startActivity(open);
}
}
};
timer.start();
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
mpeg can be compressing the video over a range of different formats/algorithms/codecs and some are supported some are not. 3gp is just one and it is supported (although a very poor format).
Try encoding a video yourself that you'll see all different options. Usually mp4 on H264 works flawlessly on mobiles.
I keep getting null pointer exceptions for methods of MediaPlayer. I was finally able to get the play function to work by moving the code for play and initialize of play functions into a separate method and call that method from inside the onClick listener.
However am still getting null pointer exception for the apps pause function. I am using pause method of media player. How to the get pause to work? I think the problem is somewhere in the structure of my code and how it is organized.
I tried moving the initialization of the Media player to a different place in the code. and and nothing seems to work. Any ideas?
// onclick listener for the playing the selected song
playB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playSong();
}
});
// onclick listener for pausing the song that is playing
pauseB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pauseSong();
}
});
// method to pause song
public void pauseSong(){
player.pause();
length = player.getCurrentPosition();
}
// method to play song and initialize the MediaPlayer class with one file
// from the drawable folder, need to initialize with something or it will be null
// for that i decided to use an mp3 in R.raw folder
public void playSong(){
// Play song
MediaPlayer player = MediaPlayer.create(this, R.raw.g2);
player.reset();
try {
player.setDataSource(selectedAudioPath);
player.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.seekTo(length);
player.start();
} // play method
You have to make a MediaPlayer player global variable. player is not visible for the pauseSong() method therefore you have nullPointerException. Create a MediaPlayer player in your main class and then in onPlaySong() initialize it only like this:
player = MediaPlayer.create(this, R.raw.g2);
I have been trying to develop an Andriod app of a simple sound board, which will play several long sounds, and only one at a time, not simultaneously. I can easily do a soundboard that uses repetitive code and many mediaplayers, but that will likely crash many devices due to allocating too many instances of MediaPlayer. I want to use a map so that I only use one mediaplayer, but after several hours, I’m still having compile problems. I could also use the App Inventor at MIT, but I don’t think I could upload that to market place without extensive key/signing hacks, so I don’t think that is a good option.
Does anyone know if there a working code example with just a couple of sounds that use just 1 mediaplayer included with the SDK, or available online? If I could start with just a basic working design, it would save me so much time.
My code looks like the below:
public class newBoard extends Activity {
int selectedSoundId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
*//below line causes an error due to use of "newboard"*
setContentView(R.layout.newboard);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
//just keep them in the same order, e.g. button01 is tied to backtoyou
final int[] buttonIds = { R.id.button01, R.id.button02, R.id.button03,
R.id.button04, R.id.button05, R.id.button06,
R.id.button07, R.id.button08, R.id.button09,
R.id.button10, R.id.button11, R.id.button12,
R.id.button13, R.id.button14, R.id.button15,
R.id.button16, R.id.button16, R.id.button17,
R.id.button18, R.id.button19, R.id.button20,
R.id.button21, R.id.button22, R.id.button23,
R.id.button24, R.id.button25 };
final int[] soundIds = { R.raw.sound01, R.raw.sound02, R.raw.sound03,
R.raw.sound04, R.raw.sound05, R.raw.sound06,
R.raw.sound07, R.raw.sound08, R.raw.sound09,
R.raw.sound10, R.raw.sound11, R.raw.sound12,
R.raw.sound13, R.raw.sound14, R.raw.sound15,
R.raw.sound16, R.raw.sound16, R.raw.sound17,
R.raw.sound18, R.raw.sound19, R.raw.sound20,
R.raw.sound21, R.raw.sound22, R.raw.sound23,
R.raw.sound24, R.raw.sound25 };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
//find the index that matches the button's ID, and then reset
//the MediaPlayer instance, set the data source to the corresponding
//sound effect, prepare it, and start it playing.
for(int i = 0; i < buttonIds.length; i++) {
if(v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
break;
}
}
}
};
//set the same listener for every button ID, no need
//to keep a reference to every button
for(int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button)findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
}
I have only one error:
newboard cannot be resolved, or is not a field Type: Java problem
A nice project download that could be used as a foundation would be optimal, if that exists!
Thanks in advance for any guidance!
Maytag87
The error means that it can't find your layout XML file.
Does the file /res/layout/newboard.xml exist in your Eclipse project? Perhaps the layout is called something else, like main.xml, in which case you should use R.layout.main in your Java code when you call setContentView().
i have created a music app.the app has 16 music btns.the app is running with no problem but as i press the btns many times the app forces down..
super.onCreate(icicle);
setContentView(R.layout.main);
int[] ids = {R.id.btn,R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btn10,
R.id.btn11, R.id.btn12, R.id.btn13, R.id.btn14, R.id.btn15, R.id.btn16 };
for (int i : ids) {
b = (Button) findViewById(i);
b.setOnClickListener(this);
}}
//outside of onCreate()
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn:
if (mp != null && mp.isPlaying()) mp.stop();
mp = MediaPlayer.create(zoo.this, R.raw.gata);
mp.start();
break;
this is the code and i use case for every btn.When the app forces down, the logCat is finding a NullPointerException in the mp.start(); of the button that forces the app down..please help!
EDIT in from comment below:
case R.id.btn:
if (mp != null && mp.isPlaying()) mp.stop();
mp.reset();
try {
mp.setDataSource("zoo.this,R.raw.gata");
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mp.start();
break;
I think the point is that a MediaPlayer is a pretty heavy weight resource and you should not create too many of them. Also, as soon as you are done with it, call it's release() method. Anon's point's are both valid: you should try to reuse your media play instead of creating a new one and you should become very familiar with the MediaPlayer documentation. For instance from the MediaPlayer documentation:
Resource may include singleton
resources such as hardware
acceleration components and failure to
call release() may cause subsequent
instances of MediaPlayer objects to
fallback to software implementations
or fail altogether.
The hypothesis is that you are allocating lots of MediaPlayer objects and/or not releasing them fast enough. However, without more code it's impossible to be certain.
i have created an app with music buttons.the app is running with no problem in the emulator of eclipse but as i use my samsung galaxy s for emulator i have a force down error as i press any button..this is my logcat when i press a btn:
![alt text][1]
this is my code from 56-86 line
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if(mp2.isPlaying()==true||mp3.isPlaying()==true||mp4.isPlaying()==true||mp5.isPlaying()==true||mp6.isPlaying()==true||mp7.isPlaying()==true||mp8.isPlaying()==true||mp9.isPlaying()==true||mp10.isPlaying()==true||mp11.isPlaying()==true||mp12.isPlaying()==true)
{mp2.stop();
mp3.stop();mp4.stop();mp5.stop();mp6.stop();mp7.stop();mp8.stop();mp9.stop();mp10.stop();mp11.stop();mp12.stop();
try {
mp2.prepare();
mp3.prepare();
mp4.prepare();
mp5.prepare();mp6.prepare();mp7.prepare();mp8.prepare();mp9.prepare();mp10.prepare();
mp11.prepare();mp12.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
Toast.makeText(a.this, "Eisai", Toast.LENGTH_SHORT).show();
}
else
mp.start();
Toast.makeText(a.this, "Eisai sto myalo", Toast.LENGTH_SHORT).show();
}
});
Is your "if" statement line 58? (Assuming you've noted the line range correctly, it should be.) Set a debug breakpoint on that line, and inspect all of your mp* variables. Most likely one of them is null. As a side note, for clearer code, you might want to try using an ArrayList<MediaPlayer> to store all those MediaPlayer objects.
EDIT: You can find the ArrayList documentation here.
Basically:
List<MediaPlayer> mediaPlayers = new ArrayList<MediaPlayer>();
//I have no idea how you're currently making the MediaPlayers,
//so modify accordingly.
mediaPlayers.add(MediaPlayer.create(Context, Uri));
Then, you can just use something like:
public void stopAllIfPlaying(ArrayList<MediaPlayer> mps) {
for (MediaPlayer mp : mps) {
if(mp.isPlaying()) mp.stop();
}
}
And instead of your if statement and stop statements, use something like this:
stopAllIfPlaying(mediaPlayers);
Then do something similar with your prepare statements.
Next time when you post a stacktrace, what happens BEFORE the NPE is a lot better than what happens after... But it's pretty obvious that Button button = (Button) findViewById(R.id.btn1) is returning null. Make sure your XML is correct and there's actually a button there.