您好我一直在尝试使用此代码一段时间,但它给logcat带来了错误,如下所示:
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)
栈跟踪
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)
这是我的包含代码的类:
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();
}
我需要为此启动服务吗? 我需要声明播放声音的任何权限吗?
答案 0 :(得分:1)
1)在播放之前检查您尝试播放的文件是否存在且路径是否正确。
2)尝试不同的音频格式,如mp3。
3)使用文件描述符而不是文件路径,即
FileInputStream fis = new FileInputStream(pathoffiletobeplayed); mediaPlayer.setDataSource(fis.getFD()); 强>
答案 1 :(得分:0)
您可以使用媒体控制器设置媒体播放器。如果它有帮助,请查看链接 http://intransitione.com/blog/playing-an-audio-file-with-android/