Reputation: 89
I would like Playing online song in my application, So i have used mediaplayer for playing song. Then it give Urisyntax error,I have confused about these error. My code is,
package com.yeshuduniya;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.Model.GallryModel;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.PowerManager.WakeLock;
public class Songs_Player extends Activity implements OnClickListener ,OnCompletionListener
{
Uri uri;
URL url;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying = false;
private String song_url;
Handler seekHandler = new Handler();
TextView txt_song_name;
private static int classID = 579; // just a number
SeekBar seek_bar;
WakeLock wakeLock;
ProgressDialog progressDialogue;
private static final String[] EXTENSIONS = { ".mp3", ".mid", ".wav", ".ogg", ".mp4" }; //Playable Extensions
List<String> trackArtworks; //Track artwork names
ImageView bg; //Track artwork
Button btn_Play,btn_prev,btn_next; //The play button will need to change from 'play' to 'pause', so we need an instance of it
int currentTrack; //index of current track selected
int type; //0 for loading from assets, 1 for loading from SD card
ArrayList<GallryModel> songList;
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_songs__player);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Lexiconda");
setUI();
Intent mIntent = getIntent();
songList=(ArrayList<GallryModel>) mIntent.getSerializableExtra("SongsList");
currentTrack = mIntent.getIntExtra("position", 0);
if(savedInstanceState ==null)
{
mediaPlayer=((YeshuDuniaApplication) Songs_Player.this.getApplication()).medaiplayer;
if(mediaPlayer.isPlaying())
{
stop();
playTrack(currentTrack);
}
else
{
playTrack(currentTrack);
//Toast.makeText(getBaseContext(), "Loaded " + Integer.toString(songList.size()) + " Tracks", Toast.LENGTH_SHORT).show();
}
//play(songList.get(currentTrack).getImage());
btn_Play.setBackgroundResource(R.drawable.pause);
}
btn_next.setOnClickListener(this);
btn_prev.setOnClickListener(this);
btn_Play.setOnClickListener(this);
}
private void setUI()
{
//seek_bar = (SeekBar) findViewById(R.id.seek_bar);
btn_next=(Button)findViewById(R.id.btn_mgallary_next);
btn_prev=(Button)findViewById(R.id.btn_mgallary_previous);
btn_Play=(Button)findViewById(R.id.btn_mgallary_Play);
bg = (ImageView) findViewById(R.id.bg);
txt_song_name=(TextView)findViewById(R.id.txt_song_name);
}
@Override
public void onResume(){
super.onResume();
wakeLock.acquire();
}
//Play Songs.
public void play(int currentTrack) throws MalformedURLException
{ this.currentTrack=currentTrack;
System.out.println("Playiong track is"+currentTrack);
this.song_url=songList.get(currentTrack).getSongUrl();
if (!isPlaying)
{
isPlaying = true;
btn_Play.setBackgroundResource(R.drawable.pause);
try {
url = new URL(song_url);
mediaPlayer.setLooping(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
uri = Uri.parse(url.toURI().toString());
mediaPlayer.setDataSource(uri.toString());
//mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(this);
// When song is ended then media player automatically called onCompletion method.
}
catch (MalformedURLException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalArgumentException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (SecurityException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalStateException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch (URISyntaxException e1)
{
btn_Play.setBackgroundResource(R.drawable.play);
isPlaying=false;
Toast.makeText(getBaseContext(), "File is not mp3 supported", Toast.LENGTH_SHORT).show();
e1.printStackTrace();
}
}
else
{
stop();
play(currentTrack);
}
}
//Stop song.
public void stop()
{
synchronized(this)
{
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.reset();
}
//stopForeground(true);
}
}
//Pause song.
public void pause()
{
if (isPlaying) {
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.pause();
mediaPlayer = null;
}
}
}
@Override
public void onPause()
{
super.onPause();
wakeLock.release();
}
//Generate a String Array that represents all of the files found
//Plays the Track
public void playTrack(int currentTrack)
{
this.currentTrack=currentTrack;
//this.song_url=currentTrack;
if(!isPlaying )
{
try {
play(currentTrack);
Toast.makeText(Songs_Player.this, "Playing " + songList.get(currentTrack).getSongTitle(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onClick(View v)
{ boolean flag=false;
switch(v.getId())
{
case R.id.btn_mgallary_Play:
synchronized(this){
if(isPlaying)
{
isPlaying = false;
if (mediaPlayer != null)
{
mediaPlayer.pause();
}
btn_Play.setBackgroundResource(R.drawable.play);
}
else
{
isPlaying = true;
mediaPlayer.start();
btn_Play.setBackgroundResource(R.drawable.pause);
//playTrack(songList.get(currentTrack).getImage());
}
}
return;
case R.id.btn_mgallary_previous:
stop();
if(currentTrack == 0)
{
isPlaying=false;
currentTrack=songList.size()-1;
playTrack(currentTrack);
}
else
{
isPlaying=false;
currentTrack=currentTrack-1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
return;
case R.id.btn_mgallary_next:
stop();
if(currentTrack == songList.size()-1)
{
isPlaying=false;
currentTrack=0;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{
isPlaying=false;
btn_Play.setBackgroundResource(R.drawable.play);
currentTrack=currentTrack+1;
playTrack(currentTrack);
}
return;
default:
return;
}
}
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
mediaPlayer.stop();
mediaPlayer.reset();
if(currentTrack != songList.size()-1)
{ isPlaying=false;
currentTrack=currentTrack+1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{ if(currentTrack == 0)
{
isPlaying=false;
currentTrack=songList.size()-1;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
else
{
if(currentTrack == songList.size()-1)
{
isPlaying=false;
currentTrack=0;
btn_Play.setBackgroundResource(R.drawable.play);
playTrack(currentTrack);
}
}
}
}
}
It gove following error,
04-09 14:30:20.131: W/System.err(2414): java.net.URISyntaxException: Illegal character in path at index 58: http://www.yeshuduniya.com/admin/yeshuDuniyaMusic/25_Aamhi devachilekare/25_Aamhi devachilekare.mp3
04-09 14:30:20.131: W/System.err(2414): at libcore.net.UriCodec.validate(UriCodec.java:63)
04-09 14:30:20.131: W/System.err(2414): at java.net.URI.parseURI(URI.java:402)
04-09 14:30:20.131: W/System.err(2414): at java.net.URI.<init>(URI.java:204)
04-09 14:30:20.131: W/System.err(2414): at java.net.URL.toURI(URL.java:505)
04-09 14:30:20.131: W/System.err(2414): at com.yeshuduniya.Songs_Player.play(Songs_Player.java:159)
04-09 14:30:20.131: W/System.err(2414): at com.yeshuduniya.Songs_Player.playTrack(Songs_Player.java:275)
04-09 14:30:20.131: W/System.err(2414): at com.yeshuduniya.Songs_Player.onCreate(Songs_Player.java:92)
04-09 14:30:20.131: W/System.err(2414): at android.app.Activity.performCreate(Activity.java:5231)
04-09 14:30:20.131: W/System.err(2414): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-09 14:30:20.131: W/System.err(2414): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-09 14:30:20.131: W/System.err(2414): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-09 14:30:20.131: W/System.err(2414): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-09 14:30:20.141: W/System.err(2414): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-09 14:30:20.141: W/System.err(2414): at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 14:30:20.141: W/System.err(2414): at android.os.Looper.loop(Looper.java:136)
04-09 14:30:20.141: W/System.err(2414): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-09 14:30:20.141: W/System.err(2414): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:30:20.141: W/System.err(2414): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:30:20.141: W/System.err(2414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-09 14:30:20.141: W/System.err(2414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-09 14:30:20.141: W/System.err(2414): at dalvik.system.NativeStart.main(Native Method)
Please give me suggestion.
Upvotes: 0
Views: 427
Reputation: 24853
Try this..
You can use the java.net.URLEncoder
to encode the URL
space need to replace with %20
use below method to use your url URLEncoder
imports
import android.webkit.URLUtil;
import android.util.Patterns;
import java.net.URLEncoder;
Codes
try {
if(URLUtil.isValidUrl(song_url) || Patterns.WEB_URL.matcher(song_url).matches()) {
song_url = URLEncoder.encode(song_url,"UTF-8");
url = new URL(song_url);
// so on
} else {
Toast.makeText(getBaseContext(), "Please enter valid URL.", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation: 3497
Tru to use URLEncoder. For exmaple:
song_url = URLEncoder.encode(song_url);
Also you can provide song_url
as datasource directly:
mediaPlayer.setDataSource(song_url);
mediaPlayer.prepare();
Upvotes: 2