New to Android coding here. I'm trying to have the user pick an audio file for the MediaPlayer to play using the "upload" button, and then the MediaPlayer should start playing the file once the user hits "play." Here is my code so far:
public class MainActivity extends Activity {
final int ACTIVITY_CHOOSE_FILE = 1;
File original;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("audio/mpeg");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
});
Button playButton = (Button)findViewById(R.id.playbutton);
playButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(original.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
original = new File(uri.getPath());
String tag = "TAG";
Log.d(tag, original.getAbsolutePath());
}
}
}
}
}
I can choose the audio file fine, but once I hit play nothing happens. What am I doing wrong?
After setting your mediaPlayer data source you have to call mediaPlayer.prepareAsync()
Media Playback in android
Related
I did not understand this problem, when i record a video through back camera then video view showing actually view which should showing in the video View but when i use front camera then its rotate to opposite. i am using one class for both then why i am getting this type behavior of video view?
video view code
public class VideoPreviewActivity extends Activity {
VideoView VideoPreview;
String videoURI ="VIDEO";
Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_preview_screen);
VideoPreview = (VideoView) findViewById(R.id.video_preview);
back = (Button) findViewById(R.id.back);
Bundle bundle = getIntent().getExtras();
videoURI = bundle.getString("VIDEO");
PlayVideo();
back.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent i = new Intent(VideoPreviewActivity.this, PicRecordShareActivity.class);
startActivity(i);
}
});
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoPreviewActivity.this);
mediaController.setAnchorView(VideoPreview);
// Toast.makeText(getApplicationContext(), "Video:\t"+videoIndex, Toast.LENGTH_LONG).show();
Uri video = Uri.parse(videoURI);
VideoPreview.setMediaController(mediaController);
VideoPreview.setVideoURI(video);
VideoPreview.requestFocus();
VideoPreview.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
VideoPreview.start();
}
});
}
catch(Exception e)
{
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
Thank You
I am new to programming and upon referring google's dev site I came up with a simple media player that plays the file selected by the user. The app seems to been running fine when choosing the file to be played for the first time but crashes right after selecting a file for the second time. I have pasted the code below. Any help will be appreciated.
public class MainActivity extends Activity {
private static int Reqs =1;
private String a;
MediaPlayer md=new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Start = (Button) findViewById(R.id.button);
final Button Stop = (Button) findViewById(R.id.button3);
final Button Pause = (Button) findViewById(R.id.button2);
final Button Select = (Button) findViewById(R.id.button4);
Pause.setEnabled(false);
Stop.setEnabled(false);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
md.start();
Toast.makeText(getApplicationContext(), "Playing", Toast.LENGTH_SHORT).show();
Pause.setEnabled(true);
Stop.setEnabled(true);
}
});
Stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Pause.setEnabled(false);
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
md.stop();
}
});
Pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
md.pause();
Toast.makeText(getApplicationContext(), "Paused", Toast.LENGTH_SHORT).show();
}
});
Select.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(intent, "Choose"), Reqs);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode <= Reqs && resultCode ==-1) {
Uri videoUri = data.getData();
a = videoUri.toString();
md.setDataSource(a); //try-catch surrounding it
md.prepare(); //try-catch surrounding it
}
}
}
You are not calling reset() to reset the state of the MediaPlayer object.
As the Android's documentation states:
In order to reuse a MediaPlayer object that is in the Error state and recover from the error, reset() can be called to restore the object to its Idle state.
Check it at: https://developer.android.com/reference/android/media/MediaPlayer.html
this is the intent which opens a chooser from the music app
public void launchMusicPlayer(View view) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,1);
}
and below is the onActivityResult code to try and obtain the song
, but the problem is that the test button when pressed, does not play anything
and as i am new to coding i don't know what i am doing wrong.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
final MediaPlayer testSong = new MediaPlayer();
Uri songUri = data.getData();
try {
testSong.setDataSource(this, songUri);
} catch (IOException e) {
e.printStackTrace();
}
testSong.prepareAsync();
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testSong.start();
}
});
}
}
The issue maybe because prepareAsync() is asynchronous, so the MediaPlayer may not be ready when you call start(). There are 2 ways to fix this:
Use prepare() instead of prepareAsync()
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testSong.prepare();
testSong.start();
}
});
call start() method inside setOnPreparedListener()
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testSong.prepareAsync();
testSong.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
player.start();
}
}
});
Refer to the documentation here http://developer.android.com/reference/android/media/MediaPlayer.html
As I said above In the code below when i click the button music is play but When I click button for the second time not pause and Will play again
what can i do?
please help me
public class AudioPlayer extends Activity implements OnClickListener {
Button playButton;
MediaPlayer player;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.ButtonPlayStop);
playButton.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/bluetooth/یه سوال دارم مگه.mp3");
player = MediaPlayer.create(this,Uri.fromFile(audioFile));
if(audioFile.exists())
{
if(player.isPlaying()){
if(player!=null){
player.pause();
}
}else{
// Resume song
if(player!=null){
player.start();
}
}
}
else
{
Builder alert = new AlertDialog.Builder( AudioPlayer.this);
alert.setTitle("Alert");
alert.setMessage("فایل دانلود نشده است");
alert.setPositiveButton("OK", null);
alert.show();
}
}
Try this:
Put this out side Onlick say in Oncreate()
player = MediaPlayer.create(this,Uri.fromFile(audioFile));
And
if(audioFile.exists())
{
if(player!=null)
{
if(player.isPlaying())
{
player.pause();
}
else
{
player.start();
}
}
}
I'm having a bit of a trouble,, here's the gen. idea,, I have created an app a simple game-like application.. however the problem is when I try to click the button in the title screen it doesn't play the click sound.. can anyone please help me.. here's the code for my sound
public class Effects extends Activity implements MediaPlayer.OnCompletionListener {
Button mPlay;
MediaPlayer mPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlay = (Button)findViewById(R.id.btnStart);
mPlay.setOnClickListener(playListener);
setContentView(R.layout.activity_main);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mPlayer != null) {
mPlayer.release();
}
}
private View.OnClickListener playListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer == null) {
try {
mPlayer = MediaPlayer.create(Effects.this, R.raw.button11);
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
};
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
and here's my code for the main java that I want the sound to be played when I clicked this button
public class Main extends Activity implements OnClickListener
{
Button about;
Button Quit;
Button start;
protected void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
setContentView(R.layout.activity_main);
about = (Button)findViewById(R.id.btnAbout);
about.setOnClickListener(this);
Quit = (Button)findViewById(R.id.btnQuit);
Quit.setOnClickListener(this);
start = (Button)findViewById(R.id.btnStart);
start.setOnClickListener(this);
}
public void onClick(View argo)
{
if(argo.getId()==R.id.btnAbout)
{
Intent i = new Intent(this,About.class);
this.startActivity(i);
}
if(argo.getId()==R.id.btnQuit)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
if(argo.getId()==R.id.btnStart)
{
Intent start = new Intent(this,Howto.class);
this.startActivity(start);
Intent svc=new Intent(this,Effects.class);
startService(svc);
}
}
}
Use SoundPool to play your sounds: http://developer.android.com/reference/android/media/SoundPool.html you should init sounds in your onCreate() and then play them back in onClick()
And instead of doing thousands of pointless if(argo.getId()==R.id.btnQuit) (you should at least use else) get familiar with switch/case syntax)