Android radio streaming, the steam link doesnt play - android

I've followed up zvzej's post on Online radio streaming app for Android
I am encountering 2 problems:
1st.) The stream link don't play, though buttonPlay.enabled(false) does work, I also don't see the progressbar, eventhough onClick of Play it should be visible. Does this mean the script cant go beyond
playSeekBar.setVisibility(View.VISIBLE);
in startPlaying() ?
2nd.) After clicking Play, then stop, then back to play, the application force crashes, sometimes it requires more clicks, sometimes fewer.
Code:
package com.afghan.radios;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ArmanFMRadio extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.armanfm);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setVisibility(View.VISIBLE);
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
try {
player.prepare();
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}

Related

Android MediaRecorder recording looping back on itself

I have made an app which uses a MediaRecorder and a MediaPlayer, but whenever I record something and play it, it kinda loops back on itself.
e.g I say, one, two, three, four, five, six, seven, eight, nine, ten
but it plays back as one, two, three three, four four, five five five etc. it gets worse if the recording is longer?
It might be a simple bug, but I don't see it.
Here is my code :
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Switch switchPlay;
Button btnRecord, btnDelete;
ProgressBar pBarMetronome;
MediaRecorder mRecorder1, mRecorder2, mRecorder3, mRecorder4, mRecorder5, mRecorder6;
MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6;
String sdPath = "/sdcard/Looper Recordings/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchPlay = (Switch)findViewById(R.id.switchPlay);
btnRecord = (Button)findViewById(R.id.buttonRecord);
btnDelete = (Button)findViewById(R.id.buttonDelete);
pBarMetronome = (ProgressBar)findViewById(R.id.progressBarMetronome);
clearFile();
createFile();
lineOne();
}
private void playOne(){
mp1 = new MediaPlayer();
Uri myUri = Uri.parse("/sdcard/Looper Recordings/one.mp3");
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp1.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp1.prepare();
Toast.makeText(MainActivity.this, "preparing", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
mp1.start();
Toast.makeText(MainActivity.this, "play!", Toast.LENGTH_SHORT).show();
}
private void recordOne(){
mRecorder1 = new MediaRecorder();
mRecorder1.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder1.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder1.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder1.setOutputFile(sdPath+"one.mp3");
//mRecorder1.setAudioEncoder(MediaRecorder.getAudioSourceMax());
mRecorder1.setAudioEncodingBitRate(16);
mRecorder1.setAudioSamplingRate(44100);
try {
mRecorder1.prepare();
Toast.makeText(MainActivity.this, "preparing...", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "starting...", Toast.LENGTH_SHORT).show();
mRecorder1.start();
new CountDownTimer(5000,1000){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
mRecorder1.stop();
Toast.makeText(MainActivity.this, "Stopped!", Toast.LENGTH_SHORT).show();
}
}.start();
}
private void lineOne(){
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recordOne();
}
});
btnRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new CountDownTimer(4000,1000){
#Override
public void onTick(long millisUntilFinished) {
btnRecord.setText(""+(millisUntilFinished/1000));
playOne();
}
#Override
public void onFinish() {
}
}.start();
}
});
switchPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//ON
}else{
//OFF
}
}
});
}
private void clearFile(){
File dir = new File(Environment.getExternalStorageDirectory()+"/sdcard/Looper Recordings");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
}
private void createFile(){
File recordingDirectory = new File(Environment.getExternalStorageDirectory()+ File.separator+"Looper Recordings");
if(!recordingDirectory.exists() && !recordingDirectory.isDirectory())
{
// create empty directory
if (recordingDirectory.mkdirs())
{
Toast.makeText(MainActivity.this, "Folder for recordings created!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Folder for recordings could not be created?", Toast.LENGTH_SHORT).show();
}
}
else
{
}
}
}
I know it's very messy but I'm still prototyping
Thanks to anyone who is willing to help!
It seems you are initializing MediaPlayer multiple times and play them simeltenously
Did you try set looping false.
private void playOne(){
if(mp1 != null && mp1.isPlaying()){
{
mp1.stop();
}
mp1 = new MediaPlayer();
mp1.setLooping(false);
Uri myUri = Uri.parse("/sdcard/Looper Recordings/one.mp3");
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp1.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp1.prepare();
Toast.makeText(MainActivity.this, "preparing", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
mp1.start();
Toast.makeText(MainActivity.this, "play!", Toast.LENGTH_SHORT).show();
}

How to play music in Thread on Android?

I am trying to write code in Android to play music. But i am starting the music in a thread so music is breaking and i need to play the music in thread because i have receive the music name through a Bluetooth socket.
So , the thread will keep listening the socket , to get the updated Music file name. But while playing the music it's breaking and not playing properly.
MyCode:
package com.example.musicexample;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btnPlay;
MediaPlayer mPlayer;
Thread canThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.btnPlay);
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
canThread = new Thread(new Runnable() {
#Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
MainActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.reset();
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
}
mPlayer.start();
}
});
}
}
});
canThread.start();
}
});
} }
This is the above code , please let me know , how can i play the music properly in a Thread?

How to apply video effects (filters like sepia, vintage, etc.) on TextureView in android?

How can we apply filters like sepia, vintage, etc. to a video, and consequently post the same on our servers?
Likewise, we can bluff user by creating a filter layer on TextureView, and consequently applying the corresponding filter on the server.
CODE :
package com.example.mediaplayerdemo_video;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
// Log tag.
private static final String TAG = MainActivity.class.getName();
// Asset video file name.
private static final String FILE_NAME = "test.mp4";
// MediaPlayer instance to control playback of video file.
private MediaPlayer mMediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
TextureView textureView = (TextureView) findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}

How To play radio app in background and show Widget on notification bar?

Considering this code:
package com.radio.radiostar;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import java.io.IOException;
public class MainActivity extends Activity implements OnClickListener {
private final static String RADIO_STATION_URL = "http://178.32.137.180:8665/stream";
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource(RADIO_STATION_URL);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
};
I want to enable the background play, with a widget on notification bar like "apollo" or any other media player (with only play/pause button and the "x" to close streaming and background).
Can you help me, writing the code that I must use, and in which part of code?
Thanks in advance.
Fabio.
EDIT: I have deleted
if (player.isPlaying()) {
player.p();
From
#Override
protected void onPause() {
super.onPause();
}
And now works in background u.u
I only need to show the widget on notification bar :)
You want to play the stream while the app is minimized? Then you have to user the Service. You should move your code to the service. And you will be able to handle notification bar from the service as well.
When you do this, you will probably ask yourself "how can I now update the UI since the player is handled in the service". The answer to this is usage of BroadcastService where your Service will broadcast all important data (like timer ticks while stream is active) and your Activity has to catch those data and use it to fill its own UI.
Since the code for what I described here is huge, you should try implementing a service and broadcast logic first, and if you are stuck, come back here with a concrete question.

Mp3 has paused when screen locked

mp3 pauses when on screen lock..here is my main java code...I've searched some information but nothing helped me.And by the way After clicking Play, then stop, then back to play, the application force crashes, sometimes it requires more clicks, sometimes fewer..Can someone help me please..Thank you !
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class Main extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://users1.jabry.com/mine/inna.mp3");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
When the screen is locked your onPause is being called, and you are explicitly stopping the player there.

Categories

Resources