Reputation: 7181
So I'm creating an application for my mobile programming class and I'm running into difficulties with the "play" portion of it. To give you some context: basically we need to create a basic audio player that downloads an mp3 from a predefined source and then play that mp3, among other things. I've correctly set up the download service but I'm confused why my code for playing the file isn't working. In my main.java I have a slew of anon. onclick listeners for the UI's buttons and then a switch statement in my mediaplayer service to figure out which button was clicked.
Here's my onclick for the play option from my main activity:
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PlayService.class);
File path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "Bob_Marley-Jammin.mp3");
intent.putExtra("path", file);
intent.putExtra("key", 0);
startService(intent);
}
});
and here's the download onclick:
download.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), DownloadService.class);
intent.putExtra("url", "https://dl.dropbox.com/s/6e6pn43916nl10i/Bob_Marley-Jammin.mp3?dl=1");
startService(intent);
}
});
and here's my PlayService:
package com.connor.black;
import java.io.IOException;
import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;
public class PlayService extends IntentService{
public PlayService() {
super("PlayService");
}
public MediaPlayer mMediaPlayer;
public String path;
@Override
protected void onHandleIntent(Intent intent) {
path = intent.getStringExtra(path);
int key = intent.getIntExtra("key", 0);
mMediaPlayer = new MediaPlayer();
switch (key) {
case 0: //Play
if (mMediaPlayer.isPlaying())
break;
else{
try {
mMediaPlayer.setDataSource(path);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
mMediaPlayer.start();
}
break;
case 1: //Pause
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
break;
case 2: //Stop
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
}
break;
default:
break;
}
}
}
Basically the file downloads correctly but when I press the "play" button nothing happens.
Upvotes: 1
Views: 1800
Reputation: 179
Is your function called? Use Log.d to check. Is your path to the mp3 correct?
The following small snippet plays an MP3 (change the path to your file)
package com.aplayer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
public class APlayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mMediaPlayer = new MediaPlayer();
Uri data;
data = Uri.parse("/sdcard/jazz.mp3");
mMediaPlayer = MediaPlayer.create(getBaseContext(), data);
mMediaPlayer.setLooping(false); // Set looping
mMediaPlayer.start();
}
}
Upvotes: 1