尝试播放播放列表中的下一首歌曲时出现非法状态异常
Illeagal state exception while trying to play the next song from playlist
我可以播放播放列表中的选定歌曲,但在尝试播放播放列表中的下一首歌曲时出现非法状态异常。能够获取到下一首歌曲的路径。从 SD 卡中检索歌曲。我究竟做错了什么?问题是什么?
package com.example.mp3;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {
ListView list;
ArrayAdapter<String> listAdapter ;
ArrayList<String> listTest;
ArrayList<String> listSoundNames;
ImageButton play,stop,back,next;
String songpath,song,strNURL,strNTITLE;
int index,current_position;
File[] listFile;
SharedPreferences sharedPref;
MediaPlayer mp;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
song = sharedPref.getString("songname", "name");
mp=new MediaPlayer();
mp.setOnCompletionListener(this);
list = (ListView)findViewById(R.id.list);
listTest = new ArrayList<String>( );
listSoundNames=new ArrayList<String>();
play = (ImageButton)findViewById(R.id.play);
back = (ImageButton)findViewById(R.id.prev);
next = (ImageButton)findViewById(R.id.next);
//adding listeners
play.setOnClickListener(this);
back.setOnClickListener(this);
next.setOnClickListener(this);
//action bar controls
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
Scanner("/sdcard/");////storage path
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(listTest.size() != 0)
{
listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listSoundNames);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) //can two text be accessed here??
{
index = position;
//accessing the song name
String name = (String) ((TextView) view).getText();
//Log.e(TAG, name);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
try{
mp.reset();
mp.setDataSource(listTest.get(position));//source
mp.prepare();
mp.start();
play.setImageResource(R.drawable.pause);
}
catch(Exception e){e.printStackTrace();}
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////*Songs added here to list*////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
private void Scanner(String path) {
// TODO Auto-generated method stub
{
try
{
File fl = new File(path);
File[] listOfFiles = fl.listFiles();
for (File listOfFile : listOfFiles)
{
String s = listOfFile.getName();
if(s.endsWith(".mp3"))
{
songpath = listOfFile.getPath();
listTest.add(songpath);//adding song names to list
//listTest.toString().replaceFirst(songpath, s);
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0)
{
song = s.substring(0, pos);
}
listSoundNames.add(song);
}
/////////////////////////////////
File f = new File(path+s+"/");
if (f.exists() && f.isDirectory()) {
Scanner(path+s+"/");
}
////////////////////////////////
}
}
catch (Exception e) { }
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(play))
{
if(mp.isPlaying())
{
mp.pause();
Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.play);
}
else
{
mp.start();
Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.pause);
//
}
}
if (v.equals(back))
{
//mp.setDataSource(Uri.parse(listTest.get(index).toString()));
//index= mp.getCurrentPosition();
//index = list.getCheckedItemPosition();
//index = index -1;
//mp.setDataSource(Uri.parse(listTest.get(index).toString()));
/*try {
mp.setDataSource(listTest.get(index));
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
*/
Toast.makeText(MainActivity.this, ""+index, Toast.LENGTH_SHORT).show();
}
if (v.equals(next))
{
mp.stop();
index = index +1;
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();//PROBLEM: NOT PLAYING
Toast.makeText(MainActivity.this, ""+uri, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
super.onPause();
mp.stop();
Toast.makeText(getApplicationContext(), "paused", Toast.LENGTH_LONG).show();
}
}
LOGCAT:
> 10-09 13:19:49.995: E/MediaPlayer(6863): attachNewPlayer called in
> state 64 10-09 13:19:49.997: W/System.err(6863):
> java.lang.IllegalStateException 10-09 13:19:50.009:
> W/System.err(6863): at android.media.MediaPlayer.setDataSource(Native
> Method) 10-09 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:984) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:963) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:926) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:874) 10-09
> 13:19:50.011: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:858) 10-09
> 13:19:50.011: W/System.err(6863): at
> com.example.mp3.MainActivity.onClick(MainActivity.java:258) 10-09
> 13:19:50.011: W/System.err(6863): at
> android.view.View.performClick(View.java:4212) 10-09 13:19:50.011:
> W/System.err(6863): at
> android.view.View$PerformClick.run(View.java:17476) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Handler.handleCallback(Handler.java:800) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Handler.dispatchMessage(Handler.java:100) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Looper.loop(Looper.java:194) 10-09 13:19:50.012:
> W/System.err(6863): at
> android.app.ActivityThread.main(ActivityThread.java:5371) 10-09
> 13:19:50.012: W/System.err(6863): at
> java.lang.reflect.Method.invokeNative(Native Method) 10-09
> 13:19:50.012: W/System.err(6863): at
> java.lang.reflect.Method.invoke(Method.java:525) 10-09 13:19:50.012:
> W/System.err(6863): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
> 10-09 13:19:50.013: W/System.err(6863): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 10-09
> 13:19:50.013: W/System.err(6863): at
> dalvik.system.NativeStart.main(Native Method) 10-09 13:19:50.020:
> D/MediaPlayer(6863): Don't notify duration to com.example.mp3! 10-09
> 13:19:51.606: V/Provider/Settings(6863): from settings cache , name =
> sound_effects_enabled , value = 0 10-09 13:19:51.629:
> I/SurfaceTextureClient(6863): [STC::queueBuffer] (this:0x516f4640)
> fps:0.32, dur:6177.53, max:6150.98, min:26.54 10-09 13:19:51.629:
> I/SurfaceTextureClient(6863): [STC::queueBuffer] this:0x516f4640,
> api:1, last queue time elapsed:6150.98
在更改 MediaPlayer dataSource 之前,您需要通过调用 reset() 将 MP 状态设置回空闲状态,然后再次 prepare() 它
我可以播放播放列表中的选定歌曲,但在尝试播放播放列表中的下一首歌曲时出现非法状态异常。能够获取到下一首歌曲的路径。从 SD 卡中检索歌曲。我究竟做错了什么?问题是什么?
package com.example.mp3;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {
ListView list;
ArrayAdapter<String> listAdapter ;
ArrayList<String> listTest;
ArrayList<String> listSoundNames;
ImageButton play,stop,back,next;
String songpath,song,strNURL,strNTITLE;
int index,current_position;
File[] listFile;
SharedPreferences sharedPref;
MediaPlayer mp;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
song = sharedPref.getString("songname", "name");
mp=new MediaPlayer();
mp.setOnCompletionListener(this);
list = (ListView)findViewById(R.id.list);
listTest = new ArrayList<String>( );
listSoundNames=new ArrayList<String>();
play = (ImageButton)findViewById(R.id.play);
back = (ImageButton)findViewById(R.id.prev);
next = (ImageButton)findViewById(R.id.next);
//adding listeners
play.setOnClickListener(this);
back.setOnClickListener(this);
next.setOnClickListener(this);
//action bar controls
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
Scanner("/sdcard/");////storage path
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(listTest.size() != 0)
{
listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listSoundNames);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) //can two text be accessed here??
{
index = position;
//accessing the song name
String name = (String) ((TextView) view).getText();
//Log.e(TAG, name);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
try{
mp.reset();
mp.setDataSource(listTest.get(position));//source
mp.prepare();
mp.start();
play.setImageResource(R.drawable.pause);
}
catch(Exception e){e.printStackTrace();}
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////*Songs added here to list*////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
private void Scanner(String path) {
// TODO Auto-generated method stub
{
try
{
File fl = new File(path);
File[] listOfFiles = fl.listFiles();
for (File listOfFile : listOfFiles)
{
String s = listOfFile.getName();
if(s.endsWith(".mp3"))
{
songpath = listOfFile.getPath();
listTest.add(songpath);//adding song names to list
//listTest.toString().replaceFirst(songpath, s);
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0)
{
song = s.substring(0, pos);
}
listSoundNames.add(song);
}
/////////////////////////////////
File f = new File(path+s+"/");
if (f.exists() && f.isDirectory()) {
Scanner(path+s+"/");
}
////////////////////////////////
}
}
catch (Exception e) { }
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(play))
{
if(mp.isPlaying())
{
mp.pause();
Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.play);
}
else
{
mp.start();
Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.pause);
//
}
}
if (v.equals(back))
{
//mp.setDataSource(Uri.parse(listTest.get(index).toString()));
//index= mp.getCurrentPosition();
//index = list.getCheckedItemPosition();
//index = index -1;
//mp.setDataSource(Uri.parse(listTest.get(index).toString()));
/*try {
mp.setDataSource(listTest.get(index));
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
*/
Toast.makeText(MainActivity.this, ""+index, Toast.LENGTH_SHORT).show();
}
if (v.equals(next))
{
mp.stop();
index = index +1;
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();//PROBLEM: NOT PLAYING
Toast.makeText(MainActivity.this, ""+uri, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
super.onPause();
mp.stop();
Toast.makeText(getApplicationContext(), "paused", Toast.LENGTH_LONG).show();
}
}
LOGCAT:
> 10-09 13:19:49.995: E/MediaPlayer(6863): attachNewPlayer called in
> state 64 10-09 13:19:49.997: W/System.err(6863):
> java.lang.IllegalStateException 10-09 13:19:50.009:
> W/System.err(6863): at android.media.MediaPlayer.setDataSource(Native
> Method) 10-09 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:984) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:963) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:926) 10-09
> 13:19:50.010: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:874) 10-09
> 13:19:50.011: W/System.err(6863): at
> android.media.MediaPlayer.setDataSource(MediaPlayer.java:858) 10-09
> 13:19:50.011: W/System.err(6863): at
> com.example.mp3.MainActivity.onClick(MainActivity.java:258) 10-09
> 13:19:50.011: W/System.err(6863): at
> android.view.View.performClick(View.java:4212) 10-09 13:19:50.011:
> W/System.err(6863): at
> android.view.View$PerformClick.run(View.java:17476) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Handler.handleCallback(Handler.java:800) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Handler.dispatchMessage(Handler.java:100) 10-09
> 13:19:50.012: W/System.err(6863): at
> android.os.Looper.loop(Looper.java:194) 10-09 13:19:50.012:
> W/System.err(6863): at
> android.app.ActivityThread.main(ActivityThread.java:5371) 10-09
> 13:19:50.012: W/System.err(6863): at
> java.lang.reflect.Method.invokeNative(Native Method) 10-09
> 13:19:50.012: W/System.err(6863): at
> java.lang.reflect.Method.invoke(Method.java:525) 10-09 13:19:50.012:
> W/System.err(6863): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
> 10-09 13:19:50.013: W/System.err(6863): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 10-09
> 13:19:50.013: W/System.err(6863): at
> dalvik.system.NativeStart.main(Native Method) 10-09 13:19:50.020:
> D/MediaPlayer(6863): Don't notify duration to com.example.mp3! 10-09
> 13:19:51.606: V/Provider/Settings(6863): from settings cache , name =
> sound_effects_enabled , value = 0 10-09 13:19:51.629:
> I/SurfaceTextureClient(6863): [STC::queueBuffer] (this:0x516f4640)
> fps:0.32, dur:6177.53, max:6150.98, min:26.54 10-09 13:19:51.629:
> I/SurfaceTextureClient(6863): [STC::queueBuffer] this:0x516f4640,
> api:1, last queue time elapsed:6150.98
在更改 MediaPlayer dataSource 之前,您需要通过调用 reset() 将 MP 状态设置回空闲状态,然后再次 prepare() 它