Why my music won't play on Android Studio? - android

I'm using Android Studio.I tried all the codes that I've seen on the internet but my app doesn't work. I have a raw folder which contains the music that I'll be use but still I don't know what is the error in my code.
Here's my code:
package com.example.aloja.babysteps;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by Aloja on 3/27/2017.
*/
public class A extends Activity {
Button btnBack3,btnPlay;
ImageView ivApple;
MediaPlayer apple;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
btnBack3 = (Button) findViewById(R.id.btnBack3);
btnPlay =(Button) findViewById(R.id.btnPlay);
ivApple = (ImageView) findViewById(R.id.ivApple);
ivApple.setImageResource(R.drawable.apple);
apple= MediaPlayer.create(this, R.raw.apple);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
apple.start();
}
});
}
protected void onPause(){
super.onPause();
apple.stop();
apple.release();
}
}
P.S Sorry for my english. Hope you understand what I'm trying to ask

Try this code hope it will help you.
int resID=getResources().getIdentifier("filename", "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.start();

I test your code on MX5 and it works well.
Did you enable your phone's music volume ?
(In MX5, you can see tone, media, notification and system volume.)

Related

seek bar and media player android

I am Making a very simple video player app with play/pause button and a seek bar.
But when I change position of seek bar the video starts playing from starting.
I have tried many solutions to do this but I cannot resolve this.
Please anybody may help me to resolve this.
I am learning the android development and I am a beginner So please give me some tips if you can.
package com.example.videoplayer;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private Button button;
private SeekBar seekBar;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).hide();
button = findViewById(R.id.button);
seekBar = findViewById(R.id.seekBar);
surfaceView = findViewById(R.id.surfaceView);
mediaPlayer = MediaPlayer.create(this,R.raw.kh);
surfaceView.setKeepScreenOn(true);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(#NonNull SurfaceHolder surfaceHolder) {
mediaPlayer.setDisplay(surfaceHolder);
}
#Override
public void surfaceChanged(#NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(#NonNull SurfaceHolder surfaceHolder) {
}
});
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
button.setText("play");
}
else {
mediaPlayer.start();
button.setText("pause");
}
}
});
}
}
Try using
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress * 1000);
}
}

MediaPlayer not working at all

I am a very beginner in Android, I am trying to make a MediaPlayer works but I have some errors.
1-If I click on play again , start playing twice at the same time.
2-If click on puse, doesn't happen anything.
3-My intention is to do a reproduction list with play, pause and stop.
Thanks in advance.
package com.example.android.allmusic;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.MediaPlayer;
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (!mp.isPlaying()) {mp.start();}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (mp.isPlaying()) { mp.pause(); }
}
});
}
}
Try declaring your MediaPlayer as a member variable, it should be enough to set it on your onCreate, but just declare it as an attribute of your class to test.
Since you are creating a new reference to the MediaPlayer every time you click on the ImageView or the TextView, you don't the old reference to pause it or to know if it has already been started.
Code:
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
//MediaPlayer as a member variable
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mp.isPlaying()) {
mp.pause();
}
}
});
}
}

RTSP Android Media player

I am trying to create a RTSP player for Android, but I am getting the error Video Can't be played. I don't know whats the mistake I am making, this is simply not working, I tried all methods, I am giving the code below
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
VideoView myVideoView;
ProgressDialog progDailog;
AudioManager audio;
MediaController mediaController;
String unStringUrl="rtsp://his.dvrdns.org:8554/channel/2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.videoplayer);
progDailog = ProgressDialog.show(MainActivity.this, null, "Video loading...", true);
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(Uri.parse(unStringUrl));
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
// called too soon with rtsp in 4.1
if(progDailog != null) {
progDailog.dismiss();
}
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
/*Intent intent = new Intent(MyVideoView.this, lastActivity);
intent.putExtra("cleTitre", activityTitle);
intent.putExtra("cleSegment", activityCat);
startActivity(intent);*/
}
});
myVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
if(progDailog != null) {
progDailog.dismiss();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You can try including internet -Permisison line in (following line) in your app's Manifest File.In my case it worked.
uses-permission android:name="android.permission.INTERNET"
Hope this helps.

Unable to play html5 streaming video in android web view

I have bought Foscam Security Camera and I am able to see the JPEG streaming on my MacBook.
But when I open the same link in my phone browser using chrome then it starts downloading something not sure what and in notification menu shows unsuccessful download.
Plus if I open the same link on my Android Firefox browser then I am able to see the video.
I have to create an android application to show the streaming of the video just as it is viewable on laptop browser.
The following is the code I am using:
package org.securitycamera;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.VideoView;
public class SecuritycameraActivity extends Activity {
WebView webView;
FrameLayout frameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = getLayoutInflater();
View inflatedView = inflator.inflate(R.layout.main, null);
if (!(inflatedView instanceof FrameLayout))
{
throw new RuntimeException("inflated view not FrameLayout");
}
else
{
frameLayout = (FrameLayout)inflatedView;
}
setContentView(frameLayout);
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON);
webView.setWebChromeClient(new MyWebChromeClient());
try
{
// webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
webView.loadUrl("http://broken-links.com/tests/video/");
}
catch(Exception e)
{
throw new RuntimeException();
}
}
private class MyWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {
VideoView videoView;
WebChromeClient.CustomViewCallback customViewCallback;
public void onProgressChanged(WebView view, int newProgress)
{
if (newProgress == 100)
{
view.loadUrl("javascript:playVideo()");
}
}
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
if (view instanceof FrameLayout){
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView){
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
setContentView(video);
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onCompletion...");
mp.stop();
setContentView(R.layout.main);
}
});
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onError");
return false;
}
});
video.start();
}
}
}
public void onPrepared(MediaPlayer mp)
{
}
public void onCompletion(MediaPlayer mp)
{
// this is needed to release the MediaPlayer and its resources so it can
// be used again later
videoView.stopPlayback();
// now remove the video and tell the callback to hide the custom view
frameLayout.removeView(videoView);
customViewCallback.onCustomViewHidden();
finish();
}
public boolean onError(MediaPlayer mp, int what, int extra)
{
return false; // we did not handle the error - onCompletion will be called
}
}
}
I followed this How to Play HTML5 video and YouTube Video within Android WebView?, and if instead of playing the video in example I play the video of my security camera i.e.
webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
I get a white screen.
this sample https://gist.github.com/3718414 has an Android webview wrapper and HTML5 video - it's not as simple as just referencing the URL (if you want the video in the webView) but it's not difficult on ICS and above (ensuring you have hardware acceleration enabled seems to be pretty key)

How can I get a sound file to play on each click of a button in Android?

I'd like to play a sound as I tap a button like hitting a drum, to have the sound playback as quick as I'm hitting the button. Right now I have to wait for the sound to finish before it will play again. Here's how I've got the sound set up.
MediaPlayer snareMP;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playdrums);
final MediaPlayer snareMP = MediaPlayer.create(this, R.raw.snare);
ImageView snareDrum = (ImageView) findViewById(R.id.snare);
snareDrum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
snareMP.reset();
snareMP.prepare();
snareMP.start();
}
});
Any help is greatly appreciated.
Thanks!
Try this:
public void onClick(View v) {
if (snareMP.isPlaying()){
snareMP.stop();
snareMP.reset();
}
snareMP.prepare();
snareMP.start();
}
I'm going to share a code that I've written a while back which I think does the exact same thing you're looking for; I hope it helps and I'd appreciate some rep points if it does :)
Here it is:
import com.myProject.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Camera extends Activity {
private MediaPlayer snapMP;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
Button snap = (Button)findViewById(R.id.snapButton);
snap.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
freeSnapMP();
snapMP = MediaPlayer.create(Camera.this, R.raw.camera_snap2);
snapMP.start();
}
});
}
protected void onDestroy() {
super.onDestroy();
freeSnapMP();
}
private void freeSnapMP(){
if (snapMP != null){ snapMP.stop(); snapMP.release(); snapMP = null; }
}
}

Categories

Resources