Reputation: 15
Hi I have been trying to work this code out for a while now, but it gives an error to the logcat as follows:
09-01 13:14:28.799: E/AwesomePlayer(123): reset+
09-01 13:14:28.799: E/AwesomePlayer(123): reset at1
09-01 13:14:28.799: E/AwesomePlayer(123): reset-
09-01 13:14:28.799: E/AwesomePlayer(123): reset at1
09-01 13:14:28.799: E/MediaPlayer(3092): error (1, -2147483648)
STACKTRACE
09-01 13:31:33.749: E/MediaPlayer(3458): error (1, -2147483648)
09-01 13:31:33.759: E/~~IOException~~(3458): java.io.IOException: Prepare failed.: status=0x1
09-01 13:31:33.789: E/->>(3458): ~~stacktrace~~
09-01 13:31:33.789: E/->>(3458): java.io.IOException: Prepare failed.: status=0x1
09-01 13:31:33.789: E/->>(3458): at android.media.MediaPlayer.prepare(Native Method)
09-01 13:31:33.789: E/->>(3458): at com.example.boombastic.Playlist.playSong(Playlist.java:110)
09-01 13:31:33.789: E/->>(3458): at com.example.boombastic.Playlist.access$0(Playlist.java:104)
09-01 13:31:33.789: E/->>(3458): at com.example.boombastic.Playlist$1.onItemClick(Playlist.java:75)
09-01 13:31:33.789: E/->>(3458): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-01 13:31:33.789: E/->>(3458): at android.widget.AbsListView.performItemClick(AbsListView.java:1070)
09-01 13:31:33.789: E/->>(3458): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2527)
09-01 13:31:33.789: E/->>(3458): at android.widget.AbsListView$1.run(AbsListView.java:3188)
09-01 13:31:33.789: E/->>(3458): at android.os.Handler.handleCallback(Handler.java:605)
09-01 13:31:33.789: E/->>(3458): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 13:31:33.789: E/->>(3458): at android.os.Looper.loop(Looper.java:137)
09-01 13:31:33.789: E/->>(3458): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-01 13:31:33.789: E/->>(3458): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 13:31:33.789: E/->>(3458): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 13:31:33.789: E/->>(3458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
09-01 13:31:33.789: E/->>(3458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-01 13:31:33.789: E/->>(3458): at dalvik.system.NativeStart.main(Native Method)
This is my class containing the code:
public class Playlist extends Activity
{
ListView lstview;
ArrayList<String> Songslist;
ArrayList<String> Path;
ArrayAdapter<String> Adapteraa;
Cursor cursor;
Uri allsongsuri;
String selection;
Thread tsearchsongs;
MediaPlayer mMediaPlayer=new MediaPlayer();
Toast t1;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
//PREPARATIONS FOR ACTIVITY
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
// Show the Up button in the action bar.
setupActionBar();
//INITIALIZATIONS
lstview=(ListView)findViewById(R.string.playlistHolder);
Songslist=new ArrayList<String>();
Path=new ArrayList<String>();
Adapteraa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Songslist);
lstview.setAdapter(Adapteraa);
String[] STAR = { "*" };
allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor=managedQuery(allsongsuri, STAR, selection, null, null);
t1=new Toast(getApplicationContext());
GETSONGS();
//LISTENERS
lstview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
// TODO Auto-generated method stub
int songclicked=arg2;
String songpath=Path.get(songclicked).toString();
try
{
playSong(songpath);
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void playSong(String path) throws IllegalArgumentException, IllegalStateException, IOException
{
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
path = extStorageDirectory + File.separator + path;
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
Do I need to start a service for this? And do I need to declare any permissions for the playing of sound as it is for storage?
Upvotes: 0
Views: 138
Reputation: 798
1) Check if the file you are trying to play exists and the path is correct before you play it.
2) Try a different audio format, like mp3.
3) Use a file descriptor instead of the file path, i.e.
FileInputStream fis = new FileInputStream(pathoffiletobeplayed); mediaPlayer.setDataSource(fis.getFD());
Upvotes: 1
Reputation: 773
You can use media controller to set the media player. Take a look at the link if it helps http://intransitione.com/blog/playing-an-audio-file-with-android/
Upvotes: 0